Skip to content

Commit 819cd6e

Browse files
author
robertlestak
committed
rename incapsula store to imperva, maintaining backwards compatibility
1 parent d114650 commit 819cd6e

File tree

6 files changed

+85
-44
lines changed

6 files changed

+85
-44
lines changed

deploy/cert-manager-sync/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ type: application
1515
# This is the chart version. This version number should be incremented each time you make changes
1616
# to the chart and its templates, including the app version.
1717
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18-
version: 1.3.2
18+
version: 1.4.0
1919

2020
# This is the version number of the application being deployed. This version number should be
2121
# incremented each time you make changes to the application. Versions are not expected to
2222
# follow Semantic Versioning. They should reflect the version the application is using.
2323
# It is recommended to use it with quotes.
24-
appVersion: "1.3.2"
24+
appVersion: "1.4.0"

internal/types/types.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const (
1616
GCPStoreType StoreType = "gcp"
1717
HerokuStoreType StoreType = "heroku"
1818
HetznerCloudStoreType StoreType = "hetznercloud"
19-
IncapsulaStoreType StoreType = "incapsula"
19+
ImpervaStoreType StoreType = "imperva"
20+
IncapsulaStoreType StoreType = "incapsula" // Deprecated: Use ImpervaStoreType
2021
ThreatxStoreType StoreType = "threatx"
2122
VaultStoreType StoreType = "vault"
2223
)
@@ -29,7 +30,8 @@ var EnabledStores = []StoreType{
2930
GCPStoreType,
3031
HerokuStoreType,
3132
HetznerCloudStoreType,
32-
IncapsulaStoreType,
33+
ImpervaStoreType,
34+
IncapsulaStoreType, // Backwards compatibility
3335
ThreatxStoreType,
3436
VaultStoreType,
3537
}

pkg/certmanagersync/certmanagersync.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/robertlestak/cert-manager-sync/stores/gcpcm"
1919
"github.com/robertlestak/cert-manager-sync/stores/heroku"
2020
"github.com/robertlestak/cert-manager-sync/stores/hetznercloud"
21-
"github.com/robertlestak/cert-manager-sync/stores/incapsula"
21+
"github.com/robertlestak/cert-manager-sync/stores/imperva"
2222
"github.com/robertlestak/cert-manager-sync/stores/threatx"
2323
"github.com/robertlestak/cert-manager-sync/stores/vault"
2424
log "github.com/sirupsen/logrus"
@@ -53,8 +53,8 @@ func NewStore(storeType cmtypes.StoreType) (RemoteStore, error) {
5353
store = &heroku.HerokuStore{}
5454
case cmtypes.HetznerCloudStoreType:
5555
store = &hetznercloud.HetznerCloudStore{}
56-
case cmtypes.IncapsulaStoreType:
57-
store = &incapsula.IncapsulaStore{}
56+
case cmtypes.ImpervaStoreType, cmtypes.IncapsulaStoreType:
57+
store = &imperva.ImpervaStore{}
5858
case cmtypes.ThreatxStoreType:
5959
store = &threatx.ThreatXStore{}
6060
case cmtypes.VaultStoreType:

pkg/certmanagersync/certmanagersync_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func TestNewStore(t *testing.T) {
5454
storeType: cmtypes.IncapsulaStoreType,
5555
wantErr: false,
5656
},
57+
{
58+
name: "Test ImpervaStoreType",
59+
storeType: cmtypes.ImpervaStoreType,
60+
wantErr: false,
61+
},
5762
{
5863
name: "Test ThreatxStoreType",
5964
storeType: cmtypes.ThreatxStoreType,

pkg/tlssecret/config_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ func secretStoresMetaEqual(m1, m2 map[string][]map[string]string) bool {
3232
if len(v1) != len(v2) {
3333
return false
3434
}
35-
for i := range v1 {
36-
if !mapsEqual(v1[i], v2[i]) {
35+
// Check that all maps in v1 exist in v2 (order-independent)
36+
for _, m1Map := range v1 {
37+
found := false
38+
for _, m2Map := range v2 {
39+
if mapsEqual(m1Map, m2Map) {
40+
found = true
41+
break
42+
}
43+
}
44+
if !found {
3745
return false
3846
}
3947
}
@@ -80,6 +88,32 @@ func TestGetSecretStoresMeta(t *testing.T) {
8088
},
8189
},
8290
},
91+
{
92+
name: "Test with imperva annotations",
93+
annotations: map[string]string{
94+
state.OperatorName + "/imperva-site-id": "12345",
95+
state.OperatorName + "/imperva-secret-name": "my-secret",
96+
},
97+
want: map[string][]map[string]string{
98+
"imperva": {
99+
{"site-id": "12345"},
100+
{"secret-name": "my-secret"},
101+
},
102+
},
103+
},
104+
{
105+
name: "Test with incapsula annotations (backwards compatibility)",
106+
annotations: map[string]string{
107+
state.OperatorName + "/incapsula-site-id": "67890",
108+
state.OperatorName + "/incapsula-secret-name": "old-secret",
109+
},
110+
want: map[string][]map[string]string{
111+
"incapsula": {
112+
{"site-id": "67890"},
113+
{"secret-name": "old-secret"},
114+
},
115+
},
116+
},
83117
}
84118

85119
for _, tt := range tests {
Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package incapsula
1+
package imperva
22

33
import (
44
"cmp"
@@ -17,7 +17,7 @@ import (
1717
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1818
)
1919

20-
type IncapsulaStore struct {
20+
type ImpervaStore struct {
2121
ID string `json:"api_id"`
2222
SiteID string `json:"site_id"`
2323
Key string `json:"api_key"`
@@ -26,21 +26,21 @@ type IncapsulaStore struct {
2626
SecretNamespace string
2727
}
2828

29-
func (s *IncapsulaStore) GetApiKey(ctx context.Context) error {
29+
func (s *ImpervaStore) GetApiKey(ctx context.Context) error {
3030
gopt := metav1.GetOptions{}
3131
if s.SecretName == "" {
3232
return fmt.Errorf("secret name not set")
3333
}
3434
sc, err := state.KubeClient.CoreV1().Secrets(s.SecretNamespace).Get(ctx, s.SecretName, gopt)
3535
if err != nil {
36-
return fmt.Errorf("failed to get Incapsula credentials secret %s/%s: %w", s.SecretNamespace, s.SecretName, err)
36+
return fmt.Errorf("failed to get Imperva credentials secret %s/%s: %w", s.SecretNamespace, s.SecretName, err)
3737
}
3838
s.ID = string(sc.Data["api_id"])
3939
s.Key = string(sc.Data["api_key"])
4040
return nil
4141
}
4242

43-
func (s *IncapsulaStore) FromConfig(c tlssecret.GenericSecretSyncConfig) error {
43+
func (s *ImpervaStore) FromConfig(c tlssecret.GenericSecretSyncConfig) error {
4444
l := log.WithFields(log.Fields{
4545
"action": "FromConfig",
4646
})
@@ -64,8 +64,8 @@ func (s *IncapsulaStore) FromConfig(c tlssecret.GenericSecretSyncConfig) error {
6464
return nil
6565
}
6666

67-
// Incapsula response contains the response from Incapsula API
68-
type IncapsulaResponse struct {
67+
// ImpervaResponse contains the response from Imperva API
68+
type ImpervaResponse struct {
6969
Res int `json:"res"`
7070
ResMessage string `json:"res_message"`
7171
}
@@ -77,15 +77,15 @@ type ImpervaCertUpload struct {
7777
AuthType string `json:"auth_type"`
7878
}
7979

80-
// UploadIncapsulaCert syncs a certificate with Incapsula site
81-
func (s *IncapsulaStore) UploadIncapsulaCert(cert *tlssecret.Certificate) error {
80+
// UploadImpervaCert syncs a certificate with Imperva site
81+
func (s *ImpervaStore) UploadImpervaCert(cert *tlssecret.Certificate) error {
8282
l := log.WithFields(
8383
log.Fields{
84-
"action": "UploadIncapsulaCert",
84+
"action": "UploadImpervaCert",
8585
"siteID": s.SiteID,
8686
},
8787
)
88-
l.Debugf("UploadIncapsulaCert")
88+
l.Debugf("UploadImpervaCert")
8989
var err error
9090
bCert := base64.StdEncoding.EncodeToString(cert.FullChain())
9191
bKey := base64.StdEncoding.EncodeToString(cert.Key)
@@ -99,56 +99,56 @@ func (s *IncapsulaStore) UploadIncapsulaCert(cert *tlssecret.Certificate) error
9999
jd, err := json.Marshal(up)
100100
if err != nil {
101101
l.WithError(err).Errorf("json.Marshal error")
102-
return fmt.Errorf("failed to marshal Incapsula certificate upload request: %w", err)
102+
return fmt.Errorf("failed to marshal Imperva certificate upload request: %w", err)
103103
}
104104
l.Debugf("url=%s data=%s", iurl, string(jd))
105105
req, rerr := http.NewRequest("PUT", iurl, strings.NewReader(string(jd)))
106106
if rerr != nil {
107107
l.WithError(rerr).Errorf("http.NewRequest error")
108-
return fmt.Errorf("failed to create Incapsula API request for site %s: %w", s.SiteID, rerr)
108+
return fmt.Errorf("failed to create Imperva API request for site %s: %w", s.SiteID, rerr)
109109
}
110110
req.Header.Set("x-api-id", s.ID)
111111
req.Header.Set("x-api-key", s.Key)
112112
req.Header.Set("Content-Type", "application/json")
113113
res, serr := c.Do(req)
114114
if serr != nil {
115115
l.WithError(serr).Errorf("c.Do error=%v", serr)
116-
return fmt.Errorf("failed to upload certificate to Incapsula site %s: %w", s.SiteID, serr)
116+
return fmt.Errorf("failed to upload certificate to Imperva site %s: %w", s.SiteID, serr)
117117
}
118118
defer res.Body.Close()
119119
bd, berr := io.ReadAll(res.Body)
120120
if berr != nil {
121121
l.WithError(berr).Errorf("io.ReadAll error")
122-
return fmt.Errorf("failed to read Incapsula API response for site %s: %w", s.SiteID, berr)
122+
return fmt.Errorf("failed to read Imperva API response for site %s: %w", s.SiteID, berr)
123123
}
124124
if res.StatusCode != 200 {
125125
l.Debugf("status=%v body=%s", res.StatusCode, string(bd))
126-
return fmt.Errorf("incapsula upload failed for site %s (status: %d, secret: %s/%s): %s", s.SiteID, res.StatusCode, s.SecretNamespace, s.SecretName, string(bd))
126+
return fmt.Errorf("imperva upload failed for site %s (status: %d, secret: %s/%s): %s", s.SiteID, res.StatusCode, s.SecretNamespace, s.SecretName, string(bd))
127127
}
128-
ir := &IncapsulaResponse{}
128+
ir := &ImpervaResponse{}
129129
if err = json.Unmarshal(bd, ir); err != nil {
130130
l.WithError(err).Errorf("json.Unmarshal error")
131131
// debug dump the response
132132
l.Debugf("status=%v body=%s", res.StatusCode, string(bd))
133-
return fmt.Errorf("failed to parse Incapsula API response for site %s: %w", s.SiteID, err)
133+
return fmt.Errorf("failed to parse Imperva API response for site %s: %w", s.SiteID, err)
134134
}
135-
l.Debugf("incapsula statusCode=%d response=%v", res.StatusCode, string(bd))
135+
l.Debugf("imperva statusCode=%d response=%v", res.StatusCode, string(bd))
136136
if ir.Res != 0 {
137137
l.Debugf("status=%v body=%s", res.StatusCode, string(bd))
138-
return fmt.Errorf("incapsula upload failed for site %s (secret: %s/%s): %s", s.SiteID, s.SecretNamespace, s.SecretName, string(bd))
138+
return fmt.Errorf("imperva upload failed for site %s (secret: %s/%s): %s", s.SiteID, s.SecretNamespace, s.SecretName, string(bd))
139139
}
140-
l.Debugf("incapsula response=%v", string(bd))
140+
l.Debugf("imperva response=%v", string(bd))
141141
return err
142142
}
143143

144-
func (s *IncapsulaStore) GetIncapsulaSiteStatus() (string, error) {
144+
func (s *ImpervaStore) GetImpervaSiteStatus() (string, error) {
145145
l := log.WithFields(
146146
log.Fields{
147-
"action": "GetIncapsulaSiteStatus",
147+
"action": "GetImpervaSiteStatus",
148148
"siteID": s.SiteID,
149149
},
150150
)
151-
l.Debugf("GetIncapsulaSiteStatus")
151+
l.Debugf("GetImpervaSiteStatus")
152152
var err error
153153
iurl := "https://my.imperva.com/api/prov/v1/sites/status"
154154
c := http.Client{}
@@ -176,25 +176,25 @@ func (s *IncapsulaStore) GetIncapsulaSiteStatus() (string, error) {
176176
l.WithError(berr).Errorf("ioutil.ReadAll error")
177177
return "", berr
178178
}
179-
ir := &IncapsulaResponse{}
179+
ir := &ImpervaResponse{}
180180
if err = json.Unmarshal(bd, ir); err != nil {
181181
l.WithError(err).Errorf("json.Unmarshal error")
182182
return string(bd), err
183183
}
184-
l.Debugf("incapsula statusCode=%d response=%v", res.StatusCode, string(bd))
184+
l.Debugf("imperva statusCode=%d response=%v", res.StatusCode, string(bd))
185185
if ir.Res != 0 {
186186
l.Debugf("status=%v body=%s", res.StatusCode, string(bd))
187-
return string(bd), fmt.Errorf("incapsula upload failed, body=%s", string(bd))
187+
return string(bd), fmt.Errorf("imperva upload failed, body=%s", string(bd))
188188
}
189-
l.Debugf("incapsula response=%v", string(bd))
189+
l.Debugf("imperva response=%v", string(bd))
190190
return string(bd), err
191191
}
192192

193-
func (s *IncapsulaStore) Sync(c *tlssecret.Certificate) (map[string]string, error) {
193+
func (s *ImpervaStore) Sync(c *tlssecret.Certificate) (map[string]string, error) {
194194
s.SecretNamespace = c.Namespace
195195
l := log.WithFields(log.Fields{
196196
"action": "Sync",
197-
"store": "incapsula",
197+
"store": "imperva",
198198
"secretName": s.SecretName,
199199
"secretNamespace": s.SecretNamespace,
200200
"siteID": s.SiteID,
@@ -205,18 +205,18 @@ func (s *IncapsulaStore) Sync(c *tlssecret.Certificate) (map[string]string, erro
205205
})
206206
ctx := context.Background()
207207
if err := s.GetApiKey(ctx); err != nil {
208-
l.WithError(err).Errorf("incapsula.GetApiKey error")
208+
l.WithError(err).Errorf("imperva.GetApiKey error")
209209
l.WithError(err).Errorf("sync error")
210210
return nil, err
211211
}
212-
bd, err := s.GetIncapsulaSiteStatus()
212+
bd, err := s.GetImpervaSiteStatus()
213213
if err != nil {
214-
l.WithError(err).Errorf("incapsula.GetIncapsulaSiteStatus error: %s", bd)
214+
l.WithError(err).Errorf("imperva.GetImpervaSiteStatus error: %s", bd)
215215
l.WithError(err).Errorf("sync error")
216216
return nil, err
217217
}
218-
if err := s.UploadIncapsulaCert(c); err != nil {
219-
l.WithError(err).Errorf("incapsula.UploadIncapsulaCert error")
218+
if err := s.UploadImpervaCert(c); err != nil {
219+
l.WithError(err).Errorf("imperva.UploadImpervaCert error")
220220
l.WithError(err).Errorf("sync error")
221221
return nil, err
222222
}

0 commit comments

Comments
 (0)