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

Fix JSON ParserError with kamal secrets extract $SECRETS #1284

Open
wants to merge 1 commit into
base: main
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
8 changes: 7 additions & 1 deletion lib/kamal/cli/secrets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def fetch(*secrets)
desc "extract", "Extract a single secret from the results of a fetch call"
option :inline, type: :boolean, required: false, hidden: true
def extract(name, secrets)
parsed_secrets = JSON.parse(secrets)
parsed_secrets = parse_secrets(secrets)
value = parsed_secrets[name] || parsed_secrets.find { |k, v| k.end_with?("/#{name}") }&.last

raise "Could not find secret #{name}" if value.nil?
Expand All @@ -35,6 +35,12 @@ def print
end

private
def parse_secrets(secrets)
secrets = secrets.shellsplit[0] if secrets.start_with?("\\{")

JSON.parse(secrets)
end

def initialize_adapter(adapter)
Kamal::Secrets::Adapters.lookup(adapter)
end
Expand Down
4 changes: 4 additions & 0 deletions test/cli/secrets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class CliSecretsTest < CliTestCase
assert_equal "oof", run_command("extract", "foo", "{\"foo\":\"oof\", \"bar\":\"rab\", \"baz\":\"zab\"}")
end

test "extract when value contains (" do
assert_equal "bar(", run_command("extract", "foo", "\\{\\\"foo\\\":\\\"bar\\(\\\"\\}")
end

test "extract match from end" do
assert_equal "oof", run_command("extract", "foo", "{\"abc/foo\":\"oof\", \"bar\":\"rab\", \"baz\":\"zab\"}")
end
Expand Down
Loading