From fe1700e686a645e8ce821aea43978fa552d72225 Mon Sep 17 00:00:00 2001 From: Adam Stephens Date: Tue, 20 Aug 2024 19:32:40 -0400 Subject: [PATCH 1/2] support sso credentials with profile in section name --- lib/ex_aws/credentials_ini/file.ex | 4 ++ test/ex_aws/credentials_ini/file_test.exs | 53 +++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/lib/ex_aws/credentials_ini/file.ex b/lib/ex_aws/credentials_ini/file.ex index 9ae3932e..9defe235 100644 --- a/lib/ex_aws/credentials_ini/file.ex +++ b/lib/ex_aws/credentials_ini/file.ex @@ -234,6 +234,10 @@ if Code.ensure_loaded?(ConfigParser) do merge_special_keys(full, config) |> strip_key_prefix() + {:ok, %{("profile " <> ^profile_name) => config} = full} -> + merge_special_keys(full, config) + |> strip_key_prefix() + {:ok, %{}} -> %{} diff --git a/test/ex_aws/credentials_ini/file_test.exs b/test/ex_aws/credentials_ini/file_test.exs index e18bf706..dc8d3fc5 100644 --- a/test/ex_aws/credentials_ini/file_test.exs +++ b/test/ex_aws/credentials_ini/file_test.exs @@ -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") @@ -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] From 6d03db06739572345d3d017c3824d135383c33df Mon Sep 17 00:00:00 2001 From: Bernard Duggan Date: Tue, 10 Sep 2024 14:39:12 +1000 Subject: [PATCH 2/2] Fix compilation on Elixir 1.13 --- lib/ex_aws/credentials_ini/file.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ex_aws/credentials_ini/file.ex b/lib/ex_aws/credentials_ini/file.ex index 9defe235..41f1f3b1 100644 --- a/lib/ex_aws/credentials_ini/file.ex +++ b/lib/ex_aws/credentials_ini/file.ex @@ -227,6 +227,8 @@ if Code.ensure_loaded?(ConfigParser) do end def parse_ini_file({:ok, contents}, profile_name) do + composite_key = "profile " <> profile_name + contents |> ConfigParser.parse_string() |> case do @@ -234,7 +236,7 @@ if Code.ensure_loaded?(ConfigParser) do merge_special_keys(full, config) |> strip_key_prefix() - {:ok, %{("profile " <> ^profile_name) => config} = full} -> + {:ok, %{^composite_key => config} = full} -> merge_special_keys(full, config) |> strip_key_prefix()