Skip to content

Commit

Permalink
fix: make sure the code passes new ameba (#16)
Browse files Browse the repository at this point in the history
* fix: make sure the code passes new ameba

* fix: change variable names
  • Loading branch information
anton7c3 authored Jun 30, 2024
1 parent f7aa414 commit a25a1fa
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
12 changes: 6 additions & 6 deletions spec/it/assume_role_with_sts_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ module Aws::Credentials
access_key_id = ENV["AC1_AWS_ACCESS_KEY_ID"]?
secret_access_key = ENV["AC1_AWS_SECRET_ACCESS_KEY"]?

if !(region && role_arn && access_key_id && secret_access_key)
unless region && role_arn && access_key_id && secret_access_key
next
end

signer = ->(request : HTTP::Request, credentials : Credentials) {
Awscr::Signer::Signers::V4.new("sts", region.not_nil!, credentials.access_key_id, credentials.secret_access_key).sign(request)
Awscr::Signer::Signers::V4.new("sts", region, credentials.access_key_id, credentials.secret_access_key).sign(request)
request
}

role_provider = AssumeRoleProvider.new(
role_arn: role_arn.not_nil!,
role_arn: role_arn,
role_session_name: "Bob",
sts_client: STSClient.new(
contractor_credential_provider: Providers.new([SimpleCredentials.new(
access_key_id: access_key_id.not_nil!,
secret_access_key: secret_access_key.not_nil!,
access_key_id: access_key_id,
secret_access_key: secret_access_key,
)] of Provider).as(Provider),
signer: signer,
region: region.not_nil!,
region: region,
),
duration: 900.seconds,
)
Expand Down
1 change: 1 addition & 0 deletions spec/scenarios.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ameba:disable Lint/SpecFilename
require "json"

module Scenarios
Expand Down
1 change: 1 addition & 0 deletions spec/stub_server.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ameba:disable Lint/SpecFilename
require "http/server"

module ServerStub
Expand Down
6 changes: 3 additions & 3 deletions src/aws-credentials/http_client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ require "http/client"

module Aws::Credentials
module HTTPClient
def self.get(uri : URI, headers : HTTP::Headers? = nil)
def self.get(uri : URI, maybe_headers : HTTP::Headers? = nil)
http = HTTP::Client.new uri
begin
http.connect_timeout = 5.seconds
headers.try do |h|
http.headers = h
maybe_headers.try do |headers|
http.headers = headers
end
http.get uri.path || raise "Missin URL path"
ensure
Expand Down
11 changes: 3 additions & 8 deletions src/aws-credentials/provider.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,12 @@ module Aws::Credentials
end

private def resolve_credentials : Credentials
@providers.find { |p|
p.credentials? ? true : false
}.try &.credentials? || raise MissingCredentials.new "No provider serves credential : #{@providers.map(&.class.name)}"
@providers.find(&.credentials?).try(&.credentials?) ||
raise MissingCredentials.new "No provider serves credential : #{@providers.map(&.class.name)}"
end

def refresh : Nil
@providers.each do |p|
p.refresh
rescue
# Nothing to do
end
@providers.each(&.refresh) rescue nil
@resolved = nil
end
end
Expand Down
20 changes: 10 additions & 10 deletions src/aws-credentials/sts_client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ module Aws::Credentials
end

def assume_role(
@role_arn : String,
@role_session_name : String,
@duration : Time::Span? = nil,
@policy : JSON::Any? = nil
role_arn : String,
role_session_name : String,
maybe_duration : Time::Span? = nil,
maybe_policy : JSON::Any? = nil
) : Credentials
param = HTTP::Params.build { |form|
form.add "Version", "2011-06-15"
form.add "Action", "AssumeRole"
form.add "RoleSessionName", @role_session_name
form.add "RoleArn", @role_arn
@policy.try { |p|
form.add "Policy", p.to_json
form.add "RoleSessionName", role_session_name
form.add "RoleArn", role_arn
maybe_policy.try { |policy|
form.add "Policy", policy.to_json
}
@duration.try { |d|
form.add "DurationSeconds", d.total_seconds.to_i64.to_s
maybe_duration.try { |duration|
form.add "DurationSeconds", duration.total_seconds.to_i64.to_s
}
form
}
Expand Down

0 comments on commit a25a1fa

Please sign in to comment.