Skip to content

Commit c649c0d

Browse files
authored
Merge pull request #139 from katrinpolit/custom_certificate
Customer certificate fix
2 parents 22d4f1a + 47ec5ce commit c649c0d

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.3.4 (Not Released)
2+
3+
* Fix bug in Custom Certificate resource
4+
15
## 3.3.3 (Released)
26

37
* Fix pagination bug in sub-account resource

GNUmakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ NAMESPACE=terraform-providers
66
PKG_NAME=incapsula
77
BINARY=terraform-provider-${PKG_NAME}
88
# Whenever bumping provider version, please update the version in incapsula/client.go (line 27) as well.
9-
VERSION=3.3.3
9+
VERSION=3.3.4
10+
1011

1112
OS_ARCH=darwin_amd64
1213
# OS_ARCH=linux_amd64

incapsula/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Client struct {
2525
func NewClient(config *Config) *Client {
2626
client := &http.Client{}
2727

28-
return &Client{config: config, httpClient: client, providerVersion: "3.3.3"}
28+
return &Client{config: config, httpClient: client, providerVersion: "3.3.4"}
2929
}
3030

3131
// Verify checks the API credentials

incapsula/client_certificate.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,33 @@ type CertificateAddResponse struct {
2323
// CertificateListResponse contains site object with details of custom certificate
2424
type CertificateListResponse struct {
2525
Res int `json:"res"`
26+
SSL SSL `json:"ssl"`
2627
}
2728

2829
// CertificateEditResponse contains confirmation of successful upload of certificate
2930
type CertificateEditResponse struct {
3031
Res int `json:"res"`
3132
ResMessage string `json:"res_message"`
33+
SSL SSL `json:"ssl"`
34+
}
35+
36+
type SSL struct {
37+
CustomCertificate CustomCertificate `json:"custom_certificate"`
38+
}
39+
40+
type CustomCertificate struct {
41+
InputHash string `json:"inputHash"`
3242
}
3343

3444
// AddCertificate adds a custom SSL certificate to a site in Incapsula
35-
func (c *Client) AddCertificate(siteID, certificate, privateKey, passphrase string) (*CertificateAddResponse, error) {
45+
func (c *Client) AddCertificate(siteID, certificate, privateKey, passphrase, inputHash string) (*CertificateAddResponse, error) {
3646

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

3949
values := url.Values{
4050
"site_id": {siteID},
4151
"certificate": {certificate},
52+
"input_hash": {inputHash},
4253
}
4354

4455
if privateKey != "" {
@@ -112,13 +123,14 @@ func (c *Client) ListCertificates(siteID string) (*CertificateListResponse, erro
112123
}
113124

114125
// EditCertificate updates the custom certifiacte on an Incapsula site
115-
func (c *Client) EditCertificate(siteID, certificate, privateKey, passphrase string) (*CertificateEditResponse, error) {
126+
func (c *Client) EditCertificate(siteID, certificate, privateKey, passphrase, inputHash string) (*CertificateEditResponse, error) {
116127

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

119130
values := url.Values{
120131
"site_id": {siteID},
121132
"certificate": {certificate},
133+
"input_hash": {inputHash},
122134
}
123135

124136
if privateKey != "" {

incapsula/resource_certificate.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package incapsula
22

33
import (
4-
"log"
5-
4+
"crypto/sha1"
5+
"encoding/hex"
66
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"log"
78
)
89

910
func resourceCertificate() *schema.Resource {
@@ -45,18 +46,31 @@ func resourceCertificate() *schema.Resource {
4546
Optional: true,
4647
Sensitive: true,
4748
},
49+
"input_hash": {
50+
Description: "inputHash",
51+
Type: schema.TypeString,
52+
Optional: true,
53+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
54+
newHash := createHash(d)
55+
if newHash == old {
56+
return true
57+
}
58+
return false
59+
},
60+
},
4861
},
4962
}
5063
}
5164

5265
func resourceCertificateCreate(d *schema.ResourceData, m interface{}) error {
5366
client := m.(*Client)
54-
67+
inputHash := createHash(d)
5568
_, err := client.AddCertificate(
5669
d.Get("site_id").(string),
5770
d.Get("certificate").(string),
5871
d.Get("private_key").(string),
5972
d.Get("passphrase").(string),
73+
inputHash,
6074
)
6175

6276
if err != nil {
@@ -89,6 +103,7 @@ func resourceCertificateRead(d *schema.ResourceData, m interface{}) error {
89103
return err
90104
}
91105

106+
d.Set("input_hash", listCertificatesResponse.SSL.CustomCertificate.InputHash)
92107
d.SetId("12345")
93108

94109
return nil
@@ -97,20 +112,22 @@ func resourceCertificateRead(d *schema.ResourceData, m interface{}) error {
97112
func resourceCertificateUpdate(d *schema.ResourceData, m interface{}) error {
98113
client := m.(*Client)
99114

115+
inputHash := createHash(d)
116+
100117
_, err := client.EditCertificate(
101118
d.Get("site_id").(string),
102119
d.Get("certificate").(string),
103120
d.Get("private_key").(string),
104121
d.Get("passphrase").(string),
122+
inputHash,
105123
)
106124

107125
if err != nil {
108126
return err
109127
}
110128

111129
d.SetId("12345")
112-
113-
return nil
130+
return resourceCertificateRead(d, m)
114131
}
115132

116133
func resourceCertificateDelete(d *schema.ResourceData, m interface{}) error {
@@ -128,3 +145,16 @@ func resourceCertificateDelete(d *schema.ResourceData, m interface{}) error {
128145

129146
return nil
130147
}
148+
149+
func createHash(d *schema.ResourceData) string {
150+
h := sha1.New()
151+
152+
certificate := d.Get("certificate").(string)
153+
passphrase := d.Get("passphrase").(string)
154+
privateKey := d.Get("private_key").(string)
155+
stringForHash := certificate + privateKey + passphrase
156+
h.Write([]byte(stringForHash))
157+
byteString := h.Sum(nil)
158+
result := hex.EncodeToString(byteString)
159+
return result
160+
}

website/docs/r/custom_certificate.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The following arguments are supported:
3030
* `certificate` - (Required) The certificate file in base64 format. You can use the Terraform HCL `file` directive to pull in the contents from a file. You can also inline the certificate in the configuration.
3131
* `private_key` - (Optional) The private key of the certificate in base64 format. Optional in case of PFX certificate file format.
3232
* `passphrase` - (Optional) The passphrase used to protect your SSL certificate.
33+
* `input_hash` - (Optional) Currently ignored. If terraform plan flags this field as changed, it means that any of: `certificate`, `private_key`, or `passphrase` has changed.
3334

3435
## Attributes Reference
3536

0 commit comments

Comments
 (0)