Skip to content

Commit

Permalink
chore: add colors to log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
danteay committed Oct 31, 2023
1 parent 88b5919 commit 4ce1ca4
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 61 deletions.
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import core from "@actions/core";
import Action from "./src/action/Action.js";

const getAction = () => {
const exclude = core.getInput("exclude").split(",");

return new Action(
core.getInput("aws_access_key_id"),
core.getInput("aws_secret_access_key"),
core.getInput("aws_region"),
core.getInput("secret_name"),
core.getInput("json_file_path"),
exclude,
core.getInput("exclude"),
);
};

Expand Down
50 changes: 39 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"lint": "npx eslint --fix .",
"format": "npx prettier --write .",
"test": "mocha ./tests/**/*.test.js",
"test": "FORCE_COLOR=0 mocha ./tests/**/*.test.js",
"pre-commit": "pre-commit install --hook-type commit-msg"
},
"repository": {
Expand Down Expand Up @@ -41,6 +41,7 @@
"dependencies": {
"@actions/core": "^1.10.1",
"@aws-sdk/client-secrets-manager": "^3.425.0",
"chalk": "^5.3.0",
"fs": "^0.0.1-security",
"lodash": "^4.17.21"
}
Expand Down
25 changes: 5 additions & 20 deletions src/action/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import lodash from "lodash";
import SecretsManager from "../secrets-manager/SecretsManager.js";
import ChangeSet from "./ChangeSet.js";

const skipPatterns = ["^_"];
const defaultSkipPattern = "^_";

/**
* Action is a class representing an action to synchronize secrets with AWS Secrets Manager.
Expand All @@ -19,23 +19,16 @@ export default class Action {
* @param {string} region - The AWS region.
* @param {string} secretName - The name of the secret in AWS Secrets Manager.
* @param {string} jsonFile - The path to the JSON file containing the new secret values.
* @param {Array<string>} skipPatterns - A list of regular expressions that eval keys of the json file and if match,
* @param {string} skipPattern - A list of regular expressions that eval keys of the json file and if match,
* that key should be omitted
*
* @throws {Error} Throws an error if any required parameter is missing or if the JSON file doesn't exist.
*/
constructor(
keyId,
secretKey,
region,
secretName,
jsonFile,
skipPatterns = [],
) {
constructor(keyId, secretKey, region, secretName, jsonFile, skipPattern) {
this.#validateData(keyId, secretKey, region, secretName, jsonFile);

this.jsonFile = jsonFile;
this.skipPatterns = this.#getSkipPatterns(skipPatterns);
this.skipPattern = skipPattern || defaultSkipPattern;

this.smClient = new SecretsManager(keyId, secretKey, region, secretName);
}
Expand All @@ -53,7 +46,7 @@ export default class Action {
this.smClient,
newSecretData,
existingSecretData,
this.skipPatterns,
this.skipPattern,
);
}

Expand Down Expand Up @@ -94,12 +87,4 @@ export default class Action {
throw new Error(`JSON file does not exist at path: ${jsonFile}`);
}
}

#getSkipPatterns(skip) {
if (lodash.isArray(skip) && skip.length > 0) {
return skipPatterns.concat(skip);
}

return skipPatterns;
}
}
89 changes: 65 additions & 24 deletions src/action/ChangeSet.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import core from "@actions/core";
import lodash from "lodash";
import chalk from "chalk";

import SecretsManager from "../secrets-manager/SecretsManager.js";

const logPrefix = chalk.cyan("SecretKey");
const skipTag = chalk.yellow("[SKIP]");
const addedTag = chalk.green("[ADDED]");
const changedTag = chalk.magenta("[CHANGED]");
const removedTag = chalk.red("[REMOVED]");

/**
* ChangeSet is a class that represents a set of changes to be applied to secrets in AWS Secrets Manager.
* It tracks the changes, provides descriptions, and allows applying those changes.
Expand All @@ -27,24 +34,24 @@ export default class ChangeSet {
#smClient;

/**
* A list of regular expressions to exclude keys
* @type {Array<string>}
* A regular expression to exclude keys
* @type {string}
*/
#skipPatterns;
#skipPattern;

/**
* Creates a new ChangeSet instance.
*
* @param {SecretsManager} smClient - An instance of the SecretsManager class for interacting with AWS Secrets Manager.
* @param {Object} newValues - The new set of values to be applied to the secrets manager.
* @param {Object} existingValues - The existing set of values to be replaced by the new ones.
* @param {Array<string>} skipPatterns - A list of regular expressions to skip keys.
* @param {string} skipPattern - A regular expression to skip keys.
*/
constructor(smClient, newValues, existingValues, skipPatterns) {
constructor(smClient, newValues, existingValues, skipPattern) {
this.#changeDesc = [];
this.#updatedValues = { ...existingValues };
this.#smClient = smClient;
this.#skipPatterns = skipPatterns || [];
this.#skipPattern = skipPattern || "";

this.#eval(newValues, existingValues);
}
Expand Down Expand Up @@ -77,7 +84,7 @@ export default class ChangeSet {
// Check for changes and update the secret (or preview changes)
for (const key in newValues) {
if (this.#shouldSkip(key)) {
this.#changeDesc.push(`SecretKey: [SKIP] '${key}'`);
this.#skipDesc(key);
continue;
}

Expand All @@ -88,24 +95,19 @@ export default class ChangeSet {
this.#updatedValues[key] = newValues[key];

if (existingValues[key] === undefined) {
this.#changeDesc.push(
`SecretKey: [ADDED] '${key}': '${newValues[key]}'`,
);

this.#addedDesc(key, newValues[key]);
continue;
}

this.#changeDesc.push(
`SecretKey: [CHANGE] '${key}': '${existingValues[key]}' => '${newValues[key]}'`,
);
this.#changedDesc(key, existingValues[key], newValues[key]);
}

for (const key in existingValues) {
if (newValues.hasOwnProperty(key)) {
continue;
}

this.#changeDesc.push(`SecretKey: [REMOVED] '${key}'`);
this.#removedDesc(key);

delete this.#updatedValues[key];
}
Expand All @@ -119,19 +121,58 @@ export default class ChangeSet {
* @returns {boolean}
*/
#shouldSkip(key) {
for (let regexp of this.#skipPatterns) {
if (lodash.isEmpty(regexp)) {
continue;
}
if (lodash.isEmpty(this.#skipPattern)) {
return false;
}

let exp = new RegExp(regexp);
let exp = new RegExp(this.#skipPattern);

if (exp.test(key)) {
core.debug(`Skipping ${key} with regexp '${regexp}'`);
return true;
}
if (exp.test(key)) {
core.debug(`Skipping ${key} with regexp '${this.#skipPattern}'`);
return true;
}

return false;
}

/**
* Create a skip message description and append to changeDesc array
*
* @param {string} key Name of the key
*/
#skipDesc(key) {
this.#changeDesc.push(`${logPrefix}: ${skipTag} '${key}'`);
}

/**
* Create a skip message description and append to changeDesc array
*
* @param {string} key Name of the key
*/
#removedDesc(key) {
this.#changeDesc.push(`${logPrefix}: ${removedTag} '${key}'`);
}

/**
* Create an added message description and append to changeDesc array
*
* @param {string} key Name of the key
* @param {string} val Value that is set to the key
*/
#addedDesc(key, val) {
this.#changeDesc.push(`${logPrefix}: ${addedTag} '${key}': '${val}'`);
}

/**
* Create a changed message description and append to changeDesc array
*
* @param {string} key Name of the key
* @param {string} oldVal Las secret value before sync
* @param {string} newVal New secret value after sync
*/
#changedDesc(key, oldVal, newVal) {
this.#changeDesc.push(
`${logPrefix}: ${changedTag} '${key}': '${oldVal}' => '${newVal}'`,
);
}
}
4 changes: 2 additions & 2 deletions tests/action/ChangeSet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ describe("ChangeSet", () => {
const descriptions = changeSet.changeDesc();

expect(descriptions).to.deep.equal([
"SecretKey: [CHANGE] 'key1': 'value1' => 'new-value1'",
"SecretKey: [CHANGE] 'key2': 'value2' => 'new-value2'",
"SecretKey: [CHANGED] 'key1': 'value1' => 'new-value1'",
"SecretKey: [CHANGED] 'key2': 'value2' => 'new-value2'",
]);
});

Expand Down

0 comments on commit 4ce1ca4

Please sign in to comment.