-
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.
chore: code cleanup. add new test for PO039.
- Loading branch information
1 parent
9f04192
commit 1d602c4
Showing
12 changed files
with
326 additions
and
171 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
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 testID = "PO039", | ||
assert = require("assert"), | ||
fs = require("fs"), | ||
path = require("path"), | ||
bl = require("../../lib/package/bundleLinter.js"), | ||
plugin = require(bl.resolvePlugin(testID)), | ||
debug = require("debug")("apigeelint:" + testID), | ||
Policy = require("../../lib/package/Policy.js"), | ||
Dom = require("@xmldom/xmldom").DOMParser, | ||
rootDir = path.resolve(__dirname, "../fixtures/resources/PO039"); | ||
|
||
const test = (suffix, cb) => { | ||
const filename = `ML-test-${suffix}.xml`; | ||
it(`should correctly process ${filename}`, () => { | ||
const fqfname = path.join(rootDir, filename), | ||
policyXml = fs.readFileSync(fqfname, "utf-8"), | ||
doc = new Dom().parseFromString(policyXml), | ||
p = new Policy(rootDir, filename, this, doc); | ||
|
||
p.getElement = () => doc.documentElement; | ||
|
||
//plugin.onBundle({ profile: "apigee" }); | ||
|
||
plugin.onPolicy(p, (e, foundIssues) => { | ||
assert.equal(e, undefined, "should be undefined"); | ||
cb(p, foundIssues); | ||
}); | ||
}); | ||
}; | ||
|
||
describe(`${testID} - MessageLogging RessourceType element`, () => { | ||
// test all the valid cases | ||
fs.readdirSync(rootDir) | ||
.map((shortFileName) => { | ||
let m = shortFileName.match("^.+-(valid.+)\\.xml$"); | ||
if (m) { | ||
return m[1]; | ||
} | ||
}) | ||
.filter((suffix) => suffix) | ||
.forEach((suffix) => { | ||
test(suffix, (p, foundIssues) => { | ||
const messages = p.getReport().messages; | ||
assert.ok(messages, "messages undefined"); | ||
debug(messages); | ||
assert.equal(foundIssues, false); | ||
}); | ||
}); | ||
|
||
test("invalid1", (p, foundIssues) => { | ||
assert.equal(foundIssues, true); | ||
const messages = p.getReport().messages; | ||
assert.ok(messages, "messages undefined"); | ||
debug(messages); | ||
assert.equal(messages.length, 1, "unexpected number of messages"); | ||
assert.ok(messages[0].message, "did not find message 0"); | ||
assert.equal( | ||
messages[0].message, | ||
"The value 'gce_instance' should not be used here. ResourceType should be 'api'", | ||
); | ||
}); | ||
|
||
test("invalid2", (p, foundIssues) => { | ||
assert.equal(foundIssues, true); | ||
const messages = p.getReport().messages; | ||
assert.ok(messages, "messages undefined"); | ||
debug(messages); | ||
assert.equal(messages.length, 1, "unexpected number of messages"); | ||
assert.ok(messages[0].message, "did not find message 0"); | ||
assert.equal( | ||
messages[0].message, | ||
"The value 'apigee.googleapis.com/Environment' should not be used here. ResourceType should be 'api'", | ||
); | ||
}); | ||
|
||
test("invalid3", (p, foundIssues) => { | ||
assert.equal(foundIssues, true); | ||
const messages = p.getReport().messages; | ||
assert.ok(messages, "messages undefined"); | ||
debug(messages); | ||
assert.equal(messages.length, 2, "unexpected number of messages"); | ||
assert.ok(messages[0].message, "did not find message 0"); | ||
assert.equal(messages[0].message, "Unsupported element 'NotKey'"); | ||
assert.equal( | ||
messages[1].message, | ||
"Label is missing a required Element: Key.", | ||
); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
externalPlugins/EX-PO001-CheckForPoliciesWhileStreaming.js
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,87 @@ | ||
/* | ||
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 plugin = { | ||
ruleId: "EX-PO001", | ||
name: "Streaming", | ||
message: "Check for policies while streaming is enabled", | ||
fatal: false, | ||
severity: 2, // 1 = warn, 2 = error | ||
nodeType: "Bundle", | ||
enabled: true, | ||
}; | ||
|
||
const onBundle = function (bundle, cb) { | ||
let flagged = false, | ||
isProxyStreamingEnabled = false, | ||
isTargetStreamingEnabled = false; | ||
const proxies = bundle.getProxyEndpoints(); | ||
proxies.forEach((proxyEndpoint, _p) => { | ||
const httpProxyConnection = proxyEndpoint.getHTTPProxyConnection(); | ||
if (httpProxyConnection) { | ||
let properties = httpProxyConnection.getProperties(); | ||
if ( | ||
properties && | ||
(properties["request.streaming.enabled"] == "true" || | ||
properties["response.streaming.enabled"] == "true") | ||
) { | ||
isProxyStreamingEnabled = true; | ||
} | ||
} | ||
}); | ||
const targets = bundle.getTargetEndpoints(); | ||
targets.forEach((targetEndpoint, _t) => { | ||
const httpTargetConnection = targetEndpoint.getHTTPTargetConnection(); | ||
if (httpTargetConnection) { | ||
let properties = httpTargetConnection.getProperties(); | ||
if ( | ||
properties && | ||
(properties["request.streaming.enabled"] == "true" || | ||
properties["response.streaming.enabled"] == "true") | ||
) { | ||
isTargetStreamingEnabled = true; | ||
} | ||
} | ||
}); | ||
|
||
if (isProxyStreamingEnabled || isTargetStreamingEnabled) { | ||
bundle.getPolicies().forEach(function (policy) { | ||
if ( | ||
(policy.getType() === "AssignMessage" || | ||
policy.getType() === "ExtractVariables") && | ||
policy.getSteps().length > 0 | ||
) { | ||
bundle.addMessage({ | ||
plugin, | ||
source: policy.getSource(), | ||
line: policy.getElement().lineNumber, | ||
column: policy.getElement().columnNumber, | ||
message: | ||
"ExtractVariables/AssignMessage policies not allowed when streaming is enabled", | ||
}); | ||
flagged = true; | ||
} | ||
}); | ||
} | ||
if (typeof cb == "function") { | ||
cb(null, flagged); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
plugin, | ||
onBundle, | ||
}; |
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
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
Oops, something went wrong.