-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cross): #889 secrets for aws from gitlab
- Add builtin for assuming aws sessions using GitLab OIDC - Add documentation
- Loading branch information
Showing
6 changed files
with
170 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
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,23 @@ | ||
{ | ||
__nixpkgs__, | ||
makeTemplate, | ||
toDerivationName, | ||
... | ||
}: { | ||
duration, | ||
name, | ||
roleArn, | ||
}: | ||
makeTemplate { | ||
replace = { | ||
__argDuration__ = duration; | ||
__argName__ = toDerivationName name; | ||
__argRoleArn__ = roleArn; | ||
}; | ||
name = "make-secret-for-aws-from-gitlab-for-${name}"; | ||
searchPaths.bin = [ | ||
__nixpkgs__.awscli | ||
__nixpkgs__.jq | ||
]; | ||
template = ./template.sh; | ||
} |
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,40 @@ | ||
# shellcheck shell=bash | ||
|
||
function _get_credential { | ||
local credential="${1}" | ||
local session="${2}" | ||
|
||
echo "${session}" | jq -rec ".Credentials.${credential}" | ||
} | ||
|
||
function login { | ||
local args=( | ||
--role-arn "${1}" | ||
--role-session-name "gitlab-${CI_PROJECT_ID}-${CI_PIPELINE_ID}-${CI_JOB_ID}" | ||
--web-identity-token "${CI_JOB_JWT_V2}" | ||
--duration-seconds "${2}" | ||
) | ||
local session | ||
export AWS_ACCESS_KEY_ID | ||
export AWS_SECRET_ACCESS_KEY | ||
export AWS_SESSION_TOKEN | ||
|
||
: \ | ||
&& session="$(aws sts assume-role-with-web-identity "${args[@]}")" \ | ||
&& AWS_ACCESS_KEY_ID="$(_get_credential "AccessKeyId" "${session}")" \ | ||
&& AWS_SECRET_ACCESS_KEY="$(_get_credential "SecretAccessKey" "${session}")" \ | ||
&& AWS_SESSION_TOKEN="$(_get_credential "SessionToken" "${session}")" | ||
} | ||
|
||
function main { | ||
: \ | ||
&& info "Making secrets for aws from gitlab for __argName__:" \ | ||
&& if test -n "${CI_JOB_JWT_V2:-}"; then | ||
info "Logging in as '__argName__' using GitLab OIDC." \ | ||
&& login "__argRoleArn__" "__argDuration__" | ||
else | ||
warn "Looks like this job is not running on GitLab CI. Skipping." | ||
fi | ||
} | ||
|
||
main "${@}" |
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
42 changes: 42 additions & 0 deletions
42
src/evaluator/modules/secrets-for-aws-from-gitlab/default.nix
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,42 @@ | ||
{ | ||
__toModuleOutputs__, | ||
makeSecretForAwsFromGitlab, | ||
... | ||
}: { | ||
config, | ||
lib, | ||
... | ||
}: let | ||
type = lib.types.submodule (_: { | ||
options = { | ||
roleArn = lib.mkOption { | ||
type = lib.types.str; | ||
}; | ||
duration = lib.mkOption { | ||
default = 3600; | ||
type = lib.types.ints.positive; | ||
}; | ||
}; | ||
}); | ||
output = name: { | ||
roleArn, | ||
duration, | ||
}: { | ||
name = "/secretsForAwsFromGitlab/${name}"; | ||
value = makeSecretForAwsFromGitlab { | ||
inherit duration; | ||
inherit name; | ||
inherit roleArn; | ||
}; | ||
}; | ||
in { | ||
options = { | ||
secretsForAwsFromGitlab = lib.mkOption { | ||
default = {}; | ||
type = lib.types.attrsOf type; | ||
}; | ||
}; | ||
config = { | ||
outputs = __toModuleOutputs__ output config.secretsForAwsFromGitlab; | ||
}; | ||
} |