Skip to content

Commit d7b6db1

Browse files
committed
fix custom certificate resource: remove force_new from certificate, private_key and passphrase attributes + add base64 method usage to the example
1 parent c457efb commit d7b6db1

File tree

3 files changed

+7
-20
lines changed

3 files changed

+7
-20
lines changed

incapsula/client_certificate.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package incapsula
22

33
import (
4-
"encoding/base64"
54
"encoding/json"
65
"fmt"
76
"io/ioutil"
87
"log"
98
"net/url"
10-
"strings"
119
)
1210

1311
// Endpoints (unexported consts)
@@ -35,12 +33,6 @@ type CertificateEditResponse struct {
3533

3634
// AddCertificate adds a custom SSL certificate to a site in Incapsula
3735
func (c *Client) AddCertificate(siteID, certificate, privateKey, passphrase string) (*CertificateAddResponse, error) {
38-
certificate = strings.TrimSpace(certificate)
39-
_, err := base64.StdEncoding.DecodeString(certificate)
40-
if err != nil {
41-
// This is not a valid base64 encoded string
42-
certificate = base64.StdEncoding.EncodeToString([]byte(certificate))
43-
}
4436

4537
log.Printf("[INFO] Adding custom certificate for site_id: %s", siteID)
4638

@@ -50,8 +42,7 @@ func (c *Client) AddCertificate(siteID, certificate, privateKey, passphrase stri
5042
}
5143

5244
if privateKey != "" {
53-
b64PrivateKey := base64.StdEncoding.EncodeToString([]byte(strings.TrimSpace(privateKey)))
54-
values.Set("private_key", b64PrivateKey)
45+
values.Set("private_key", privateKey)
5546
}
5647
if passphrase != "" {
5748
values.Set("passphrase", passphrase)
@@ -122,18 +113,17 @@ func (c *Client) ListCertificates(siteID string) (*CertificateListResponse, erro
122113

123114
// EditCertificate updates the custom certifiacte on an Incapsula site
124115
func (c *Client) EditCertificate(siteID, certificate, privateKey, passphrase string) (*CertificateEditResponse, error) {
125-
b64Certificate := base64.StdEncoding.EncodeToString([]byte(strings.TrimSpace(certificate)))
126116

127117
log.Printf("[INFO] Editing custom certificate for Incapsula site_id: %s\n", siteID)
128118

129119
values := url.Values{
130120
"site_id": {siteID},
131-
"certificate": {b64Certificate},
121+
"certificate": {certificate},
132122
}
133123

134124
if privateKey != "" {
135-
b64PrivateKey := base64.StdEncoding.EncodeToString([]byte(strings.TrimSpace(privateKey)))
136-
values.Set("private_key", b64PrivateKey)
125+
values.Set("private_key", privateKey)
126+
137127
}
138128
if passphrase != "" {
139129
values.Set("passphrase", passphrase)

incapsula/resource_certificate.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func resourceCertificate() *schema.Resource {
1010
return &schema.Resource{
1111
Create: resourceCertificateCreate,
1212
Read: resourceCertificateRead,
13-
Update: nil,
13+
Update: resourceCertificateUpdate,
1414
Delete: resourceCertificateDelete,
1515
Importer: &schema.ResourceImporter{
1616
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
@@ -31,21 +31,18 @@ func resourceCertificate() *schema.Resource {
3131
Description: "The certificate file in base64 format.",
3232
Type: schema.TypeString,
3333
Required: true,
34-
ForceNew: true,
3534
},
3635
// Optional Arguments
3736
"private_key": {
3837
Description: "The private key of the certificate in base64 format. Optional in case of PFX certificate file format. This will be encoded in sha256 in terraform state.",
3938
Type: schema.TypeString,
4039
Optional: true,
41-
ForceNew: true,
4240
Sensitive: true,
4341
},
4442
"passphrase": {
4543
Description: "The passphrase used to protect your SSL certificate. This will be encoded in sha256 in terraform state.",
4644
Type: schema.TypeString,
4745
Optional: true,
48-
ForceNew: true,
4946
Sensitive: true,
5047
},
5148
},

website/docs/r/custom_certificate.html.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Custom certificates must be one of the following formats: PFX, PEM, or CER.
1616
```hcl
1717
resource "incapsula_custom_certificate" "custom-certificate" {
1818
site_id = incapsula_site.example-site.id
19-
certificate = "${file("path/to/your/cert.crt")}"
20-
private_key = "${file("path/to/your/private_key.key")}"
19+
certificate = filebase64("${"path/to/your/cert.crt"}")
20+
private_key = filebase64("${"path/to/your/private_key.key"}")
2121
passphrase = "yourpassphrase"
2222
}
2323
```

0 commit comments

Comments
 (0)