-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #472 from DinoChiesa/target-lb-checks
feat: TD008 and TD009 to perform checks on LoadBalancer
- Loading branch information
Showing
17 changed files
with
520 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,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 | ||
}; |
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 @@ | ||
/* | ||
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 | ||
}; |
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,8 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<Server name="server1"/> | ||
<Server name="server1"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
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,9 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<Server name="server1"/> | ||
<Server name="server2"/> | ||
<Server name="server1"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
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,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> |
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,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> |
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,9 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<Server name="server1"/> | ||
<Server /> | ||
<Server name="server3"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
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,8 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<Server name="server1"/> | ||
<Server name="server2"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
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,9 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<Server name="server1"/> | ||
<Server name="server2"/> | ||
<Server name="server3"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
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 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<Server name="server1"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
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,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> |
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,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> |
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,9 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<!-- a single LoadBalancer element will pass the TD009 check --> | ||
<LoadBalancer> | ||
<Server name="server1"/> | ||
<Server name="server2"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
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 @@ | ||
<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> |
Oops, something went wrong.