-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1099 from mrbongiolo/feat-secrets-add-doppler-ada…
…pter feat(secrets): add Doppler adapter
- Loading branch information
Showing
6 changed files
with
273 additions
and
4 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,53 @@ | ||
class Kamal::Secrets::Adapters::Doppler < Kamal::Secrets::Adapters::Base | ||
def requires_account? | ||
false | ||
end | ||
|
||
private | ||
def login(*) | ||
unless loggedin? | ||
`doppler login -y` | ||
raise RuntimeError, "Failed to login to Doppler" unless $?.success? | ||
end | ||
end | ||
|
||
def loggedin? | ||
`doppler me --json 2> /dev/null` | ||
$?.success? | ||
end | ||
|
||
def fetch_secrets(secrets, **) | ||
project_and_config_flags = "" | ||
unless service_token_set? | ||
project, config, _ = secrets.first.split("/") | ||
|
||
unless project && config | ||
raise RuntimeError, "Missing project or config from '--from=project/config' option" | ||
end | ||
|
||
project_and_config_flags = "-p #{project.shellescape} -c #{config.shellescape}" | ||
end | ||
|
||
secret_names = secrets.collect { |s| s.split("/").last } | ||
|
||
items = `doppler secrets get #{secret_names.map(&:shellescape).join(" ")} --json #{project_and_config_flags}` | ||
raise RuntimeError, "Could not read #{secrets} from Doppler" unless $?.success? | ||
|
||
items = JSON.parse(items) | ||
|
||
items.transform_values { |value| value["computed"] } | ||
end | ||
|
||
def service_token_set? | ||
ENV["DOPPLER_TOKEN"] && ENV["DOPPLER_TOKEN"][0, 5] == "dp.st" | ||
end | ||
|
||
def check_dependencies! | ||
raise RuntimeError, "Doppler CLI is not installed" unless cli_installed? | ||
end | ||
|
||
def cli_installed? | ||
`doppler --version 2> /dev/null` | ||
$?.success? | ||
end | ||
end |
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,5 @@ | ||
class Kamal::Secrets::Adapters::TestOptionalAccount < Kamal::Secrets::Adapters::Test | ||
def requires_account? | ||
false | ||
end | ||
end |
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,186 @@ | ||
require "test_helper" | ||
|
||
class DopplerAdapterTest < SecretAdapterTestCase | ||
setup do | ||
`true` # Ensure $? is 0 | ||
end | ||
|
||
test "fetch" do | ||
stub_ticks_with("doppler --version 2> /dev/null", succeed: true) | ||
stub_ticks.with("doppler me --json 2> /dev/null") | ||
|
||
stub_ticks | ||
.with("doppler secrets get SECRET1 FSECRET1 FSECRET2 --json -p my-project -c prd") | ||
.returns(<<~JSON) | ||
{ | ||
"SECRET1": { | ||
"computed":"secret1", | ||
"computedVisibility":"unmasked", | ||
"note":"" | ||
}, | ||
"FSECRET1": { | ||
"computed":"fsecret1", | ||
"computedVisibility":"unmasked", | ||
"note":"" | ||
}, | ||
"FSECRET2": { | ||
"computed":"fsecret2", | ||
"computedVisibility":"unmasked", | ||
"note":"" | ||
} | ||
} | ||
JSON | ||
|
||
json = JSON.parse( | ||
shellunescape run_command("fetch", "--from", "my-project/prd", "SECRET1", "FSECRET1", "FSECRET2") | ||
) | ||
|
||
expected_json = { | ||
"SECRET1"=>"secret1", | ||
"FSECRET1"=>"fsecret1", | ||
"FSECRET2"=>"fsecret2" | ||
} | ||
|
||
assert_equal expected_json, json | ||
end | ||
|
||
test "fetch having DOPPLER_TOKEN" do | ||
ENV["DOPPLER_TOKEN"] = "dp.st.xxxxxxxxxxxxxxxxxxxxxx" | ||
|
||
stub_ticks_with("doppler --version 2> /dev/null", succeed: true) | ||
stub_ticks.with("doppler me --json 2> /dev/null") | ||
|
||
stub_ticks | ||
.with("doppler secrets get SECRET1 FSECRET1 FSECRET2 --json ") | ||
.returns(<<~JSON) | ||
{ | ||
"SECRET1": { | ||
"computed":"secret1", | ||
"computedVisibility":"unmasked", | ||
"note":"" | ||
}, | ||
"FSECRET1": { | ||
"computed":"fsecret1", | ||
"computedVisibility":"unmasked", | ||
"note":"" | ||
}, | ||
"FSECRET2": { | ||
"computed":"fsecret2", | ||
"computedVisibility":"unmasked", | ||
"note":"" | ||
} | ||
} | ||
JSON | ||
|
||
json = JSON.parse( | ||
shellunescape run_command("fetch", "SECRET1", "FSECRET1", "FSECRET2") | ||
) | ||
|
||
expected_json = { | ||
"SECRET1"=>"secret1", | ||
"FSECRET1"=>"fsecret1", | ||
"FSECRET2"=>"fsecret2" | ||
} | ||
|
||
assert_equal expected_json, json | ||
|
||
ENV.delete("DOPPLER_TOKEN") | ||
end | ||
|
||
test "fetch with folder in secret" do | ||
stub_ticks_with("doppler --version 2> /dev/null", succeed: true) | ||
stub_ticks.with("doppler me --json 2> /dev/null") | ||
|
||
stub_ticks | ||
.with("doppler secrets get SECRET1 FSECRET1 FSECRET2 --json -p my-project -c prd") | ||
.returns(<<~JSON) | ||
{ | ||
"SECRET1": { | ||
"computed":"secret1", | ||
"computedVisibility":"unmasked", | ||
"note":"" | ||
}, | ||
"FSECRET1": { | ||
"computed":"fsecret1", | ||
"computedVisibility":"unmasked", | ||
"note":"" | ||
}, | ||
"FSECRET2": { | ||
"computed":"fsecret2", | ||
"computedVisibility":"unmasked", | ||
"note":"" | ||
} | ||
} | ||
JSON | ||
|
||
json = JSON.parse( | ||
shellunescape run_command("fetch", "my-project/prd/SECRET1", "my-project/prd/FSECRET1", "my-project/prd/FSECRET2") | ||
) | ||
|
||
expected_json = { | ||
"SECRET1"=>"secret1", | ||
"FSECRET1"=>"fsecret1", | ||
"FSECRET2"=>"fsecret2" | ||
} | ||
|
||
assert_equal expected_json, json | ||
end | ||
|
||
test "fetch without --from" do | ||
stub_ticks_with("doppler --version 2> /dev/null", succeed: true) | ||
stub_ticks.with("doppler me --json 2> /dev/null") | ||
|
||
error = assert_raises RuntimeError do | ||
run_command("fetch", "FSECRET1", "FSECRET2") | ||
end | ||
|
||
assert_equal "Missing project or config from '--from=project/config' option", error.message | ||
end | ||
|
||
test "fetch with signin" do | ||
stub_ticks_with("doppler --version 2> /dev/null", succeed: true) | ||
stub_ticks_with("doppler me --json 2> /dev/null", succeed: false) | ||
stub_ticks_with("doppler login -y", succeed: true).returns("") | ||
stub_ticks.with("doppler secrets get SECRET1 --json -p my-project -c prd").returns(single_item_json) | ||
|
||
json = JSON.parse(shellunescape(run_command("fetch", "--from", "my-project/prd", "SECRET1"))) | ||
|
||
expected_json = { | ||
"SECRET1"=>"secret1" | ||
} | ||
|
||
assert_equal expected_json, json | ||
end | ||
|
||
test "fetch without CLI installed" do | ||
stub_ticks_with("doppler --version 2> /dev/null", succeed: false) | ||
|
||
error = assert_raises RuntimeError do | ||
JSON.parse(shellunescape(run_command("fetch", "HOST", "PORT"))) | ||
end | ||
|
||
assert_equal "Doppler CLI is not installed", error.message | ||
end | ||
|
||
private | ||
def run_command(*command) | ||
stdouted do | ||
Kamal::Cli::Secrets.start \ | ||
[ *command, | ||
"-c", "test/fixtures/deploy_with_accessories.yml", | ||
"--adapter", "doppler" ] | ||
end | ||
end | ||
|
||
def single_item_json | ||
<<~JSON | ||
{ | ||
"SECRET1": { | ||
"computed":"secret1", | ||
"computedVisibility":"unmasked", | ||
"note":"" | ||
} | ||
} | ||
JSON | ||
end | ||
end |