Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: BN013 correct handling of resource shortname #486

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/package/plugins/BN013-unreferenced-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ const onBundle = function (bundle, cb) {
return;
}
const trailingPath = resource.path.substr(ix + marker.length),
[rtype, rshortname] = trailingPath.split("/", 2);
parts = trailingPath.split("/"),
rtype = parts[0],
rshortname = parts.slice(1).join("/");
debug(
`trailingPath(${trailingPath}) rtype(${rtype}) rshortname(${rshortname})`
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<AssignMessage name='AM-Clean-Request-Headers-From-Response'>
<Remove>
<Headers>
<Header name='Host'/>
<Header name='Accept'/>
<Header name='Authorization'/>
<Header name='Origin'/>
<Header name='user-agent'/>
<Header name='X-Request-id'/>
<Header name='x-client-data'/>
<Header name='x-cloud-trace-context'/>
<Header name='X-Powered-By'/>
<Header name='X-b3-traceid'/>
<Header name='X-b3-spanid'/>
<Header name='X-b3-sampled'/>
<Header name='X-Forwarded-For'/>
<Header name='X-Forwarded-Port'/>
<Header name='X-Forwarded-Proto'/>
</Headers>
</Remove>
</AssignMessage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<AssignMessage name='AM-Inject-Proxy-Revision-Header'>
<Set>
<Headers>
<Header name='APIProxy'>{apiproxy.name} r{apiproxy.revision}</Header>
</Headers>
</Set>
</AssignMessage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<AssignMessage name='AM-Response'>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignVariable>
<Name>examplevalue</Name>
<Value>The Spanish letter ñ, also known as eñe</Value>
</AssignVariable>
<Set>
<Headers>
<!-- the following will not be encoded correctly -->
<Header name='test-header'>{examplevalue}</Header>
<!-- wrapping the non-ASCII in base64 works around the issue; the caller must decode -->
<Header name='encoded-header'>{encodeBase64(examplevalue)}</Header>
</Headers>
<!-- payload is always encoded correctly -->
<Payload contentType='application/json'>{
"status" : "ok",
"message" : "Spanish ñ is known as eñe"
}
</Payload>
<ReasonPhrase>OK</ReasonPhrase>
<StatusCode>200</StatusCode>
</Set>
<AssignTo>response</AssignTo>
</AssignMessage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Javascript name='JS-Example-Policy' timeLimit='1200' >
<Properties>
<!-- to retrieve properties in js code: properties.prop1 -->
<Property name='prop1'>value-here</Property>
</Properties>
<ResourceURL>jsc://responses/example.js</ResourceURL>
</Javascript>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<RaiseFault name='RF-Unknown-Request'>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<FaultResponse>
<Set>
<Payload contentType='application/json'>{
"error" : {
"code" : 404.01,
"message" : "that request was unknown; try a different request."
}
}
</Payload>
<StatusCode>404</StatusCode>
<ReasonPhrase>Not Found</ReasonPhrase>
</Set>
</FaultResponse>
</RaiseFault>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<ProxyEndpoint name="endpoint1">
<Description>Proxy Endpoint 1</Description>
<HTTPProxyConnection>
<BasePath>/test-issue482</BasePath>
</HTTPProxyConnection>

<FaultRules/>
<DefaultFaultRule name="default-fault-rule">
<Step>
<Name>AM-Inject-Proxy-Revision-Header</Name>
</Step>
<AlwaysEnforce>true</AlwaysEnforce>
</DefaultFaultRule>

<PreFlow name="PreFlow">
<Request>
</Request>
<Response>
<Step>
<Name>AM-Clean-Request-Headers-From-Response</Name>
</Step>
</Response>
</PreFlow>

<PostFlow name="PostFlow">
<Request>
</Request>
<Response>
<Step>
<Name>AM-Inject-Proxy-Revision-Header</Name>
</Step>
</Response>
</PostFlow>

<PostClientFlow name="PostClientFlow">
<Request>
</Request>
<Response>
</Response>
</PostClientFlow>

<Flows>

<Flow name="flow1">
<Request>
</Request>
<Response>
<Step>
<Name>AM-Response</Name>
</Step>
</Response>
<Condition>(proxy.pathsuffix MatchesPath "/t1") and (request.verb = "GET")</Condition>
</Flow>

<Flow name="unknown request">
<Request>
<Step>
<Name>RF-Unknown-Request</Name>
</Step>
</Request>
<Response>
</Response>
</Flow>

</Flows>

<RouteRule name="NoRouteRule"/>

</ProxyEndpoint>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var thing = "goo";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<APIProxy revision="1" name="test-issue482">
<ConfigurationVersion minorVersion="0" majorVersion="4"/>
<DisplayName>test-issue482</DisplayName>
</APIProxy>
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ const assert = require("assert"),
path = require("path"),
util = require("util"),
testID = "BN013",
debug = require("debug")("apigeelint:" + testID),
debug = require("debug")("apigeelint:${testID}-test"),
//Bundle = require("../../lib/package/Bundle.js"),
bl = require("../../lib/package/bundleLinter.js");

describe("BN013 - Check for unreferenced resources", function () {
it("should flag unused resources in the test bundle", () => {
it("should flag unused resources in bundle1", () => {
const configuration = {
debug: true,
source: {
type: "filesystem",
path: path.resolve(
__dirname,
"../fixtures/resources/BN013-unreferenced-resources/apiproxy"
"../fixtures/resources/BN013/bundle1/apiproxy"
),
bundleType: "apiproxy"
},
Expand Down Expand Up @@ -87,4 +87,40 @@ describe("BN013 - Check for unreferenced resources", function () {
}
});
});

it("should flag no issues in test-issue482", () => {
const configuration = {
debug: true,
source: {
type: "filesystem",
path: path.resolve(
__dirname,
"../fixtures/resources/BN013/test-issue482/apiproxy"
),
bundleType: "apiproxy"
},
profile: "apigeex",
excluded: {},
setExitCode: false,
output: () => {} // suppress output
};

debug("test configuration: " + JSON.stringify(configuration));
bl.lint(configuration, (bundle) => {
const items = bundle.getReport();
assert.ok(items);
assert.ok(items.length);
const actualErrors = items.filter(
(item) => item.messages && item.messages.length
);
debug(util.format(actualErrors));
const bn013Items = actualErrors.filter((e) =>
e.messages.find((m) => m.ruleId == "BN013")
);
debug("BN013 items: " + util.format(bn013Items));
assert.equal(bn013Items.length, 0);
});
});


});
Loading