Class: Terrafying::Components::LetsEncrypt
- Inherits:
-
Terrafying::Context
- Object
- Terrafying::Context
- Terrafying::Components::LetsEncrypt
- Includes:
- CA
- Defined in:
- lib/terrafying/components/letsencrypt.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #create(name, bucket, options = {}) ⇒ Object
- #create_keypair_in(ctx, name, options = {}) ⇒ Object
- #find(name, bucket, prefix: "") ⇒ Object
- #generate_alpha_num ⇒ Object
-
#initialize ⇒ LetsEncrypt
constructor
A new instance of LetsEncrypt.
- #output_with_children ⇒ Object
- #renew ⇒ Object
- #renew_alert ⇒ Object
- #setup_providers ⇒ Object
Methods included from CA
#<=>, #ca?, #create_keypair, #find_keypair, #object_arn, #object_ident, #object_key, #object_name, #object_url, #reference_keypair
Constructor Details
#initialize ⇒ LetsEncrypt
Returns a new instance of LetsEncrypt.
20 21 22 23 24 |
# File 'lib/terrafying/components/letsencrypt.rb', line 20 def initialize super @acme_providers = setup_providers @zones = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/terrafying/components/letsencrypt.rb', line 9 def name @name end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
9 10 11 |
# File 'lib/terrafying/components/letsencrypt.rb', line 9 def source @source end |
Class Method Details
.create(name, bucket, options = {}) ⇒ Object
13 14 15 |
# File 'lib/terrafying/components/letsencrypt.rb', line 13 def self.create(name, bucket, = {}) LetsEncrypt.new.create name, bucket, end |
.find(name, bucket, options = {}) ⇒ Object
16 17 18 |
# File 'lib/terrafying/components/letsencrypt.rb', line 16 def self.find(name, bucket, = {}) LetsEncrypt.new.find name, bucket, end |
Instance Method Details
#create(name, bucket, options = {}) ⇒ Object
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 84 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 |
# File 'lib/terrafying/components/letsencrypt.rb', line 41 def create(name, bucket, = {}) = { prefix: '', provider: :staging, email_address: '[email protected]', public_certificate: false, curve: 'P384', rsa_bits: '3072', use_external_dns: false, renewing: false, renew_alert_options: { protocol: nil, endpoint: nil, endpoint_auto_confirms: false, confirmation_timeout_in_minutes: 1, raw_message_delivery: false, filter_policy: nil, delivery_policy: nil } }.merge() @name = name @bucket = bucket @prefix = [:prefix] @acme_provider = @acme_providers[[:provider]] @use_external_dns = [:use_external_dns] @renewing = [:renewing] @renew_alert_options = [:renew_alert_options] @prefix_path = [@prefix, @name].reject(&:empty?).join("/") renew() if @renewing renew_alert() if @renew_alert_options[:endpoint] != nil provider :tls, {} resource :tls_private_key, "#{@name}-account", algorithm: "RSA", rsa_bits: [:rsa_bits] resource :acme_registration, "#{@name}-reg", provider: @acme_provider[:ref], account_key_pem: output_of(:tls_private_key, "#{@name}-account", 'private_key_pem'), email_address: [:email_address] @account_key = output_of(:acme_registration, "#{@name}-reg", 'account_key_pem') resource :aws_s3_bucket_object, "#{@name}-account", bucket: @bucket, key: File.join('', @prefix, @name, 'account.key'), content: @account_key resource :aws_s3_bucket_object, "#{@name}-config", { bucket: @bucket, key: File.join('', @prefix, @name, "config.json"), content: { id: output_of(:acme_registration, "#{@name}-reg", "id"), url: @acme_provider[:url], email_address: [:email_address], }.to_json, } @ca_cert_acl = [:public_certificate] ? 'public-read' : 'private' open(@acme_provider[:ca_cert], 'rb') do |cert| @ca_cert = cert.read end resource :aws_s3_bucket_object, object_name(@name, :cert), bucket: @bucket, key: object_key(@name, :cert), content: @ca_cert, acl: @ca_cert_acl @source = object_url(@name, :cert) resource :aws_s3_bucket_object, "#{@name}-metadata", bucket: @bucket, key: File.join('', @prefix, @name, '.metadata'), content: { provider: [:provider].to_s, public_certificate: [:public_certificate], use_external_dns: [:use_external_dns], }.to_json self end |
#create_keypair_in(ctx, name, options = {}) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/terrafying/components/letsencrypt.rb', line 156 def create_keypair_in(ctx, name, = {}) = { common_name: name, organization: "uSwitch Limited", dns_names: [], ip_addresses: [], curve: "P384" }.merge() @zones << [:zone] if [:zone] key_ident = "#{@name}-#{tf_safe(name)}" ctx.resource :tls_private_key, key_ident, algorithm: 'ECDSA', ecdsa_curve: [:curve] 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: [:common_name], organization: [:organization] }, dns_names: [:dns_names], ip_addresses: [:ip_addresses] = {} [:recursive_nameservers] = ['1.1.1.1:53', '8.8.8.8:53', '8.8.4.4:53'] if @use_external_dns @renewing ? min_days_remaining = -1 : min_days_remaining = 21 # we don't want Terraform to renew certs if the certbot lambda is provisioned ctx.resource :acme_certificate, key_ident, { provider: @acme_provider[:ref], account_key_pem: @account_key, min_days_remaining: min_days_remaining, dns_challenge: { provider: 'route53' }, certificate_request_pem: output_of(:tls_cert_request, key_ident, :cert_request_pem) }.merge() csr_version = "${sha256(tls_cert_request.#{key_ident}.cert_request_pem)}" ctx.resource :aws_s3_bucket_object, "#{key_ident}-csr", bucket: @bucket, key: object_key(name, :csr, csr_version), content: output_of(:tls_cert_request, key_ident, :cert_request_pem) ctx.resource :aws_s3_bucket_object, "#{key_ident}-csr-latest", bucket: @bucket, key: object_key(name, :csr, 'latest'), content: csr_version key_version = "${sha256(tls_private_key.#{key_ident}.private_key_pem)}" ctx.resource :aws_s3_bucket_object, "#{key_ident}-key", bucket: @bucket, key: object_key(name, :key, key_version), content: output_of(:tls_private_key, key_ident, :private_key_pem) ctx.resource :aws_s3_bucket_object, "#{key_ident}-key-latest", bucket: @bucket, key: object_key(name, :key, 'latest'), content: key_version cert_version = "${sha256(acme_certificate.#{key_ident}.certificate_pem)}" cert_config = { bucket: @bucket, key: object_key(name, :cert, cert_version), content: output_of(:acme_certificate, key_ident, :certificate_pem).to_s + @ca_cert, } cert_config[:lifecycle] = { ignore_changes: [ "content" ] } if @renewing ctx.resource :aws_s3_bucket_object, "#{key_ident}-cert", cert_config ctx.resource :aws_s3_bucket_object, "#{key_ident}-cert-latest", bucket: @bucket, key: object_key(name, :cert, 'latest'), content: cert_version reference_keypair(ctx, name, key_version: key_version, cert_version: cert_version) end |
#find(name, bucket, prefix: "") ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/terrafying/components/letsencrypt.rb', line 128 def find(name, bucket, prefix: "") @name = name @bucket = bucket @prefix = prefix # load the rest of the config from an s3 metadata file = aws.s3_object(@bucket, [@prefix, @name, '.metadata'].compact.reject(&:empty?).join('/')) = JSON.parse(, symbolize_names: true) @acme_provider = @acme_providers[[:provider].to_sym] @use_external_dns = [:use_external_dns] @ca_cert_acl = [:public_certificate] ? 'public-read' : 'private' account_key_obj = data :aws_s3_bucket_object, "#{@name}-account", bucket: @bucket, key: File.join('', @prefix, @name, 'account.key') @account_key = account_key_obj["body"] open(@acme_provider[:ca_cert], 'rb') do |cert| @ca_cert = cert.read end @source = object_url(@name, :cert) self end |
#generate_alpha_num ⇒ Object
423 424 425 426 427 428 429 |
# File 'lib/terrafying/components/letsencrypt.rb', line 423 def generate_alpha_num() result = @name.split("").each do |ch| alpha_num = ch.upcase.ord - 'A'.ord return alpha_num.abs if (alpha_num.abs < 24) end result.is_a?(Integer) ? result : 6 end |
#output_with_children ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/terrafying/components/letsencrypt.rb', line 241 def output_with_children iam_policy = {} if @renewing iam_policy = resource :aws_iam_policy, "#{@name}_lambda_execution_policy", { name: "#{@name}_lambda_execution_policy", description: "A policy for the #{@name}_lambda function to access S3 and R53", policy: JSON.pretty_generate( { Version: "2012-10-17", Statement: [ { Action: [ "s3:Put*", "s3:Get*", "s3:DeleteObject" ], Resource: [ "arn:aws:s3:::#{@bucket}/#{@prefix_path}/*" ], Effect: "Allow" }, { Action: [ "s3:ListBucket" ], Resource: [ "arn:aws:s3:::#{@bucket}" ], Effect: "Allow" }, { Action: [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], Resource: [ "arn:aws:logs:*:*:*" ], Effect: "Allow" }, { Action: [ "route53:ListHostedZones", ], Resource: [ "*" ], Effect: "Allow" }, { Action: [ "route53:GetChange", ], Resource: [ "arn:aws:route53:::change/*" ], Effect: "Allow" }, { Action: [ "route53:ChangeResourceRecordSets", ], Resource: @zones.compact.map { | zone | "arn:aws:route53:::#{zone.id[1..-1]}" }, Effect: "Allow" } ] } ) } end super end |
#renew ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/terrafying/components/letsencrypt.rb', line 318 def renew execution_role = resource :aws_iam_role, "#{@name}_lambda_execution", { name: "#{@name}_lambda_execution", assume_role_policy: JSON.pretty_generate( { Version: "2012-10-17", Statement: [ { Action: "sts:AssumeRole", Principal: { Service: "lambda.amazonaws.com" }, Effect: "Allow", Sid: "" } ] } ) } lambda_function = resource :aws_lambda_function, "#{@name}_lambda", { function_name: "#{@name}_lambda", s3_bucket: "uswitch-certbot-lambda", s3_key: "certbot-lambda.zip", handler: "main.handler", runtime: "python3.7", timeout: "900", role: execution_role["arn"], environment:{ variables: { CA_BUCKET: @bucket, CA_PREFIX: @prefix_path } } } resource :aws_iam_role_policy_attachment, "#{@name}_lambda_policy_attachment", { role: execution_role["name"], policy_arn: "${aws_iam_policy.#{@name}_lambda_execution_policy.arn}" } alpha_num = generate_alpha_num().to_s event_rule = resource :aws_cloudwatch_event_rule, "once_per_day", { name: "once-per-day", description: "Fires once per day", schedule_expression: "cron(0 #{alpha_num} * * ? *)" } resource :aws_cloudwatch_event_target, "#{@name}_lambda_event_target", { rule: event_rule["name"], target_id: lambda_function["id"], arn: lambda_function["arn"] } resource :aws_lambda_permission, "allow_cloudwatch_to_invoke_#{@name}_lambda", { statement_id: "AllowExecutionFromCloudWatch", action: "lambda:InvokeFunction", function_name: lambda_function["function_name"], principal: "events.amazonaws.com", source_arn: event_rule["arn"] } self end |
#renew_alert ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/terrafying/components/letsencrypt.rb', line 383 def renew_alert topic = resource :aws_sns_topic, "#{@name}_lambda_cloudwatch_topic", { name: "#{@name}_lambda_cloudwatch_topic" } alarm = resource :aws_cloudwatch_metric_alarm, "#{@name}_lambda_failure_alarm", { alarm_name: "#{@name}-lambda-failure-alarm", comparison_operator: "GreaterThanOrEqualToThreshold", evaluation_periods: "1", period: "300", metric_name: "Errors", namespace: "AWS/Lambda", threshold: 1, statistic: "Maximum", alarm_description: "Alert generated if the #{@name} certbot lambda fails execution", actions_enabled: true, dimensions: { FunctionName: "${aws_lambda_function.#{@name}_lambda.function_name}" }, alarm_actions: [ "${aws_sns_topic.#{@name}_lambda_cloudwatch_topic.arn}" ], ok_actions: [ "${aws_sns_topic.#{@name}_lambda_cloudwatch_topic.arn}" ] } subscription = resource :aws_sns_topic_subscription, "#{@name}_lambda_cloudwatch_subscription", { topic_arn: "${aws_sns_topic.#{@name}_lambda_cloudwatch_topic.arn}", protocol: @renew_alert_options[:protocol], endpoint: @renew_alert_options[:endpoint], endpoint_auto_confirms: @renew_alert_options[:endpoint_auto_confirms], confirmation_timeout_in_minutes: @renew_alert_options[:confirmation_timeout_in_minutes], raw_message_delivery: @renew_alert_options[:raw_message_delivery], filter_policy: @renew_alert_options[:filter_policy], delivery_policy: @renew_alert_options[:delivery_policy] } self end |
#setup_providers ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/terrafying/components/letsencrypt.rb', line 26 def setup_providers { staging: { url: 'https://acme-staging-v02.api.letsencrypt.org/directory', ref: provider(:acme, alias: :staging, server_url: 'https://acme-staging-v02.api.letsencrypt.org/directory'), ca_cert: 'https://letsencrypt.org/certs/fakeleintermediatex1.pem' }, live: { url: 'https://acme-v02.api.letsencrypt.org/directory', ref: provider(:acme, alias: :live, server_url: 'https://acme-v02.api.letsencrypt.org/directory'), ca_cert: 'https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem.txt' } } end |