Class: Terrafying::Components::LetsEncrypt

Inherits:
Terrafying::Context
  • Object
show all
Includes:
CA
Defined in:
lib/terrafying/components/letsencrypt.rb

Constant Summary collapse

PROVIDERS =
{
  staging: {
    api_url: 'https://acme-staging.api.letsencrypt.org/directory',
    ca_cert: 'https://letsencrypt.org/certs/fakeleintermediatex1.pem'
  },
  live:    {
    api_url: 'https://acme-v01.api.letsencrypt.org/directory',
    ca_cert: 'https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem.txt'
  }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CA

#<=>, #create_keypair, #reference_keypair

Constructor Details

#initializeLetsEncrypt

Returns a new instance of LetsEncrypt.



30
31
32
# File 'lib/terrafying/components/letsencrypt.rb', line 30

def initialize()
  super
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/terrafying/components/letsencrypt.rb', line 11

def name
  @name
end

#sourceObject (readonly)

Returns the value of attribute source.



11
12
13
# File 'lib/terrafying/components/letsencrypt.rb', line 11

def source
  @source
end

Class Method Details

.create(name, bucket, options = {}) ⇒ Object



26
27
28
# File 'lib/terrafying/components/letsencrypt.rb', line 26

def self.create(name, bucket, options={})
  LetsEncrypt.new.create name, bucket, options
end

Instance Method Details

#create(name, bucket, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/terrafying/components/letsencrypt.rb', line 34

def create(name, bucket, options={})
  options = {
    prefix: "",
    provider: :staging,
    email_address: "[email protected]",
    public_certificate: false,
  }.merge(options)

  @name = name
  @bucket = bucket
  @prefix = options[:prefix]
  @provider = PROVIDERS[options[:provider].to_sym]

  provider :acme, {}
  provider :tls, {}

  resource :tls_private_key, "#{@name}-account", {
             algorithm: "ECDSA",
             ecdsa_curve: "P384",
           }

  @account_key = output_of(:tls_private_key, "#{@name}-account", "private_key_pem")

  @registration_url = resource :acme_registration, "#{@name}-reg", {
                                 server_url: @provider[:server_url],
                                 account_key_pem: @account_key,
                                 email_address: options[:email_address],
                               }

  resource :aws_s3_bucket_object, "#{@name}-account", {
             bucket: @bucket,
             key: File.join(@prefix, @name, "account.key"),
             content: @account_key,
           }

  @ca_cert_acl = options[:public_certificate] ? 'public-read' : 'private'

  open(@provider[:ca_cert], 'rb') do |cert|
    resource :aws_s3_bucket_object, "#{@name}-cert", {
               bucket: @bucket,
               key: File.join(@prefix, @name, "ca.cert"),
               content: cert.read,
               acl: @ca_cert_acl
             }
  end

  @source = File.join("s3://", @bucket, @prefix, @name, "ca.cert")

  self
end

#create_keypair_in(ctx, name, options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/terrafying/components/letsencrypt.rb', line 85

def create_keypair_in(ctx, name, options={})
  options = {
    common_name: name,
    organization: "uSwitch Limited",
    validity_in_hours: 24 * 365,
    allowed_uses: [
      "nonRepudiation",
      "digitalSignature",
      "keyEncipherment"
    ],
    dns_names: [],
    ip_addresses: [],
    min_days_remaining: 21,
  }.merge(options)

  key_ident = "#{@name}-#{tf_safe(name)}"

  ctx.resource :tls_private_key, key_ident, {
                 algorithm: "ECDSA",
                 ecdsa_curve: "P384",
               }

  ctx.resource :tls_cert_request, key_ident, {
                 key_algorithm: "ECDSA",
                 private_key_pem: output_of(:tls_private_key, key_ident, :private_key_pem),
                 subject: {
                   common_name: options[:common_name],
                   organization: options[:organization],
                 },
                 dns_names: options[:dns_names],
                 ip_addresses: options[:ip_addresses],
               }

  ctx.resource :acme_certificate, key_ident, {
                 server_url: @provider[:server_url],
                 account_key_pem: @account_key,
                 registration_url: @registration_url,
                 min_days_remaining: options[:min_days_remaining],
                 dns_challenge: {
                   provider: "route53",
                 },
                 certificate_request_pem: output_of(:tls_cert_request, key_ident, :cert_request_pem),
               }

  ctx.resource :aws_s3_bucket_object, "#{key_ident}-key", {
                 bucket: @bucket,
                 key: File.join(@prefix, @name, name, "key"),
                 content: output_of(:tls_private_key, key_ident, :private_key_pem),
               }

  ctx.resource :aws_s3_bucket_object, "#{key_ident}-cert", {
                 bucket: @bucket,
                 key: File.join(@prefix, @name, name, "cert"),
                 content: output_of(:acme_certificate, key_ident, :certificate_pem),
               }

  reference_keypair(ctx, name)
end