Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict type Dependabot::Docker::Requirement::CredentialsFinder #9889

Merged
16 changes: 10 additions & 6 deletions docker/lib/dependabot/docker/utils/credentials_finder.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "aws-sdk-ecr"
require "base64"
require "sorbet-runtime"

require "dependabot/credential"
require "dependabot/errors"
Expand All @@ -16,6 +17,7 @@ class CredentialsFinder
AWS_ECR_URL = /dkr\.ecr\.(?<region>[^.]+)\.amazonaws\.com/
DEFAULT_DOCKER_HUB_REGISTRY = "registry.hub.docker.com"

sig { params(credentials: T::Array[Dependabot::Credential]).void }
def initialize(credentials)
@credentials = credentials
end
Expand All @@ -32,14 +34,16 @@ def credentials_for_registry(registry_hostname)
build_aws_credentials(registry_details)
end

sig { returns(T.nilable(String)) }
def base_registry
@base_registry ||= T.let(nil, T.nilable(String))
@base_registry ||= credentials.find do |cred|
cred["type"] == "docker_registry" && cred.replaces_base?
end
@base_registry ||= { "registry" => DEFAULT_DOCKER_HUB_REGISTRY, "credentials" => nil }
@base_registry["registry"]
end&.fetch("registry", nil)
@base_registry ||= { "registry" => DEFAULT_DOCKER_HUB_REGISTRY, "credentials" => nil }["registry"]
raj-meka marked this conversation as resolved.
Show resolved Hide resolved
end

sig { params(registry: String).returns(T::Boolean) }
def using_dockerhub?(registry)
registry == DEFAULT_DOCKER_HUB_REGISTRY
end
Expand Down Expand Up @@ -76,11 +80,11 @@ def build_aws_credentials(registry_details)

# Otherwise, we need to use the provided Access Key ID and secret to
# generate a temporary username and password
@authorization_tokens ||= {}
@authorization_tokens ||= T.let({}, T.nilable(T::Hash[String, String]))
@authorization_tokens[registry_hostname] ||=
ecr_client.get_authorization_token.authorization_data.first.authorization_token
username, password =
Base64.decode64(@authorization_tokens[registry_hostname]).split(":")
Base64.decode64(T.must(@authorization_tokens[registry_hostname])).split(":")
registry_details.merge(Dependabot::Credential.new({ "username" => username, "password" => password }))
rescue Aws::Errors::MissingCredentialsError,
Aws::ECR::Errors::UnrecognizedClientException,
Expand Down
Loading