Skip to content
Merged
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
9 changes: 6 additions & 3 deletions src/utilities/credentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ function _get_ini_value(
end

function _aws_profile_config(ini::Inifile, profile::AbstractString)
if profile != "default" || !haskey(sections(ini), "default")
profile = "profile $profile"
# Prefer using "profile default" over "default"
section_name = if profile != "default" || haskey(sections(ini), "profile default")
"profile $profile"
else
"default"
end

return get(sections(ini), profile, Dict())
return copy(get(sections(ini), section_name, IniFile.HTSS()))
end

function _aws_profile_config(ini::Inifile, profile::Nothing)
Expand Down
4 changes: 1 addition & 3 deletions test/AWSCredentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ end
"""
[profile test]
output = json
region = us-east-1

[profile test:dev]
source_profile = test
Expand All @@ -226,8 +225,6 @@ end
[profile test2]
aws_access_key_id = WRONG_ACCESS_ID
aws_secret_access_key = WRONG_ACCESS_KEY
output = json
region = us-east-1

[profile test3]
source_profile = test:dev
Expand Down Expand Up @@ -264,6 +261,7 @@ end
"AWS_DEFAULT_PROFILE" => "test",
"AWS_PROFILE" => nothing,
"AWS_ACCESS_KEY_ID" => nothing,
"AWS_REGION" => "us-east-1",
) do
@testset "Loading" begin
# Check credentials load
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using Dates
using Downloads
using GitHub
using HTTP
using IniFile: Inifile
using IniFile: Inifile, sections
using JSON
using OrderedCollections: LittleDict, OrderedDict
using MbedTLS: digest, MD_SHA256, MD_MD5
Expand Down Expand Up @@ -66,6 +66,7 @@ const AWS_CONFIG = Ref{AbstractAWSConfig}()
@testset "Unit Tests" begin
if RUN_UNIT_TESTS
include("unit/AWS.jl")
include("unit/AWSCredentials.jl")
else
@warn "Skipping unit tests"
end
Expand Down
63 changes: 63 additions & 0 deletions test/unit/AWSCredentials.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
@testset "_aws_profile_config" begin
using AWS: _aws_profile_config

@testset "access" begin
#! format: off
buffer = IOBuffer(
"""
[profile A]
output = json
region = us-east-1

[profile B]
role_arn = arn:aws:iam::123456789000:role/A
source_profile = A
"""
)
#! format: on
ini = Inifile()
read(ini, buffer)

# Retrieve fields from profile "A"
config = _aws_profile_config(ini, "A")
@test keys(config) ⊆ Set(["output", "region"])
@test config["output"] == "json"
@test config["region"] == "us-east-1"

# Ensure that mutating the returned dictionary does not mutated the contents of the
# `ini`
config["output"] = "yaml-stream"
section = sections(ini)["profile A"]
@test section["output"] == "json"

# Use of `source_profile` does not inherit properties from the source. This mirrors
# the AWS CLI (version v2.27.15) behavior for `region` which can be seen using
# `aws configure list --profile X`
config = _aws_profile_config(ini, "B")
@test keys(config) ⊆ Set(["role_arn", "source_profile"])
@test config["role_arn"] == "arn:aws:iam::123456789000:role/A"
@test config["source_profile"] == "A"
end

# AWS CLI (version v2.27.15) will use "profile default" over "default" when both are
# defined within the configuration. This is true when `AWS_PROFILE` is unset or
# `AWS_PROFILE="default".
@testset "default profile" begin
#! format: off
buffer = IOBuffer(
"""
[default]
region = default-1

[profile default]
region = default-2
"""
)
#! format: on
ini = Inifile()
read(ini, buffer)

config = _aws_profile_config(ini, "default")
@test config["region"] == "default-2"
end
end
Loading