Skip to content

Commit

Permalink
Merge pull request #472 from DinoChiesa/target-lb-checks
Browse files Browse the repository at this point in the history
feat: TD008 and TD009 to perform checks on LoadBalancer
  • Loading branch information
ssvaidyanathan authored Sep 27, 2024
2 parents e21e568 + 0c88e64 commit 0e85491
Show file tree
Hide file tree
Showing 17 changed files with 520 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ This is the current list:
|   |:white_check_mark:| TD005 | TargetEndpoint SSLInfo references | TargetEndpoint SSLInfo should use references for KeyStore and TrustStore. |
|   |:white_check_mark:| TD006 | TargetEndpoint SSLInfo | When using a LoadBalancer, the SSLInfo should not be configured under HTTPTargetConnection. |
|   |:white_check_mark:| TD007 | TargetEndpoint SSLInfo | TargetEndpoint HTTPTargetConnection SSLInfo should use TrustStore. |
|   |:white_check_mark:| TD008 | TargetEndpoint LoadBalancer Servers | LoadBalancer should not have duplicate Server entries or multiple Fallbacks. |
|   |:white_check_mark:| TD009 | TargetEndpoint LoadBalancer | TargetEndpoint HTTPTargetConnection should have at most one LoadBalancer. |
| Flow |   |   |   |   |
|   |:white_check_mark:| FL001 | Unconditional Flows | Only one unconditional flow will get executed. Error if more than one was detected. |
| Step |   |   |   |   |
Expand Down
138 changes: 138 additions & 0 deletions lib/package/plugins/TD008-target-LB-servers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
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(),
xpath = require("xpath");

const plugin = {
ruleId,
name: "TargetEndpoint HTTPTargetConnection LoadBalancer with duplicate Server elements",
message: "Duplicate Server elements found within LoadBalancer.",
fatal: false,
severity: 1, // 1 = warn, 2 = error
nodeType: "Endpoint",
enabled: true
};

const path = require("path"),
debug = require("debug")("apigeelint:" + ruleId);

const onTargetEndpoint = function (endpoint, cb) {
const htc = endpoint.getHTTPTargetConnection(),
shortFilename = path.basename(endpoint.getFileName());
let flagged = false;

debug(`onTargetEndpoint shortfile(${shortFilename})`);
if (htc) {
try {
const loadBalancers = htc.select("LoadBalancer");
debug(`loadBalancers(${loadBalancers.length})`);

if (loadBalancers.length == 1) {
const loadBalancer = loadBalancers[0];
// check multiple fallbacks
const fallbacks = xpath.select(
"Server[IsFallback = 'true']",
loadBalancer
);
if (fallbacks.length > 1) {
endpoint.addMessage({
plugin,
line: loadBalancers[0].lineNumber,
column: loadBalancers[0].columnNumber,
message: "Multiple Server entries with IsFallback=true"
});
flagged = true;
}

const servers = xpath.select("Server", loadBalancer);
if (servers.length > 1) {
const previouslyDetected = [];
servers.slice(0, -1).forEach((item, i) => {
if (!previouslyDetected.includes(i)) {
const dupesForI = [];
const compares = servers.slice(i + 1);
compares.forEach((c, j) => {
const indexOfCompared = j + i + 1;
if (!previouslyDetected.includes(indexOfCompared)) {
const attrsA = xpath.select("@*", item);
const attrsB = xpath.select("@*", c);
if (!attrsA || !attrsA.find((attr) => attr.name == "name")) {
endpoint.addMessage({
plugin,
line: item.lineNumber,
column: item.columnNumber,
message:
"Missing name attribute for server within LoadBalancer"
});
flagged = true;
} else {
const nameAttrA = attrsA.find(
(attr) => attr.name == "name"
);

if (attrsB) {
const nameAttrB = attrsB.find(
(attr) => attr.name == "name"
);
if (nameAttrB && nameAttrB.value == nameAttrA.value) {
debug(`duplicate found at index ${indexOfCompared}`);
dupesForI.push(indexOfCompared);
}
}
}
}
});

if (dupesForI.length) {
dupesForI.forEach((ix) =>
endpoint.addMessage({
plugin,
line: servers[ix].lineNumber,
column: servers[ix].columnNumber,
message: plugin.message
})
);
flagged = true;
previouslyDetected.push(...dupesForI);
}
}
});
}
}
} catch (exc1) {
console.error("exception: " + exc1);
debug(`onTargetEndpoint exc(${exc1})`);
debug(`${exc1.stack}`);
endpoint.addMessage({
plugin,
message: "Exception while processing: " + exc1
});

flagged = true;
}
}

if (typeof cb == "function") {
debug(`onTargetEndpoint isFlagged(${flagged})`);
cb(null, flagged);
}
};

module.exports = {
plugin,
onTargetEndpoint
};
70 changes: 70 additions & 0 deletions lib/package/plugins/TD009-target-LB-multiple-LB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
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();

const plugin = {
ruleId,
name: "TargetEndpoint HTTPTargetConnection should have at most one LoadBalancer",
message:
"TargetEndpoint HTTPTargetConnection should not have more than one LoadBalancer.",
fatal: false,
severity: 1, // 1 = warn, 2 = error
nodeType: "Endpoint",
enabled: true
};

const path = require("path"),
debug = require("debug")("apigeelint:" + ruleId);

const onTargetEndpoint = function (endpoint, cb) {
const htc = endpoint.getHTTPTargetConnection(),
shortFilename = path.basename(endpoint.getFileName());
let flagged = false;

debug(`onTargetEndpoint shortfile(${shortFilename})`);
if (htc) {
try {
const loadBalancers = htc.select("LoadBalancer");
debug(`loadBalancers(${loadBalancers.length})`);

if (loadBalancers.length > 1) {
endpoint.addMessage({
plugin,
line: loadBalancers[1].lineNumber,
column: loadBalancers[1].columnNumber,
message: plugin.message
});
debug(`onTargetEndpoint set flagged`);
flagged = true;
}
} catch (exc1) {
console.error("exception: " + exc1);
debug(`onTargetEndpoint exc(${exc1})`);
debug(`${exc1.stack}`);
}
}

if (typeof cb == "function") {
debug(`onTargetEndpoint isFlagged(${flagged})`);
cb(null, flagged);
}
};

module.exports = {
plugin,
onTargetEndpoint
};
8 changes: 8 additions & 0 deletions test/fixtures/resources/TD008/fail/t1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<TargetEndpoint name="default">
<HTTPTargetConnection>
<LoadBalancer>
<Server name="server1"/>
<Server name="server1"/>
</LoadBalancer>
</HTTPTargetConnection>
</TargetEndpoint>
9 changes: 9 additions & 0 deletions test/fixtures/resources/TD008/fail/t2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<TargetEndpoint name="default">
<HTTPTargetConnection>
<LoadBalancer>
<Server name="server1"/>
<Server name="server2"/>
<Server name="server1"/>
</LoadBalancer>
</HTTPTargetConnection>
</TargetEndpoint>
12 changes: 12 additions & 0 deletions test/fixtures/resources/TD008/fail/t3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<TargetEndpoint name="default">
<HTTPTargetConnection>
<LoadBalancer>
<Server name="server1"/>
<Server name="server2"/>
<Server name="server3"/>
<Server name="server2"/>
<Server name="server4"/>
<Server name="server2"/>
</LoadBalancer>
</HTTPTargetConnection>
</TargetEndpoint>
14 changes: 14 additions & 0 deletions test/fixtures/resources/TD008/fail/t4.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<TargetEndpoint name="default">
<HTTPTargetConnection>
<LoadBalancer>
<Server name="server1"/>
<!-- two servers marked fallback will fail the TD008 check -->
<Server name="server2">
<IsFallback>true</IsFallback>
</Server>
<Server name="server3">
<IsFallback>true</IsFallback>
</Server>
</LoadBalancer>
</HTTPTargetConnection>
</TargetEndpoint>
9 changes: 9 additions & 0 deletions test/fixtures/resources/TD008/fail/t5.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<TargetEndpoint name="default">
<HTTPTargetConnection>
<LoadBalancer>
<Server name="server1"/>
<Server />
<Server name="server3"/>
</LoadBalancer>
</HTTPTargetConnection>
</TargetEndpoint>
8 changes: 8 additions & 0 deletions test/fixtures/resources/TD008/pass/t1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<TargetEndpoint name="default">
<HTTPTargetConnection>
<LoadBalancer>
<Server name="server1"/>
<Server name="server2"/>
</LoadBalancer>
</HTTPTargetConnection>
</TargetEndpoint>
9 changes: 9 additions & 0 deletions test/fixtures/resources/TD008/pass/t2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<TargetEndpoint name="default">
<HTTPTargetConnection>
<LoadBalancer>
<Server name="server1"/>
<Server name="server2"/>
<Server name="server3"/>
</LoadBalancer>
</HTTPTargetConnection>
</TargetEndpoint>
7 changes: 7 additions & 0 deletions test/fixtures/resources/TD008/pass/t3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<TargetEndpoint name="default">
<HTTPTargetConnection>
<LoadBalancer>
<Server name="server1"/>
</LoadBalancer>
</HTTPTargetConnection>
</TargetEndpoint>
12 changes: 12 additions & 0 deletions test/fixtures/resources/TD008/pass/t4.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<TargetEndpoint name="default">
<HTTPTargetConnection>
<LoadBalancer>
<Server name="server1"/>
<Server name="server2"/>
<!-- single fallback is OK -->
<Server name="server3">
<IsFallback>true</IsFallback>
</Server>
</LoadBalancer>
</HTTPTargetConnection>
</TargetEndpoint>
11 changes: 11 additions & 0 deletions test/fixtures/resources/TD009/fail/t1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<TargetEndpoint name="default">
<HTTPTargetConnection>
<!-- multiple LoadBalancer elements will fail the TD009 check -->
<LoadBalancer>
<Server name="server1"/>
</LoadBalancer>
<LoadBalancer>
<Server name="server2"/>
</LoadBalancer>
</HTTPTargetConnection>
</TargetEndpoint>
9 changes: 9 additions & 0 deletions test/fixtures/resources/TD009/pass/t1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<TargetEndpoint name="default">
<HTTPTargetConnection>
<!-- a single LoadBalancer element will pass the TD009 check -->
<LoadBalancer>
<Server name="server1"/>
<Server name="server2"/>
</LoadBalancer>
</HTTPTargetConnection>
</TargetEndpoint>
16 changes: 16 additions & 0 deletions test/fixtures/resources/TD009/pass/t2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<TargetEndpoint name="t2">
<HTTPTargetConnection>

<!-- zero LoadBalancer elements will also pass the TD009 check -->

<SSLInfo>
<Enabled>true</Enabled>
<Enforce>true</Enforce>
<CommonName
wildcardMatch="false">echo.dchiesa.demo.altostrat.com</CommonName>
<TrustStore>ref://myTrustStoreRef</TrustStore>
</SSLInfo>

<URL>https://echo.dchiesa.demo.altostrat.com</URL>
</HTTPTargetConnection>
</TargetEndpoint>
Loading

0 comments on commit 0e85491

Please sign in to comment.