Skip to content

Commit

Permalink
fix(lb): handle support for valid custom certificates (scaleway#971)
Browse files Browse the repository at this point in the history
* fix(k8s): cassette

* feat(lb): create-certificate script to wrap the ACME client on letsencrypt and scaleway provider

* feat(lb): add wait to certificate creation

* feat(lb): remove custom test on lb

* feat(lb): remove .lego directory

* feat(lb): remove custom certificate cassette

* feat(lb): add new certificate test

* feat(lb): update cassette
  • Loading branch information
Monitob authored Dec 21, 2021
1 parent 2acdb2b commit 5c81757
Show file tree
Hide file tree
Showing 5 changed files with 1,532 additions and 323 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ website/vendor
dist/

terraform-provider-scaleway

# Lego ACME client
.lego/
92 changes: 91 additions & 1 deletion scaleway/resource_lb_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,33 @@ func resourceScalewayLbCertificateCreate(ctx context.Context, d *schema.Resource
return diag.FromErr(errors.New("you need to define either letsencrypt or custom_certificate configuration"))
}

retryInterval := DefaultWaitLBRetryInterval
_, err = lbAPI.WaitForLb(&lb.ZonedAPIWaitForLBRequest{
Zone: zone,
LBID: lbID,
Timeout: scw.TimeDurationPtr(defaultInstanceServerWaitTimeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
// check err waiting process
if err != nil {
return diag.FromErr(err)
}

res, err := lbAPI.CreateCertificate(createReq, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

_, err = lbAPI.WaitForLb(&lb.ZonedAPIWaitForLBRequest{
Zone: zone,
LBID: lbID,
Timeout: scw.TimeDurationPtr(defaultInstanceServerWaitTimeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

d.SetId(newZonedIDString(zone, res.ID))

return resourceScalewayLbCertificateRead(ctx, d, meta)
Expand All @@ -169,6 +191,17 @@ func resourceScalewayLbCertificateRead(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(err)
}

retryInterval := DefaultWaitLBRetryInterval
_, err = lbAPI.WaitForLb(&lb.ZonedAPIWaitForLBRequest{
Zone: zone,
LBID: res.LB.ID,
Timeout: scw.TimeDurationPtr(defaultInstanceServerWaitTimeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

_ = d.Set("lb_id", newZonedIDString(zone, res.LB.ID))
_ = d.Set("name", res.Name)
_ = d.Set("common_name", res.CommonName)
Expand All @@ -186,6 +219,25 @@ func resourceScalewayLbCertificateUpdate(ctx context.Context, d *schema.Resource
return diag.FromErr(err)
}

cert, err := lbAPI.GetCertificate(&lb.ZonedAPIGetCertificateRequest{
CertificateID: ID,
Zone: zone,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

retryInterval := DefaultWaitLBRetryInterval
_, err = lbAPI.WaitForLb(&lb.ZonedAPIWaitForLBRequest{
Zone: zone,
LBID: cert.LB.ID,
Timeout: scw.TimeDurationPtr(defaultInstanceServerWaitTimeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

req := &lb.ZonedAPIUpdateCertificateRequest{
CertificateID: ID,
Zone: zone,
Expand All @@ -197,6 +249,16 @@ func resourceScalewayLbCertificateUpdate(ctx context.Context, d *schema.Resource
return diag.FromErr(err)
}

_, err = lbAPI.WaitForLb(&lb.ZonedAPIWaitForLBRequest{
Zone: zone,
LBID: cert.LB.ID,
Timeout: scw.TimeDurationPtr(defaultInstanceServerWaitTimeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

return resourceScalewayLbCertificateRead(ctx, d, meta)
}

Expand All @@ -206,14 +268,42 @@ func resourceScalewayLbCertificateDelete(ctx context.Context, d *schema.Resource
return diag.FromErr(err)
}

cert, err := lbAPI.GetCertificate(&lb.ZonedAPIGetCertificateRequest{
CertificateID: ID,
Zone: zone,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

retryInterval := DefaultWaitLBRetryInterval
_, err = lbAPI.WaitForLb(&lb.ZonedAPIWaitForLBRequest{
Zone: zone,
LBID: cert.LB.ID,
Timeout: scw.TimeDurationPtr(defaultInstanceServerWaitTimeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

err = lbAPI.DeleteCertificate(&lb.ZonedAPIDeleteCertificateRequest{
Zone: zone,
CertificateID: ID,
}, scw.WithContext(ctx))

if err != nil && !is404Error(err) {
return diag.FromErr(err)
}

_, err = lbAPI.WaitForLb(&lb.ZonedAPIWaitForLBRequest{
Zone: zone,
LBID: cert.LB.ID,
Timeout: scw.TimeDurationPtr(defaultInstanceServerWaitTimeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

return nil
}
109 changes: 90 additions & 19 deletions scaleway/resource_lb_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,97 @@ func TestAccScalewayLbCertificate_Basic(t *testing.T) {
custom_certificate {
certificate_chain = <<EOF
-----BEGIN CERTIFICATE-----
MIIDXzCCAkegAwIBAgIUCQTOZjf11LGYmvGVw7xxWurGFgMwDQYJKoZIhvcNAQEL
BQAwSDELMAkGA1UEBhMCRlIxDjAMBgNVBAcMBVBhcmlzMREwDwYDVQQKDAhTY2Fs
ZXdhdTEWMBQGA1UEAwwNKi5leGFtcGxlLmNvbTAeFw0yMDA0MjgxNzQ2MzFaFw0y
MjA0MjgxNzQ2MzFaMEgxCzAJBgNVBAYTAkZSMQ4wDAYDVQQHDAVQYXJpczERMA8G
A1UECgwIU2NhbGV3YXUxFjAUBgNVBAMMDSouZXhhbXBsZS5jb20wggEiMA0GCSqG
SIb3DQEBAQUAA4IBDwAwggEKAoIBAQCws8az+MXTtmNKN66Bl2HmlkAxazMcK9eG
rmU5jOs3nNZevjn4FhQClVt2mOX7G37b32wpLyt5MUYGg+Ac95fL5zx9nwp57YBx
NteGYIUpLTD2tb3aKuuIM+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MAgGBmeBDAECATA/BgsrBgEEAYLfEwEB
ATAwMC4GCCsGAQUFBwIBFiJodHRwOi8vY3BzLnJvb3QteDEubGV0c2VuY3J5cHQu
b3JnMDwGA1UdHwQ1MDMwMaAvoC2GK2h0dHA6Ly9jcmwuaWRlbnRydXN0LmNvbS9E
U1RST09UQ0FYM0NSTC5jcmwwHQYDVR0OBBYEFHm0WeZ7tuXkAXOACIjIGlj26Ztu
MA0GCSqGSIb3DQEBCwUAA4IBAQAKcwBslm7/DlLQrt2M51oGrS+o44+/yQoDFVDC
5WxCu2+b9LRPwkSICHXM6webFGJueN7sJ7o5XPWioW5WlHAQU7G75K/QosMrAdSW
9MUgNTP52GE24HGNtLi1qoJFlcDyqSMo59ahy2cI2qBDLKobkx/J3vWraV0T9VuG
WCLKTVXkcGdtwlfFRjlBz4pYg1htmf5X6DYO8A4jqv2Il9DjXA6USbW1FzXSLr9O
he8Y4IWS6wY7bCkjCWDcRQJMEhg76fsO3txE+FiYruq9RUWhiF1myv4Q6W+CyBFC
Dfvp7OOGAN6dEOM4+qR9sdjoSYKEBpsr6GtPAQw4dy753ec5
-----END CERTIFICATE-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEII45PLJDlsxyXYk55ladXnPUQOwYphEOy3Z3qlt5EoRBoAoGCCqGSM49
AwEHoUQDQgAE+q2/yTMlbZAjcsb3dJxLXdJKblIgkja4CgiGfLguPTqDTl9/uYyC
Z35eAgPo6ZY4H49qyjdR2rqFxwRQ39RGUg==
-----END EC PRIVATE KEY-----
EOF
}
}
Expand Down
Loading

0 comments on commit 5c81757

Please sign in to comment.