Skip to content

Commit de5cd09

Browse files
committed
Add ACME support for lba commands
Passing a list of comma separated domains to either `lbs create` or `lbs update` will request that domains are provisioned from Let's Encrypt. For this to work a `https` (not `tcp`) listener needs to be available on port 443 otherwise the request will be rejected. brightbox lbs create --acme-domains domain.example --listeners 443:443:https:5000 srv-12345 Domains and their status are listed under the `lbs show` command output.
1 parent c8b27ca commit de5cd09

File tree

7 files changed

+127
-0
lines changed

7 files changed

+127
-0
lines changed

lib/brightbox-cli/commands/lbs/create.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ module Brightbox
4444
c.default_value "3"
4545
c.flag [:d, "hc-down"]
4646

47+
c.desc "ACME domains"
48+
c.flag ["acme_domains"]
49+
4750
c.desc "Filepath to the SSL certificate file to use."
4851
c.flag ["ssl-cert"]
4952

@@ -82,6 +85,10 @@ module Brightbox
8285
options[:b] = options[:b].to_i
8386
end
8487

88+
if options["acme_domains"]
89+
options["acme_domains"] = options["acme_domains"].split(",")
90+
end
91+
8592
hc_arg_lookup = {
8693
:k => :port,
8794
:y => :type,
@@ -118,6 +125,7 @@ module Brightbox
118125
msg = "Creating a new load balancer"
119126
info msg
120127
lb = LoadBalancer.create(
128+
domains: options["acme_domains"],
121129
buffer_size: options[:b],
122130
certificate_pem: ssl_cert,
123131
certificate_private_key: ssl_key,

lib/brightbox-cli/commands/lbs/show.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Brightbox
1616
created_at
1717
deleted_at
1818
policy
19+
acme_domains
1920
ssl_minimum_version
2021
ssl_issuer
2122
ssl_subject

lib/brightbox-cli/commands/lbs/update.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ module Brightbox
3737
c.desc "Healthcheck threshold down. Number of failed healthchecks for the node to be considered down."
3838
c.flag [:d, "hc-down"]
3939

40+
c.desc "ACME domains"
41+
c.flag ["acme_domains"]
42+
4043
c.desc "Filepath to the SSL certificate file to use."
4144
c.flag ["ssl-cert"]
4245

@@ -132,6 +135,10 @@ module Brightbox
132135
lbopts[:ssl_minimum_version] = options["ssl-min-ver"]
133136
end
134137

138+
if options["acme_domains"]
139+
lbopts[:domains] = options["acme_domains"].split(",")
140+
end
141+
135142
lbopts.nilify_blanks
136143

137144
lb = LoadBalancer.find lb_id

lib/brightbox-cli/load_balancers.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,26 @@ def self.create(options)
99
new(conn.load_balancers.create(options))
1010
end
1111

12+
def acme_domains
13+
if attributes["acme"]
14+
attributes["acme"]["domains"].map do |domain|
15+
[domain["identifier"], domain["status"]].join(":")
16+
end.join(",")
17+
else
18+
[]
19+
end
20+
rescue StandardError
21+
[]
22+
end
23+
1224
def attributes
1325
fog_model.attributes
1426
end
1527

1628
def to_row
1729
attributes.merge(
1830
:locked => locked?,
31+
:acme_domains => acme_domains,
1932
:ssl_minimum_version => ssl_minimum_version,
2033
:ssl_issuer => certificate_issuer,
2134
:ssl_subject => certificate_subject,

spec/commands/lbs/create_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,46 @@
169169
expect(stdout).to include("lba-12345")
170170
end
171171
end
172+
173+
context "--acme-domains=example.com" do
174+
let(:argv) { ["lbs", "create", "--acme-domains", "example.com", "--listeners", "443:443:https:5000", "lba-12345"] }
175+
let(:json_response) do
176+
<<~EOS
177+
{
178+
"id":"lba-12345",
179+
"acme": {
180+
"domains": [
181+
{
182+
"identifier": "example.com",
183+
"last_message": null,
184+
"status": "pending"
185+
}
186+
]
187+
},
188+
"certificate": null,
189+
"listeners": [
190+
{
191+
"in": 443,
192+
"out": 443,
193+
"protocol": "https",
194+
"proxy_protocol": null,
195+
"timeout": 5000
196+
}
197+
]
198+
}
199+
EOS
200+
end
201+
202+
before do
203+
stub_request(:post, "http://api.brightbox.localhost/1.0/load_balancers?account_id=acc-12345")
204+
.with(body: hash_including(domains: ["example.com"]))
205+
.to_return(:status => 202, :body => json_response)
206+
end
207+
208+
it "includes acme_certificate_domain in response" do
209+
expect(stderr).to eq("Creating a new load balancer\n")
210+
expect(stdout).to include("lba-12345")
211+
end
212+
end
172213
end
173214
end

spec/commands/lbs/show_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@
5858
"name":"app-lb1",
5959
"status":"active",
6060
"created_at":"2012-03-05T12:00:00Z",
61+
"acme": {
62+
"domains": [
63+
{
64+
"identifier": "domain.test",
65+
"status": "verified"
66+
},
67+
{
68+
"identifier": "domain2.test",
69+
"status": "verified"
70+
}
71+
]
72+
},
6173
"nodes":[
6274
{
6375
"id":"srv-12345",
@@ -81,6 +93,7 @@
8193
expect(stdout).to include("name: app-lb1")
8294
expect(stdout).to include("created_at: 2012-03-05T12:00Z")
8395
expect(stdout).to include("nodes: srv-12345")
96+
expect(stdout).to include("acme_domains: domain.test:verified,domain2.test:verified")
8497
end
8598
end
8699
end

spec/commands/lbs/update_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,49 @@
9999
expect(stdout).to include("lba-kl432")
100100
end
101101
end
102+
103+
context "--acme-domains=example.com" do
104+
let(:argv) { ["lbs", "update", "--acme-domains", "example.com", "--listeners", "443:443:https:5000", "lba-12345"] }
105+
let(:json_response) do
106+
<<~EOS
107+
{
108+
"id":"lba-12345",
109+
"acme": {
110+
"domains": [
111+
{
112+
"identifier": "example.com",
113+
"last_message": null,
114+
"status": "pending"
115+
}
116+
]
117+
},
118+
"certificate": null,
119+
"listeners": [
120+
{
121+
"in": 443,
122+
"out": 443,
123+
"protocol": "https",
124+
"proxy_protocol": null,
125+
"timeout": 5000
126+
}
127+
]
128+
}
129+
EOS
130+
end
131+
132+
before do
133+
stub_request(:get, "http://api.brightbox.localhost/1.0/load_balancers/lba-12345?account_id=acc-12345")
134+
.to_return(:status => 200, :body => '{"id":"lba-12345"}')
135+
136+
stub_request(:put, "http://api.brightbox.localhost/1.0/load_balancers/lba-12345?account_id=acc-12345")
137+
.with(body: hash_including(domains: ["example.com"]))
138+
.to_return(:status => 202, :body => json_response)
139+
end
140+
141+
it "includes acme_certificate_domain in response" do
142+
expect(stderr).to eq("Updating load balancer lba-12345\n")
143+
expect(stdout).to include("lba-12345")
144+
end
145+
end
102146
end
103147
end

0 commit comments

Comments
 (0)