Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add filters #37

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ inputs:
description: 'If true, disable annotation warnings in the GitHub Actions output.'
required: false
default: 'false'
filters:
description: 'two strings: tag key and tag value to filter on'
required: false
runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
65 changes: 37 additions & 28 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36014,6 +36014,7 @@ var Inputs;
Inputs["SECRETS"] = "secrets";
Inputs["PARSE_JSON"] = "parse-json";
Inputs["DISABLE_WARNINGS"] = "disable-warnings";
Inputs["FILTERS"] = "filters";
})(Inputs || (Inputs = {}));

// EXTERNAL MODULE: ./node_modules/aws-sdk/lib/aws.js
Expand Down Expand Up @@ -36112,33 +36113,40 @@ const getSecretValue = (secretsManagerClient, secretName) => {
core.debug(`Fetching '${secretName}'`);
return secretsManagerClient.getSecretValue({ SecretId: secretName }).promise();
};
const listSecretsPaginated = (secretsManagerClient, nextToken) => secretsManagerClient.listSecrets({ NextToken: nextToken }).promise();
const listSecrets = (secretsManagerClient) => {
const listSecretsPaginated = (secretsManagerClient, nextToken, filters) => {
return secretsManagerClient.listSecrets({ NextToken: nextToken,
Filters: [{
Key: 'tag-key',
Values: [filters[0]]
}, { Key: 'tag-value', Values: [filters[1]] },
]
}).promise();
};
const iterateSecrets = (resolver, secretsManagerClient, allSecretNames, filters, nextToken = null) => {
listSecretsPaginated(secretsManagerClient, nextToken, filters)
.then(res => {
// get all non-deleted secret names
res['SecretList'].forEach(secret => {
if (!('DeletedDate' in secret)) {
allSecretNames.push(secret['Name']);
}
});
// fetch nextToken if it exists, reset to null otherwise
if ('NextToken' in res) {
iterateSecrets(resolver, secretsManagerClient, allSecretNames, filters, res['NextToken']);
}
else {
resolver(allSecretNames);
}
})
.catch(err => {
throw err;
});
};
const listSecrets = (secretsManagerClient, filters = []) => {
return new Promise((resolve, reject) => {
let nextToken = null;
const allSecretNames = [];
do {
listSecretsPaginated(secretsManagerClient, nextToken)
.then(res => {
// fetch nextToken if it exists, reset to null otherwise
if ('NextToken' in res) {
nextToken = res['NextToken'];
}
else {
nextToken = null;
}
// get all non-deleted secret names
res['SecretList'].forEach(secret => {
if (!('DeletedDate' in secret)) {
allSecretNames.push(secret['Name']);
}
});
resolve(allSecretNames);
})
.catch(err => {
reject(err);
});
} while (nextToken);
iterateSecrets(resolve, secretsManagerClient, allSecretNames, filters);
});
};
const getSecretValueMap = (secretsManagerClient, secretName, shouldParseJSON = false) => {
Expand Down Expand Up @@ -36207,12 +36215,12 @@ const getSecretValueMap = (secretsManagerClient, secretName, shouldParseJSON = f
});
});
};
const getSecretNamesToFetch = (secretsManagerClient, inputSecretNames) => {
const getSecretNamesToFetch = (secretsManagerClient, inputSecretNames, filters = []) => {
return new Promise((resolve, reject) => {
// list secrets, filter against wildcards and fetch filtered secrets
// else, fetch specified secrets directly
const secretNames = [];
listSecrets(secretsManagerClient)
listSecrets(secretsManagerClient, filters)
.then(secrets => {
inputSecretNames.forEach(inputSecretName => {
secretNames.push(...filterBy(secrets, inputSecretName));
Expand Down Expand Up @@ -36247,11 +36255,12 @@ const inputSecretNames = [...new Set(core.getMultilineInput(Inputs.SECRETS))];
// Check if any secret name contains a wildcard '*'
const hasWildcard = inputSecretNames.some(secretName => secretName.includes('*'));
const shouldParseJSON = core.getBooleanInput(Inputs.PARSE_JSON);
const filters = [...new Set(core.getMultilineInput(Inputs.FILTERS))];
const AWSConfig = {};
const secretsManagerClient = getSecretsManagerClient(AWSConfig);
if (hasWildcard) {
core.debug('Found wildcard secret names');
getSecretNamesToFetch(secretsManagerClient, inputSecretNames)
getSecretNamesToFetch(secretsManagerClient, inputSecretNames, filters)
.then(secretNamesToFetch => {
fetchAndInject(secretsManagerClient, secretNamesToFetch, shouldParseJSON);
})
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

65 changes: 37 additions & 28 deletions src/awsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,44 @@ const getSecretValue = (secretsManagerClient: SecretsManager, secretName: string
return secretsManagerClient.getSecretValue({ SecretId: secretName }).promise()
}

const listSecretsPaginated = (secretsManagerClient, nextToken) =>
secretsManagerClient.listSecrets({ NextToken: nextToken }).promise()
const listSecretsPaginated = (secretsManagerClient, nextToken, filters) => {
return secretsManagerClient.listSecrets({ NextToken: nextToken,
Filters: [{
Key: 'tag-key',
Values: [filters[0]]
}, {Key: 'tag-value', Values: [filters[1]]},
]
}).promise()
}


const iterateSecrets = (resolver, secretsManagerClient: SecretsManager, allSecretNames: string[],
filters: string[], nextToken = null) => {
listSecretsPaginated(secretsManagerClient, nextToken, filters)
.then(res => {
// get all non-deleted secret names
res['SecretList'].forEach(secret => {
if (!('DeletedDate' in secret)) {
allSecretNames.push(secret['Name'])
}
})

// fetch nextToken if it exists, reset to null otherwise
if ('NextToken' in res) {
iterateSecrets(resolver, secretsManagerClient, allSecretNames, filters, res['NextToken'])
} else {
resolver(allSecretNames)
}
})
.catch(err => {
throw err;
})
}

const listSecrets = (secretsManagerClient: SecretsManager): Promise<Array<string>> => {
const listSecrets = (secretsManagerClient: SecretsManager, filters: string[] = []): Promise<Array<string>> => {
return new Promise<Array<string>>((resolve, reject) => {
let nextToken: string = null
const allSecretNames: string[] = []
do {
listSecretsPaginated(secretsManagerClient, nextToken)
.then(res => {
// fetch nextToken if it exists, reset to null otherwise
if ('NextToken' in res) {
nextToken = res['NextToken']
} else {
nextToken = null
}
// get all non-deleted secret names
res['SecretList'].forEach(secret => {
if (!('DeletedDate' in secret)) {
allSecretNames.push(secret['Name'])
}
})
resolve(allSecretNames)
})
.catch(err => {
reject(err)
})
}
while (nextToken)
iterateSecrets(resolve, secretsManagerClient, allSecretNames, filters)
})
}

Expand Down Expand Up @@ -112,12 +121,12 @@ const getSecretValueMap = (secretsManagerClient: SecretsManager, secretName: str
}

const getSecretNamesToFetch =
(secretsManagerClient: SecretsManager, inputSecretNames: string[]): Promise<Array<string>> => {
(secretsManagerClient: SecretsManager, inputSecretNames: string[], filters: string[] = []): Promise<Array<string>> => {
return new Promise<Array<string>>((resolve, reject) => {
// list secrets, filter against wildcards and fetch filtered secrets
// else, fetch specified secrets directly
const secretNames: string[] = []
listSecrets(secretsManagerClient)
listSecrets(secretsManagerClient, filters)
.then(secrets => {
inputSecretNames.forEach(inputSecretName => {
secretNames.push(...filterBy(secrets, inputSecretName))
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum Inputs {
SECRETS = 'secrets',
PARSE_JSON = 'parse-json',
DISABLE_WARNINGS = 'disable-warnings'
DISABLE_WARNINGS = 'disable-warnings',
FILTERS = 'filters'
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ const hasWildcard: boolean = inputSecretNames.some(secretName => secretName.incl

const shouldParseJSON = core.getBooleanInput(Inputs.PARSE_JSON)

const filters: string[] = [...new Set(core.getMultilineInput(Inputs.FILTERS))]

const AWSConfig = {}

const secretsManagerClient = getSecretsManagerClient(AWSConfig)

if (hasWildcard) {
core.debug('Found wildcard secret names')
getSecretNamesToFetch(secretsManagerClient, inputSecretNames)
getSecretNamesToFetch(secretsManagerClient, inputSecretNames, filters)
.then(secretNamesToFetch => {
fetchAndInject(secretsManagerClient, secretNamesToFetch, shouldParseJSON)
})
Expand Down