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

feat: BN012 plugin to find unreferenced targets #430

Merged
merged 1 commit into from
Apr 12, 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ This is the current list:
|   |:white_check_mark:| BN009 | Statistics Collector - duplicate policies | Warn on duplicate policies when no conditions are present or conditions are duplicates. |
|   |:white_check_mark:| BN010 | Missing policies | Issue an error if a referenced policy is not present in the bundle. |
|   |:white_check_mark:| BN011 | Check each XML file for well-formedness.|
|   |:white_check_mark:| BN012 | unreferrenced Target Endpoints | Check that each TargetEndpoint can be reached. |
| Proxy Definition |   |   |   |   |
|   |:white_check_mark:| PD001 | RouteRules to Targets | RouteRules should map to defined Targets. |
|   |:white_check_mark:| PD002 | Unreachable Route Rules - defaults | Only one RouteRule should be present without a condition. |
Expand Down
77 changes: 77 additions & 0 deletions lib/package/plugins/BN012-unreferencedTargets.js
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
};
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,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,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>
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>
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>
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>
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>
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>
Loading
Loading