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

Implement Core API for retrieving docker authorization #106

Merged
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
21 changes: 21 additions & 0 deletions features/docker_registry_credentials.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Feature: Docker Registry Credentials
In order to authorize the Docker Engine with the AWS ECR registry
As a User
I want to be able to retrieve the Docker authorization credentials

Background:
Given I'm an Engine Yard user
And ey-core is configured with my cloud token
And I have the following accounts:
| Account Name |
| one |
| two |

Scenario Outline: Retrieving Docker authorization credentials
When I run `ey-core get-docker-registry-login <Account Flag> one <Location Flag> us-east-1`
Then I see the docker login command

Examples:
| Account Flag | Location Flag |
| -c | -l |
| --account | --location |
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Then %(I see the docker login command) do
expect(output_text).to match("docker login -u foo -p bar https://012345678901.dkr.ecr.us-east-1.amazonaws.com")
end
29 changes: 29 additions & 0 deletions lib/ey-core/cli/docker_registry_login.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'ey-core/cli/subcommand'

module Ey
module Core
module Cli
class DockerRegistryLogin < Subcommand
title "get-docker-registry-login"
summary "Prints the docker login command to authorize the Docker Engine with the AWS ECR registry"

option :account,
short: 'c',
long: 'account',
description: 'Name or ID of the account that the environment resides in.',
argument: 'Account name or id'

option :location,
short: 'l',
long: 'location',
description: 'ECR availability regions',
argument: 'Location name'

def handle
credentials = core_account.retrieve_docker_registry_credentials(option(:location))
stdout.puts "docker login -u #{credentials.username} -p #{credentials.password} #{credentials.registry_endpoint}"
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/ey-core/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'ey-core/cli/console'
require 'ey-core/cli/current_user'
require 'ey-core/cli/deploy'
require 'ey-core/cli/docker_registry_login'
require 'ey-core/cli/environments'
require 'ey-core/cli/environment_variables'
require 'ey-core/cli/help'
Expand Down Expand Up @@ -45,6 +46,7 @@ class Main < Belafonte::App
mount Console
mount CurrentUser
mount Deploy
mount DockerRegistryLogin
mount Environments
mount EnvironmentVariables
mount Help
Expand Down
1 change: 1 addition & 0 deletions lib/ey-core/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ class Ey::Core::Client < Cistern::Service
request :reset_password
request :reset_server_state
request :restart_environment_app_servers
request :retrieve_docker_registry_credentials
request :run_cluster_application_action
request :run_environment_application_action
request :signup
Expand Down
10 changes: 10 additions & 0 deletions lib/ey-core/models/account.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'hashie'

class Ey::Core::Client::Account < Ey::Core::Model
extend Ey::Core::Associations

Expand Down Expand Up @@ -67,4 +69,12 @@ def save!
else raise NotImplementedError # update
end
end

# Get authorization data for an Amazon ECR registry.
# @param [String] location_id Aws region
# @return [Hashie::Mash]
def retrieve_docker_registry_credentials(location_id)
result = self.connection.retrieve_docker_registry_credentials(self.id, location_id).body
::Hashie::Mash.new(result['docker_registry_credentials'])
end
end
24 changes: 24 additions & 0 deletions lib/ey-core/requests/retrieve_docker_registry_credentials.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Ey::Core::Client
class Real
def retrieve_docker_registry_credentials(account_id, location_id)
request(
:path => "/accounts/#{account_id}/docker-registry/#{location_id}/credentials"
)
end
end

class Mock
def retrieve_docker_registry_credentials(account_id, location_id)
response(
:body => {
'docker_registry_credentials' => {
'username' => 'foo',
'password' => 'bar',
'registry_endpoint' => "https://012345678901.dkr.ecr.#{location_id}.amazonaws.com",
'expires_at' => (Time.now + 8 * 3600).to_i
}
}
)
end
end
end
16 changes: 16 additions & 0 deletions spec/docker_registry_credentials_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

describe 'as a user' do
let(:client) { create_client }
let(:location) { 'us-east-1' }
let!(:account) { create_account(client: client) }

it 'returns a valid docker registry credentials' do
credentials = account.retrieve_docker_registry_credentials(location)

expect(credentials.username).to be
expect(credentials.password).to be
expect(credentials.registry_endpoint).to match(/#{location}/)
expect(credentials.expires_at).to be > (Time.now + 6 * 3600).to_i
end
end