From a25a1fa7664317be5d75ec3a3f70075f37d9d21e Mon Sep 17 00:00:00 2001 From: Anton Karankevich <83635660+anton7c3@users.noreply.github.com> Date: Sun, 30 Jun 2024 10:03:52 +0300 Subject: [PATCH] fix: make sure the code passes new ameba (#16) * fix: make sure the code passes new ameba * fix: change variable names --- spec/it/assume_role_with_sts_spec.cr | 12 ++++++------ spec/scenarios.cr | 1 + spec/stub_server.cr | 1 + src/aws-credentials/http_client.cr | 6 +++--- src/aws-credentials/provider.cr | 11 +++-------- src/aws-credentials/sts_client.cr | 20 ++++++++++---------- 6 files changed, 24 insertions(+), 27 deletions(-) diff --git a/spec/it/assume_role_with_sts_spec.cr b/spec/it/assume_role_with_sts_spec.cr index 368af1d..451f2e7 100644 --- a/spec/it/assume_role_with_sts_spec.cr +++ b/spec/it/assume_role_with_sts_spec.cr @@ -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, ) diff --git a/spec/scenarios.cr b/spec/scenarios.cr index 510c80e..3d8068e 100644 --- a/spec/scenarios.cr +++ b/spec/scenarios.cr @@ -1,3 +1,4 @@ +# ameba:disable Lint/SpecFilename require "json" module Scenarios diff --git a/spec/stub_server.cr b/spec/stub_server.cr index 8fb6689..935459f 100644 --- a/spec/stub_server.cr +++ b/spec/stub_server.cr @@ -1,3 +1,4 @@ +# ameba:disable Lint/SpecFilename require "http/server" module ServerStub diff --git a/src/aws-credentials/http_client.cr b/src/aws-credentials/http_client.cr index 14784de..75d57c5 100644 --- a/src/aws-credentials/http_client.cr +++ b/src/aws-credentials/http_client.cr @@ -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 diff --git a/src/aws-credentials/provider.cr b/src/aws-credentials/provider.cr index bdf92d9..57d1a76 100644 --- a/src/aws-credentials/provider.cr +++ b/src/aws-credentials/provider.cr @@ -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 diff --git a/src/aws-credentials/sts_client.cr b/src/aws-credentials/sts_client.cr index 7eb332f..7391bdc 100644 --- a/src/aws-credentials/sts_client.cr +++ b/src/aws-credentials/sts_client.cr @@ -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 }