Skip to content

Commit

Permalink
chore: code cleanup. add new test for PO039.
Browse files Browse the repository at this point in the history
  • Loading branch information
DinoChiesa committed Dec 19, 2024
1 parent 9f04192 commit 1d602c4
Show file tree
Hide file tree
Showing 12 changed files with 326 additions and 171 deletions.
105 changes: 105 additions & 0 deletions PO039-test.js
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.",
);
});
});
53 changes: 0 additions & 53 deletions externalPlugins/EX-001-CheckForPoliciesWhileStreaming.js

This file was deleted.

87 changes: 87 additions & 0 deletions externalPlugins/EX-PO001-CheckForPoliciesWhileStreaming.js
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,
};
1 change: 0 additions & 1 deletion lib/package/Endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ function Endpoint(element, bundle, fname, bundletype) {
this.httpProxyConnection = null;
this.httpTargetConnection = null;
this.report = {
//filePath: path.join(bundle.sourcePath, path.basename(fname)),
filePath: lintUtil.effectivePath(bundle, fname),
errorCount: 0,
warningCount: 0,
Expand Down
58 changes: 29 additions & 29 deletions lib/package/HTTPProxyConnection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 Google LLC
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.
Expand All @@ -21,23 +21,23 @@ function HTTPProxyConnection(element, parent) {
this.element = element;
}

HTTPProxyConnection.prototype.getName = function() {
HTTPProxyConnection.prototype.getName = function () {
if (!this.name) {
var attr = xpath.select("//@name", this.element);
this.name = (attr[0] && attr[0].value) || "";
}
return this.name;
};

HTTPProxyConnection.prototype.getMessages = function() {
HTTPProxyConnection.prototype.getMessages = function () {
return this.parent.getMessages();
};

HTTPProxyConnection.prototype.getLines = function(start, stop) {
HTTPProxyConnection.prototype.getLines = function (start, stop) {
return this.parent.getLines(start, stop);
};

HTTPProxyConnection.prototype.getSource = function() {
HTTPProxyConnection.prototype.getSource = function () {
if (!this.source) {
var start = this.element.lineNumber - 1,
stop = this.element.nextSibling.lineNumber - 1;
Expand All @@ -46,57 +46,57 @@ HTTPProxyConnection.prototype.getSource = function() {
return this.source;
};

HTTPProxyConnection.prototype.getType = function() {
HTTPProxyConnection.prototype.getType = function () {
return this.element.tagName;
};

HTTPProxyConnection.prototype.getBasePath = function() {
HTTPProxyConnection.prototype.getBasePath = function () {
if (!this.basePath) {
//find the preflow tag
var doc = xpath.select("./BasePath", this.element);
if (doc && doc[0]) {
this.basePath =
(doc && doc[0] && doc[0].childNodes[0].nodeValue) || "";
this.basePath = (doc && doc[0] && doc[0].childNodes[0].nodeValue) || "";
}
}
return this.basePath;
};



HTTPProxyConnection.prototype.getElement = function() {
HTTPProxyConnection.prototype.getElement = function () {
return this.element;
};

HTTPProxyConnection.prototype.getParent = function() {
HTTPProxyConnection.prototype.getParent = function () {
return this.parent;
};

HTTPProxyConnection.prototype.addMessage = function(msg) {
HTTPProxyConnection.prototype.addMessage = function (msg) {
if (!msg.hasOwnProperty("entity")) {
msg.entity = this;
}
this.parent.addMessage(msg);
};

HTTPProxyConnection.prototype.getProperties = function() {
var props = new Map();
if (!this.properties) {
var propsNodeList = xpath.select("./Properties", this.element)[0].childNodes;
Array.from(propsNodeList).forEach(function(prop) {
if (prop.childNodes){
props[prop.attributes[0].nodeValue]=prop.childNodes[0].nodeValue;
HTTPProxyConnection.prototype.getProperties = function () {
if (this.properties == undefined) {
let props = {};
this.properties = props;
let propsNodeList = xpath.select("./Properties", this.element);
if (propsNodeList && propsNodeList[0]) {
Array.from(propsNodeList[0].childNodes).forEach((prop) => {
if (prop.childNodes && prop.attributes[0] && prop.childNodes[0]) {
props[prop.attributes[0].nodeValue] = prop.childNodes[0].nodeValue;
}
});
});
}
}
return props;
return this.properties;
};

HTTPProxyConnection.prototype.summarize = function() {
var summary = {};
summary.name = this.getName();
summary.basePath = this.getBasePath();
return summary;
HTTPProxyConnection.prototype.summarize = function () {
// not sure this is ever used
return {
name: this.getName(),
basePath: this.getBasePath(),
};
};

//Public
Expand Down
6 changes: 3 additions & 3 deletions lib/package/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ function Policy(realpath, fname, bundle, doc) {
fixableWarningCount: 0,
messages: [],
};
this.name = null;
}

Policy.prototype.getName = function () {
if (!this.name) {
let attr = xpath.select("//@name", this.getElement());
this.name = (attr[0] && attr[0].value) || "";
if (this.name == null) {
this.name = xpath.select("string(/*/@name)", this.getElement());
}
return this.name;
};
Expand Down
Loading

0 comments on commit 1d602c4

Please sign in to comment.