Skip to content

Commit 1dc6ffb

Browse files
authored
Merge pull request #121 from imperva/custom_certificate_fix
Custom certificate fix
2 parents e66ee4b + d7b6db1 commit 1dc6ffb

File tree

4 files changed

+14
-27
lines changed

4 files changed

+14
-27
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/client_policy_asset_association.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type PolicyAssetAssociationStatus struct {
1515

1616
// AddPolicyAssetAssociation adds a policy to be managed by Incapsula
1717
func (c *Client) AddPolicyAssetAssociation(policyID, assetID, assetType string) error {
18-
log.Printf("[INFO] Adding Incapsula Policy Asset Association: %s-%s-%s\n", policyID, assetID, assetType)
18+
log.Printf("[INFO] Adding Incapsula Policy Asset Association: %s/%s/%s\n", policyID, assetID, assetType)
1919

2020
// Post form to Incapsula
2121
reqURL := fmt.Sprintf("%s/policies/v2/assets/%s/%s/policies/%s", c.config.BaseURLAPI, assetType, assetID, policyID)
@@ -41,7 +41,7 @@ func (c *Client) AddPolicyAssetAssociation(policyID, assetID, assetType string)
4141

4242
// DeletePolicyAssetAssociation deletes a policy asset association currently managed by Incapsula
4343
func (c *Client) DeletePolicyAssetAssociation(policyID, assetID, assetType string) error {
44-
log.Printf("[INFO] Deleting Incapsula Policy Asset Association: %s-%s-%s\n", policyID, assetID, assetType)
44+
log.Printf("[INFO] Deleting Incapsula Policy Asset Association: %s/%s/%s\n", policyID, assetID, assetType)
4545

4646
// Delete request to Incapsula
4747
reqURL := fmt.Sprintf("%s/policies/v2/assets/%s/%s/policies/%s", c.config.BaseURLAPI, assetType, assetID, policyID)
@@ -66,32 +66,32 @@ func (c *Client) DeletePolicyAssetAssociation(policyID, assetID, assetType strin
6666
}
6767

6868
func (c *Client) isPolicyAssetAssociated(policyID, assetID, assetType string) (bool, error) {
69-
log.Printf("[INFO] Checking Policy Asset Association: %s-%s-%s\n", policyID, assetID, assetType)
69+
log.Printf("[INFO] Checking Policy Asset Association: %s/%s/%s\n", policyID, assetID, assetType)
7070

7171
// Check with Policies if the association exist
7272
reqURL := fmt.Sprintf("%s/policies/v2/policies/%s/assets/%s/%s", c.config.BaseURLAPI, policyID, assetType, assetID)
7373
resp, err := c.DoJsonRequestWithHeaders(http.MethodGet, reqURL, nil)
7474
if err != nil {
75-
return false, fmt.Errorf("error from Incapsula service when checking if Policy Asset Association exist: %s-%s-%s, err: %s", policyID, assetID, assetType, err)
75+
return false, fmt.Errorf("error from Incapsula service when checking if Policy Asset Association exist: %s/%s/%s, err: %s", policyID, assetID, assetType, err)
7676
}
7777

7878
// Read the body
7979
defer resp.Body.Close()
8080
responseBody, err := ioutil.ReadAll(resp.Body)
8181

82-
log.Printf("[DEBUG] Incapsula isPolicyAssetAssociated for: %s-%s-%s , response is: %s\n", policyID, assetID, assetType, string(responseBody))
82+
log.Printf("[DEBUG] Incapsula isPolicyAssetAssociated for: %s/%s/%s , response is: %s\n", policyID, assetID, assetType, string(responseBody))
8383

8484
// Check the response code
8585
// If policy asset is not associated 404 will be returned from policies
8686
if resp.StatusCode != 200 {
87-
return false, fmt.Errorf("Error status code %d from Incapsula service when checking the reading Policy Asset Association: %s-%s-%s, response is: %s", resp.StatusCode, policyID, assetID, assetType, string(responseBody))
87+
return false, fmt.Errorf("Error status code %d from Incapsula service when checking the reading Policy Asset Association: %s/%s/%s, response is: %s", resp.StatusCode, policyID, assetID, assetType, string(responseBody))
8888
}
8989

9090
// Parse the JSON
9191
var policyAssetAssociationStatus PolicyAssetAssociationStatus
9292
err = json.Unmarshal([]byte(responseBody), &policyAssetAssociationStatus)
9393
if err != nil {
94-
return false, fmt.Errorf("error parsing Policy Asset Association JSON response for Policy Asset Association: %d-%s-%s: %s\nresponse: %s, err: %s", resp.StatusCode, policyID, assetID, assetType, err, string(responseBody))
94+
return false, fmt.Errorf("error parsing Policy Asset Association JSON response for Policy Asset Association: %d/%s/%s: %s\nresponse: %s, err: %s", resp.StatusCode, policyID, assetID, assetType, err, string(responseBody))
9595
}
9696

9797
return true, nil

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)