Class: FastlyCTL::TLSManagedSubCmd

Inherits:
SubCommandBase show all
Defined in:
lib/fastlyctl/commands/tls/managed.rb

Constant Summary collapse

SubcommandPrefix =
"tls managed"
DomainRegex =
/(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/

Instance Method Summary collapse

Methods inherited from SubCommandBase

banner

Instance Method Details

#challenges(domain) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fastlyctl/commands/tls/managed.rb', line 68

def challenges(domain)
  abort "Must specify valid domain name" unless domain =~ DomainRegex

  domains = FastlyCTL::Fetcher.api_request(:get,"/tls/domains?include=tls_subscriptions.tls_authorizations", {
    use_vnd: true
  })

  tls_authorizations = FastlyCTL::Utils.filter_vnd(domains["included"],"tls_authorization")

  tls_authorizations.each do |tls_authorization|
    tls_authorization["attributes"]["challenges"].each do |challenge|
      if challenge["record_name"] == domain
        FastlyCTL::TLSUtils.print_challenges(tls_authorization)
        abort
      end
    end
  end

  say("#{domain} not found in domain list.")
end

#create(domain) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fastlyctl/commands/tls/managed.rb', line 7

def create(domain)
  abort "Must specify valid domain name" unless domain =~ DomainRegex

  tls_configs = FastlyCTL::TLSUtils.get_tls_configs
  tls_config = FastlyCTL::TLSUtils.select_tls_config(tls_configs)

  payload = {
    data: {
      type: "tls_subscription",
      attributes: {
        certificate_authority: "lets-encrypt"
      },
      relationships: {
        tls_domains: {
          data: [
            {
              type: "tls_domain",
              id: domain
            }
          ]
        },
        tls_configuration: {
          data: {
            type: "tls_configuration",
            id: tls_config["id"]
          }
        }
      }
    }
  }

  subscription = FastlyCTL::Fetcher.api_request(:post,"/tls/subscriptions", {
    body: payload.to_json,
    use_vnd: true
  })

  tls_authorization = FastlyCTL::Utils.filter_vnd(subscription["included"],"tls_authorization")
  abort "Unable to fetch TLS Authorization for the domain." unless tls_authorization.length > 0
  FastlyCTL::TLSUtils.print_challenges(tls_authorization[0])
end

#delete(domain) ⇒ Object



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
# File 'lib/fastlyctl/commands/tls/managed.rb', line 90

def delete(domain)
  abort "Must specify valid domain name" unless domain =~ DomainRegex

  activation = FastlyCTL::Fetcher.api_request(:get,"/tls/activations?filter[tls_domain.id]=#{domain}", {use_vnd: true})

  if activation["data"].length >= 1
    say("TLS is currently active for #{domain}. If you proceed, Fastly will no longer be able to serve TLS requests to clients for #{domain}.")
    answer = ask("Please type the name of the domain to confirm deactivation and deletion of the Fastly Managed TLS subscription: ")
    abort "Supplied domain does not match the domain requested for deletion--aborting." unless answer == domain

    FastlyCTL::Fetcher.api_request(:delete,"/tls/activations/#{activation["data"][0]["id"]}",{use_vnd:true})
  end

  subscriptions = FastlyCTL::Fetcher.api_request(:get,"/tls/subscriptions", {
    use_vnd: true
  })

  subscriptions["data"].each do |subscription|
    next unless subscription["relationships"]["tls_domains"]["data"][0]["id"] == domain 
    
    FastlyCTL::Fetcher.api_request(:delete,"/tls/subscriptions/#{subscription["id"]}",{use_vnd:true})
    
    say("TLS Subscription for #{domain} has been deleted.")
    abort
  end

  say("No TLS Subscription found for #{domain}...")
end

#statusObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fastlyctl/commands/tls/managed.rb', line 49

def status
  subscriptions = FastlyCTL::Fetcher.api_request(:get,"/tls/subscriptions", {
    use_vnd: true
  })

  if subscriptions["data"].length == 0
    say("No Fastly Managed TLS Subscriptions found.")
    abort
  end

  subscriptions["data"].each do |subscription|
    output = subscription["relationships"]["tls_domains"]["data"][0]["id"]
    output += " - " + subscription["attributes"]["certificate_authority"]
    output += " - " + subscription["attributes"]["state"]
    say(output)
  end
end