From 3d502ab12db7bdfe4aae44075a97651561f14ada Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Wed, 4 Sep 2024 12:40:27 +0100 Subject: [PATCH] Add test adapter and interpolate secrets in integration tests --- lib/kamal/cli/secrets.rb | 2 +- lib/kamal/secrets/adapters/test.rb | 10 ++++++++++ test/integration/docker/deployer/app/.kamal/secrets | 3 +++ test/integration/docker/deployer/app/config/deploy.yml | 2 ++ test/integration/main_test.rb | 4 +++- 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 lib/kamal/secrets/adapters/test.rb diff --git a/lib/kamal/cli/secrets.rb b/lib/kamal/cli/secrets.rb index e5c3b7d5..572476f9 100644 --- a/lib/kamal/cli/secrets.rb +++ b/lib/kamal/cli/secrets.rb @@ -20,7 +20,7 @@ class Kamal::Cli::Secrets < Kamal::Cli::Base raise "Could not find secret #{name}" if value.nil? - puts JSON.parse(secrets).fetch(name) + puts value rescue => e handle_error(e) end diff --git a/lib/kamal/secrets/adapters/test.rb b/lib/kamal/secrets/adapters/test.rb new file mode 100644 index 00000000..8750a2e2 --- /dev/null +++ b/lib/kamal/secrets/adapters/test.rb @@ -0,0 +1,10 @@ +class Kamal::Secrets::Adapters::Test < Kamal::Secrets::Adapters::Base + private + def login(account) + true + end + + def fetch_from_vault(secrets, account:, session:) + secrets.to_h { |secret| [ secret, secret.reverse ] } + end +end diff --git a/test/integration/docker/deployer/app/.kamal/secrets b/test/integration/docker/deployer/app/.kamal/secrets index ea15ab06..454ba88f 100644 --- a/test/integration/docker/deployer/app/.kamal/secrets +++ b/test/integration/docker/deployer/app/.kamal/secrets @@ -1,2 +1,5 @@ SECRET_TOKEN='1234 with "中文"' SECRET_TAG='TAGME' +SECRETS=$(kamal secrets fetch --adapter test --account test INTERPOLATED_SECRET1 INTERPOLATED_SECRET2) +INTERPOLATED_SECRET1=$(kamal secrets extract INTERPOLATED_SECRET1 ${SECRETS}) +INTERPOLATED_SECRET2=$(kamal secrets extract INTERPOLATED_SECRET2 ${SECRETS}) diff --git a/test/integration/docker/deployer/app/config/deploy.yml b/test/integration/docker/deployer/app/config/deploy.yml index 887de825..ae67ea3e 100644 --- a/test/integration/docker/deployer/app/config/deploy.yml +++ b/test/integration/docker/deployer/app/config/deploy.yml @@ -10,6 +10,8 @@ env: HOST_TOKEN: "${HOST_TOKEN}" secret: - SECRET_TOKEN + - INTERPOLATED_SECRET1 + - INTERPOLATED_SECRET2 tags: tag1: CLEAR_TAG: tagged diff --git a/test/integration/main_test.rb b/test/integration/main_test.rb index 7ed6ee8f..4967002d 100644 --- a/test/integration/main_test.rb +++ b/test/integration/main_test.rb @@ -102,7 +102,7 @@ class MainTest < IntegrationTest end private - def assert_envs(version:) + def assert_envs(version:) assert_env :CLEAR_TOKEN, "4321", version: version, vm: :vm1 assert_env :HOST_TOKEN, "abcd", version: version, vm: :vm1 assert_env :SECRET_TOKEN, "1234 with \"中文\"", version: version, vm: :vm1 @@ -110,6 +110,8 @@ class MainTest < IntegrationTest assert_no_env :SECRET_TAG, version: version, vm: :vm1 assert_env :CLEAR_TAG, "tagged", version: version, vm: :vm2 assert_env :SECRET_TAG, "TAGME", version: version, vm: :vm2 + assert_env :INTERPOLATED_SECRET1, "1TERCES_DETALOPRETNI", version: version, vm: :vm2 + assert_env :INTERPOLATED_SECRET2, "2TERCES_DETALOPRETNI", version: version, vm: :vm2 end def assert_env(key, value, vm:, version:)