-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: BN012 plugin to find unreferenced targets
- Loading branch information
1 parent
d2a52a6
commit 4efce60
Showing
11 changed files
with
406 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2019-2024 Google LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
const ruleId = require("../myUtil.js").getRuleId(), | ||
util = require("util"), | ||
debug = require("debug")("apigeelint:" + ruleId); | ||
|
||
const plugin = { | ||
ruleId, | ||
name: "Check for unreferenced targets", | ||
message: | ||
"Unreferenced targets are dead code; should be removed from bundles.", | ||
fatal: false, | ||
severity: 1, //1=warn, 2=error | ||
nodeType: "Bundle", | ||
enabled: true | ||
}; | ||
|
||
const onBundle = function (bundle, cb) { | ||
const proxies = bundle.getProxyEndpoints(), | ||
targets = bundle | ||
.getTargetEndpoints() | ||
.reduce((a, c) => ((a[c.getName()] = { count: 0, ep: c }), a), {}); | ||
debug(`targetmap: ${util.format(targets)}`); | ||
let flagged = false; | ||
if (Object.keys(targets).length) { | ||
proxies.forEach((proxyEndpoint, _pix) => { | ||
const routeRules = proxyEndpoint.getRouteRules(); | ||
routeRules.forEach((rr, rrix) => { | ||
const targetName = rr.getTargetEndpoint(); | ||
debug(`rr${rrix} = "${targetName}"`); | ||
if (targetName) { | ||
if (targets[targetName]) { | ||
targets[targetName].count++; | ||
} else { | ||
// unknown target - caught by a different plugin | ||
} | ||
} | ||
}); | ||
}); | ||
const unreferenced = Object.keys(targets).filter( | ||
(key) => targets[key].count == 0 | ||
); | ||
if (unreferenced.length) { | ||
flagged = true; | ||
unreferenced.forEach((tepname) => { | ||
debug(`unreferenced: ${tepname}`); | ||
bundle.addMessage({ | ||
plugin, | ||
entity: targets[tepname].ep, | ||
message: `Unreferenced TargetEndpoint ${tepname}. There are no RouteRules that reference this TargetEndpoint.` | ||
}); | ||
}); | ||
} | ||
} | ||
if (typeof cb == "function") { | ||
cb(null, flagged); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
plugin, | ||
onBundle | ||
}; |
7 changes: 7 additions & 0 deletions
7
...resources/BN012-unreferenced-target/apiproxy/policies/AM-Inject-Proxy-Revision-Header.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
16 changes: 16 additions & 0 deletions
16
test/fixtures/resources/BN012-unreferenced-target/apiproxy/policies/RF-Unknown-Request.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
70 changes: 70 additions & 0 deletions
70
test/fixtures/resources/BN012-unreferenced-target/apiproxy/proxies/endpoint1.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<ProxyEndpoint name="endpoint1"> | ||
|
||
<HTTPProxyConnection> | ||
<BasePath>/unreferenced-target</BasePath> | ||
<Properties/> | ||
<VirtualHost>secure</VirtualHost> | ||
</HTTPProxyConnection> | ||
|
||
<FaultRules/> | ||
<DefaultFaultRule name="default-fault-rule"> | ||
<Step> | ||
<Name>AM-Inject-Proxy-Revision-Header</Name> | ||
</Step> | ||
<AlwaysEnforce>true</AlwaysEnforce> | ||
</DefaultFaultRule> | ||
|
||
<PreFlow name="PreFlow"> | ||
<Request/> | ||
<Response/> | ||
</PreFlow> | ||
|
||
<PostFlow name="PostFlow"> | ||
<Request/> | ||
<Response> | ||
<Step> | ||
<Name>AM-Inject-Proxy-Revision-Header</Name> | ||
</Step> | ||
</Response> | ||
</PostFlow> | ||
|
||
<PostClientFlow name="PostClientFlow"> | ||
<Request/> | ||
<Response> | ||
</Response> | ||
</PostClientFlow> | ||
|
||
<Flows> | ||
<Flow name="t1"> | ||
<Request> | ||
</Request> | ||
<Response> | ||
</Response> | ||
<Condition>proxy.pathsuffix MatchesPath "/t1" and request.verb = "GET"</Condition> | ||
</Flow> | ||
|
||
<Flow name="t2"> | ||
<Request> | ||
</Request> | ||
<Response> | ||
</Response> | ||
<Condition>proxy.pathsuffix MatchesPath "/t2" and request.verb = "POST"</Condition> | ||
</Flow> | ||
|
||
<Flow name="unknown request"> | ||
<Request> | ||
<Step> | ||
<Name>RF-Unknown-Request</Name> | ||
</Step> | ||
</Request> | ||
<Response> | ||
</Response> | ||
</Flow> | ||
|
||
</Flows> | ||
|
||
<RouteRule name="default"> | ||
<TargetEndpoint>target-1</TargetEndpoint> | ||
</RouteRule> | ||
|
||
</ProxyEndpoint> |
70 changes: 70 additions & 0 deletions
70
test/fixtures/resources/BN012-unreferenced-target/apiproxy/proxies/endpoint2.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<ProxyEndpoint name="endpoint2"> | ||
|
||
<HTTPProxyConnection> | ||
<BasePath>/unreferenced-target/2</BasePath> | ||
<Properties/> | ||
<VirtualHost>secure</VirtualHost> | ||
</HTTPProxyConnection> | ||
|
||
<FaultRules/> | ||
<DefaultFaultRule name="default-fault-rule"> | ||
<Step> | ||
<Name>AM-Inject-Proxy-Revision-Header</Name> | ||
</Step> | ||
<AlwaysEnforce>true</AlwaysEnforce> | ||
</DefaultFaultRule> | ||
|
||
<PreFlow name="PreFlow"> | ||
<Request/> | ||
<Response/> | ||
</PreFlow> | ||
|
||
<PostFlow name="PostFlow"> | ||
<Request/> | ||
<Response> | ||
<Step> | ||
<Name>AM-Inject-Proxy-Revision-Header</Name> | ||
</Step> | ||
</Response> | ||
</PostFlow> | ||
|
||
<PostClientFlow name="PostClientFlow"> | ||
<Request/> | ||
<Response> | ||
</Response> | ||
</PostClientFlow> | ||
|
||
<Flows> | ||
<Flow name="t1"> | ||
<Request> | ||
</Request> | ||
<Response> | ||
</Response> | ||
<Condition>proxy.pathsuffix MatchesPath "/t1" and request.verb = "GET"</Condition> | ||
</Flow> | ||
|
||
<Flow name="t2"> | ||
<Request> | ||
</Request> | ||
<Response> | ||
</Response> | ||
<Condition>proxy.pathsuffix MatchesPath "/t2" and request.verb = "POST"</Condition> | ||
</Flow> | ||
|
||
<Flow name="unknown request"> | ||
<Request> | ||
<Step> | ||
<Name>RF-Unknown-Request</Name> | ||
</Step> | ||
</Request> | ||
<Response> | ||
</Response> | ||
</Flow> | ||
|
||
</Flows> | ||
|
||
<RouteRule name="default"> | ||
<TargetEndpoint>target-2</TargetEndpoint> | ||
</RouteRule> | ||
|
||
</ProxyEndpoint> |
26 changes: 26 additions & 0 deletions
26
test/fixtures/resources/BN012-unreferenced-target/apiproxy/targets/target-1.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<TargetEndpoint name="target-1"> | ||
<Description/> | ||
<FaultRules/> | ||
<PreFlow name="PreFlow"> | ||
<Request/> | ||
<Response/> | ||
</PreFlow> | ||
<PostFlow name="PostFlow"> | ||
<Request/> | ||
<Response/> | ||
</PostFlow> | ||
<Flows/> | ||
<HTTPTargetConnection> | ||
<SSLInfo> | ||
<Enabled>true</Enabled> | ||
<IgnoreValidationErrors>false</IgnoreValidationErrors> | ||
</SSLInfo> | ||
<Properties> | ||
<Property name="success.codes">1xx,2xx,3xx</Property> | ||
<Property name="request.retain.headers">User-Agent,Referer,Accept-Language</Property> | ||
<Property name="retain.queryparams">apikey</Property> | ||
</Properties> | ||
|
||
<URL>https://target-1.demo.altostrat.com</URL> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
26 changes: 26 additions & 0 deletions
26
test/fixtures/resources/BN012-unreferenced-target/apiproxy/targets/target-2.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<TargetEndpoint name="target-2"> | ||
<Description/> | ||
<FaultRules/> | ||
<PreFlow name="PreFlow"> | ||
<Request/> | ||
<Response/> | ||
</PreFlow> | ||
<PostFlow name="PostFlow"> | ||
<Request/> | ||
<Response/> | ||
</PostFlow> | ||
<Flows/> | ||
<HTTPTargetConnection> | ||
<SSLInfo> | ||
<Enabled>true</Enabled> | ||
<IgnoreValidationErrors>false</IgnoreValidationErrors> | ||
</SSLInfo> | ||
<Properties> | ||
<Property name="success.codes">1xx,2xx,3xx</Property> | ||
<Property name="request.retain.headers">User-Agent,Referer,Accept-Language</Property> | ||
<Property name="retain.queryparams">apikey</Property> | ||
</Properties> | ||
|
||
<URL>https://target-2.demo.altostrat.com</URL> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
26 changes: 26 additions & 0 deletions
26
test/fixtures/resources/BN012-unreferenced-target/apiproxy/targets/target-3.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<TargetEndpoint name="target-3"> | ||
<Description/> | ||
<FaultRules/> | ||
<PreFlow name="PreFlow"> | ||
<Request/> | ||
<Response/> | ||
</PreFlow> | ||
<PostFlow name="PostFlow"> | ||
<Request/> | ||
<Response/> | ||
</PostFlow> | ||
<Flows/> | ||
<HTTPTargetConnection> | ||
<SSLInfo> | ||
<Enabled>true</Enabled> | ||
<IgnoreValidationErrors>false</IgnoreValidationErrors> | ||
</SSLInfo> | ||
<Properties> | ||
<Property name="success.codes">1xx,2xx,3xx</Property> | ||
<Property name="request.retain.headers">User-Agent,Referer,Accept-Language</Property> | ||
<Property name="retain.queryparams">apikey</Property> | ||
</Properties> | ||
|
||
<URL>https://target-3.demo.altostrat.com</URL> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
18 changes: 18 additions & 0 deletions
18
test/fixtures/resources/BN012-unreferenced-target/apiproxy/unreferenced-target.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<APIProxy name="unused-target"> | ||
<ConfigurationVersion majorVersion="4" minorVersion="0"/> | ||
<CreatedAt>1491498008040</CreatedAt> | ||
<Description/> | ||
<DisplayName>unused-target</DisplayName> | ||
<LastModifiedAt>1491517153495</LastModifiedAt> | ||
<Policies/> | ||
<ProxyEndpoints> | ||
<ProxyEndpoint>default</ProxyEndpoint> | ||
</ProxyEndpoints> | ||
<Resources/> | ||
<Spec/> | ||
<TargetServers/> | ||
<TargetEndpoints> | ||
<TargetEndpoint>default</TargetEndpoint> | ||
</TargetEndpoints> | ||
<validate>false</validate> | ||
</APIProxy> |
Oops, something went wrong.