Skip to content

Commit

Permalink
support sso credentials with profile in section name
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcstephens authored and bernardd committed Sep 10, 2024
1 parent c0b431f commit fe1700e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/ex_aws/credentials_ini/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ if Code.ensure_loaded?(ConfigParser) do
merge_special_keys(full, config)
|> strip_key_prefix()

{:ok, %{("profile " <> ^profile_name) => config} = full} ->

Check failure on line 237 in lib/ex_aws/credentials_ini/file.ex

View workflow job for this annotation

GitHub Actions / build (24.x, 1.13.x)

** (CompileError) lib/ex_aws/credentials_ini/file.ex:237: cannot use pin operator ^profile_name inside a data structure as a map key in a pattern. The pin operator can only be used as the whole key
merge_special_keys(full, config)
|> strip_key_prefix()

{:ok, %{}} ->
%{}

Expand Down
53 changes: 53 additions & 0 deletions test/ex_aws/credentials_ini/file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ defmodule ExAws.CredentialsIni.File.FileTest do
assert config.sso_role_name == "SomeRole"
end

test "config file is parsed with non-default sso config profile that uses sso_session" do
example_config = """
[sso-session somecompany]
sso_start_url = https://start.us-gov-home.awsapps.com/directory/somecompany
sso_region = us-gov-west-1
[profile somecompany]
sso_session = somecompany
sso_account_id = 123456789101
sso_role_name = SomeRole
region = us-gov-west-1
output = json
"""

config = ExAws.CredentialsIni.File.parse_ini_file({:ok, example_config}, "somecompany")

assert config.sso_session == "somecompany"
assert config.sso_start_url == "https://start.us-gov-home.awsapps.com/directory/somecompany"
assert config.sso_region == "us-gov-west-1"
assert config.sso_account_id == "123456789101"
assert config.sso_role_name == "SomeRole"
end

test "{:system} in profile name gets dynamic profile name" do
System.put_env("AWS_PROFILE", "custom-profile")

Expand All @@ -79,6 +102,36 @@ defmodule ExAws.CredentialsIni.File.FileTest do
assert credentials.security_token == "TESTTOKEN"
end

test "{:system} in profile name gets dynamic profile name using sso config" do
System.put_env("AWS_PROFILE", "custom-profile")

example_credentials = """
[sso-session somecompany]
sso_start_url = https://start.us-gov-home.awsapps.com/directory/somecompany
sso_region = us-gov-west-1
[profile custom-profile]
sso_session = somecompany
sso_account_id = 123456789101
sso_role_name = SomeRole
region = us-gov-west-1
output = json
"""

credentials =
ExAws.CredentialsIni.File.parse_ini_file({:ok, example_credentials}, :system)
|> ExAws.CredentialsIni.File.replace_token_key()

assert credentials.sso_session == "somecompany"

assert credentials.sso_start_url ==
"https://start.us-gov-home.awsapps.com/directory/somecompany"

assert credentials.sso_region == "us-gov-west-1"
assert credentials.sso_account_id == "123456789101"
assert credentials.sso_role_name == "SomeRole"
end

test "config file is parsed" do
example_config = """
[default]
Expand Down

0 comments on commit fe1700e

Please sign in to comment.