Skip to content

Commit

Permalink
add resolver for acm certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayerl committed Jun 8, 2018
1 parent 8e211c1 commit f7215e8
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,15 @@ db_username:
env: DB_USERNAME
```

### ACM Certificates

Find an ACM certificate by domain name:

```yaml
cert:
acm_certificate: www.example.com
```

### Custom parameter resolvers

New parameter resolvers can be created in a separate gem.
Expand Down
14 changes: 8 additions & 6 deletions lib/stack_master.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require 'commander'
require 'yaml'
require "aws-sdk-cloudformation"
require "aws-sdk-ec2"
require "aws-sdk-s3"
require "aws-sdk-sns"
require "aws-sdk-ssm"
require "colorize"
require 'aws-sdk-acm'
require 'aws-sdk-cloudformation'
require 'aws-sdk-ec2'
require 'aws-sdk-s3'
require 'aws-sdk-sns'
require 'aws-sdk-ssm'
require 'colorize'
require 'active_support/core_ext/string'
require 'multi_json'

Expand Down Expand Up @@ -60,6 +61,7 @@ module Commands
end

module ParameterResolvers
autoload :AcmCertificate, 'stack_master/parameter_resolvers/acm_certificate'
autoload :AmiFinder, 'stack_master/parameter_resolvers/ami_finder'
autoload :StackOutput, 'stack_master/parameter_resolvers/stack_output'
autoload :Secret, 'stack_master/parameter_resolvers/secret'
Expand Down
37 changes: 37 additions & 0 deletions lib/stack_master/parameter_resolvers/acm_certificate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module StackMaster
module ParameterResolvers
class AcmCertificate < Resolver
CertificateNotFound = Class.new(StandardError)

def initialize(config, stack_definition)
@config = config
@stack_definition = stack_definition
end

def resolve(domain_name)
cert_arn = find_cert_arn_by_domain_name(domain_name)
raise CertificateNotFound, "Could not find certificate #{domain_name} in #{@stack_definition.region}" unless cert_arn
cert_arn
end

private

def all_certs
certs = []
next_token = nil
client = Aws::ACM::Client.new(region: @stack_definition.region)
loop do
resp = client.list_certificates(certificate_statuses: ['ISSUED'], next_token: next_token)
certs << resp.certificate_summary_list
next_token = resp.next_token
break if next_token.nil?
end
certs.flatten
end

def find_cert_arn_by_domain_name(domain_name)
all_certs.map { |c| c.certificate_arn if c.domain_name == domain_name }.compact.first
end
end
end
end
36 changes: 36 additions & 0 deletions spec/stack_master/parameter_resolvers/acm_certificate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
RSpec.describe StackMaster::ParameterResolvers::AcmCertificate do
let(:config) { double(base_dir: '/base') }
let(:stack_definition) { double(stack_name: 'mystack', region: 'us-east-1') }
subject(:resolver) { described_class.new(config, stack_definition) }
let(:acm) { Aws::ACM::Client.new }

before do
allow(Aws::ACM::Client).to receive(:new).and_return(acm)
end

context 'when a certificate is found' do
before do
acm.stub_responses(:list_certificates, certificate_summary_list: [
{ certificate_arn: 'arn:aws:acm:us-east-1:12345:certificate/abc', domain_name: 'abc' },
{ certificate_arn: 'arn:aws:acm:us-east-1:12345:certificate/def', domain_name: 'def' }
])
end

it 'returns the certificate' do
expect(resolver.resolve('def')).to eq 'arn:aws:acm:us-east-1:12345:certificate/def'
end
end

context 'when no certificate is found' do
before do
acm.stub_responses(:list_certificates, certificate_summary_list: [])
end

it 'raises an error' do
expect { resolver.resolve('def') }.to raise_error(
StackMaster::ParameterResolvers::AcmCertificate::CertificateNotFound,
'Could not find certificate def in us-east-1'
)
end
end
end
1 change: 1 addition & 0 deletions stack_master.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "os"
spec.add_dependency "ruby-progressbar"
spec.add_dependency "commander"
spec.add_dependency "aws-sdk-acm", "~> 1"
spec.add_dependency "aws-sdk-cloudformation", "~> 1"
spec.add_dependency "aws-sdk-ec2", "~> 1"
spec.add_dependency "aws-sdk-s3", "~> 1"
Expand Down

0 comments on commit f7215e8

Please sign in to comment.