-
Notifications
You must be signed in to change notification settings - Fork 692
Add secrets adapter for AWS SSM Parameter Store #1791
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
Open
davafons
wants to merge
1
commit into
basecamp:main
Choose a base branch
from
davafons:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+244
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,50 @@ | ||
| class Kamal::Secrets::Adapters::AwsSsmParameterStore < Kamal::Secrets::Adapters::Base | ||
| MAX_PARAMETERS_PER_REQUEST = 10 | ||
|
|
||
| def requires_account? | ||
| false | ||
| end | ||
|
|
||
| private | ||
| def login(_account) | ||
| nil | ||
| end | ||
|
|
||
| def fetch_secrets(secrets, from:, account: nil, session:) | ||
| {}.tap do |results| | ||
| prefixed_secrets(secrets, from: from).each_slice(MAX_PARAMETERS_PER_REQUEST) do |batch| | ||
| get_from_parameter_store(batch, account: account).each do |secret| | ||
| results[secret["Name"]] = secret["Value"] | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def get_from_parameter_store(secrets, account: nil) | ||
| args = [ "aws", "ssm", "get-parameters", "--names" ] + secrets.map(&:shellescape) | ||
| # We have to pass --with-decryption. Otherwise, we would get the raw encrypted value for secrets with type SecureString (AWS KMS encrypted secrets). | ||
| args += [ "--with-decryption" ] | ||
| args += [ "--profile", account.shellescape ] if account | ||
| args += [ "--output", "json" ] | ||
| cmd = args.join(" ") | ||
|
|
||
| `#{cmd}`.tap do |response| | ||
| raise RuntimeError, "Could not read from AWS SSM Parameter Store" unless $?.success? | ||
|
|
||
| response = JSON.parse(response) | ||
|
|
||
| return response["Parameters"] unless response["InvalidParameters"].present? | ||
|
|
||
| raise RuntimeError, response["InvalidParameters"].map { |name| "#{name}: SSM Parameter Store can't find the specified secret." }.join(" ") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sadly, |
||
| end | ||
| end | ||
|
|
||
| def check_dependencies! | ||
| raise RuntimeError, "AWS CLI is not installed" unless cli_installed? | ||
| end | ||
|
|
||
| def cli_installed? | ||
| `aws --version 2> /dev/null` | ||
| $?.success? | ||
| end | ||
| end | ||
This file contains hidden or 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,194 @@ | ||
| require "test_helper" | ||
|
|
||
| class AwsSsmParameterStoreAdapterTest < SecretAdapterTestCase | ||
| test "fails when errors are present" do | ||
| stub_ticks.with("aws --version 2> /dev/null") | ||
| stub_ticks | ||
| .with("aws ssm get-parameters --names unknown1 unknown2 --with-decryption --profile default --output json") | ||
| .returns(<<~JSON) | ||
| { | ||
| "Parameters": [], | ||
| "InvalidParameters": [ | ||
| "unknown1", | ||
| "unknown2" | ||
| ] | ||
| } | ||
| JSON | ||
|
|
||
| error = assert_raises RuntimeError do | ||
| JSON.parse(run_command("fetch", "unknown1", "unknown2")) | ||
| end | ||
|
|
||
| assert_equal [ | ||
| "unknown1: SSM Parameter Store can't find the specified secret.", | ||
| "unknown2: SSM Parameter Store can't find the specified secret." | ||
| ].join(" "), error.message | ||
| end | ||
|
|
||
| test "fetch" do | ||
| stub_ticks.with("aws --version 2> /dev/null") | ||
| stub_ticks | ||
| .with("aws ssm get-parameters --names secret/KEY1 secret/KEY2 secret2/KEY3 --with-decryption --profile default --output json") | ||
| .returns(<<~JSON) | ||
| { | ||
| "Parameters": [ | ||
| { | ||
| "Name": "secret/KEY1", | ||
| "Value": "VALUE1" | ||
| }, | ||
| { | ||
| "Name": "secret/KEY2", | ||
| "Value": "VALUE2" | ||
| }, | ||
| { | ||
| "Name": "secret2/KEY3", | ||
| "Value": "VALUE3" | ||
| } | ||
| ], | ||
| "InvalidParameters": [] | ||
| } | ||
| JSON | ||
|
|
||
| json = JSON.parse(run_command("fetch", "secret/KEY1", "secret/KEY2", "secret2/KEY3")) | ||
|
|
||
| expected_json = { | ||
| "secret/KEY1"=>"VALUE1", | ||
| "secret/KEY2"=>"VALUE2", | ||
| "secret2/KEY3"=>"VALUE3" | ||
| } | ||
|
|
||
| assert_equal expected_json, json | ||
| end | ||
|
|
||
| test "fetch batches requests to stay within the API limit" do | ||
| stub_ticks.with("aws --version 2> /dev/null") | ||
| stub_ticks | ||
| .with("aws ssm get-parameters --names secret1 secret2 secret3 secret4 secret5 secret6 secret7 secret8 secret9 secret10 --with-decryption --profile default --output json") | ||
| .returns(JSON.generate({ | ||
| "Parameters" => (1..10).map { |i| { "Name" => "secret#{i}", "Value" => "VALUE#{i}" } }, | ||
| "InvalidParameters" => [] | ||
| })) | ||
| stub_ticks | ||
| .with("aws ssm get-parameters --names secret11 --with-decryption --profile default --output json") | ||
| .returns(JSON.generate({ | ||
| "Parameters" => [ { "Name" => "secret11", "Value" => "VALUE11" } ], | ||
| "InvalidParameters" => [] | ||
| })) | ||
|
|
||
| json = JSON.parse(run_command("fetch", *(1..11).map { |i| "secret#{i}" })) | ||
|
|
||
| expected_json = (1..11).map { |i| [ "secret#{i}", "VALUE#{i}" ] }.to_h | ||
|
|
||
| assert_equal expected_json, json | ||
| end | ||
|
|
||
| test "fetch with string value" do | ||
| stub_ticks.with("aws --version 2> /dev/null") | ||
| stub_ticks | ||
| .with("aws ssm get-parameters --names secret secret2/KEY1 --with-decryption --profile default --output json") | ||
| .returns(<<~JSON) | ||
| { | ||
| "Parameters": [ | ||
| { | ||
| "Name": "secret", | ||
| "Value": "a-string-secret" | ||
| }, | ||
| { | ||
| "Name": "secret2/KEY1", | ||
| "Value": "{\\"KEY2\\":\\"VALUE2\\"}" | ||
| } | ||
| ], | ||
| "InvalidParameters": [] | ||
| } | ||
| JSON | ||
|
|
||
| json = JSON.parse(run_command("fetch", "secret", "secret2/KEY1")) | ||
|
|
||
| expected_json = { | ||
| "secret"=>"a-string-secret", | ||
| "secret2/KEY1"=>"{\"KEY2\":\"VALUE2\"}" | ||
| } | ||
|
|
||
| assert_equal expected_json, json | ||
| end | ||
|
|
||
| test "fetch with secret names" do | ||
| stub_ticks.with("aws --version 2> /dev/null") | ||
| stub_ticks | ||
| .with("aws ssm get-parameters --names secret/KEY1 secret/KEY2 --with-decryption --profile default --output json") | ||
| .returns(<<~JSON) | ||
| { | ||
| "Parameters": [ | ||
| { | ||
| "Name": "secret/KEY1", | ||
| "Value": "VALUE1" | ||
| }, | ||
| { | ||
| "Name": "secret/KEY2", | ||
| "Value": "VALUE2" | ||
| } | ||
| ], | ||
| "InvalidParameters": [] | ||
| } | ||
| JSON | ||
|
|
||
| json = JSON.parse(run_command("fetch", "--from", "secret", "KEY1", "KEY2")) | ||
|
|
||
| expected_json = { | ||
| "secret/KEY1"=>"VALUE1", | ||
| "secret/KEY2"=>"VALUE2" | ||
| } | ||
|
|
||
| assert_equal expected_json, json | ||
| end | ||
|
|
||
| test "fetch without CLI installed" do | ||
| stub_ticks_with("aws --version 2> /dev/null", succeed: false) | ||
|
|
||
| error = assert_raises RuntimeError do | ||
| JSON.parse(run_command("fetch", "SECRET1")) | ||
| end | ||
| assert_equal "AWS CLI is not installed", error.message | ||
| end | ||
|
|
||
| test "fetch without account option omits --profile" do | ||
| stub_ticks.with("aws --version 2> /dev/null") | ||
| stub_ticks | ||
| .with("aws ssm get-parameters --names secret/KEY1 secret/KEY2 --with-decryption --output json") | ||
| .returns(<<~JSON) | ||
| { | ||
| "Parameters": [ | ||
| { | ||
| "Name": "secret/KEY1", | ||
| "Value": "VALUE1" | ||
| }, | ||
| { | ||
| "Name": "secret/KEY2", | ||
| "Value": "VALUE2" | ||
| } | ||
| ], | ||
| "InvalidParameters": [] | ||
| } | ||
| JSON | ||
|
|
||
| json = JSON.parse(run_command("fetch", "--from", "secret", "KEY1", "KEY2", account: nil)) | ||
|
|
||
| expected_json = { | ||
| "secret/KEY1"=>"VALUE1", | ||
| "secret/KEY2"=>"VALUE2" | ||
| } | ||
|
|
||
| assert_equal expected_json, json | ||
| end | ||
|
|
||
| private | ||
| def run_command(*command, account: "default") | ||
| stdouted do | ||
| args = [ *command, | ||
| "-c", "test/fixtures/deploy_with_accessories.yml", | ||
| "--adapter", "aws_ssm_parameter_store" ] | ||
| args += [ "--account", account ] if account | ||
| Kamal::Cli::Secrets.start(args) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.