Skip to content

Commit 1b56fd7

Browse files
committed
ApplicationAuth: support multiple app_key
1 parent a741d50 commit 1b56fd7

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

controllers/capabilities/applicationauth_controller.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23+
"strings"
2324

2425
capabilitiesv1beta1 "github.com/3scale/3scale-operator/apis/capabilities/v1beta1"
2526
controllerhelper "github.com/3scale/3scale-operator/pkg/controller/helper"
@@ -168,7 +169,7 @@ func (r *ApplicationAuthReconciler) Reconcile(ctx context.Context, req ctrl.Requ
168169
return reconcileStatus(r.BaseReconciler, applicationAuth, err, reqLogger)
169170
}
170171

171-
err = syncApplicationAuth(*developerAccount.Status.ID, *application.Status.ID, *authMode, *authSecret, threescaleAPIClient)
172+
err = syncApplicationAuth(*developerAccount.Status.ID, *application.Status.ID, *authMode, *authSecret, threescaleAPIClient, reqLogger)
172173
if err != nil {
173174
return reconcileStatus(r.BaseReconciler, applicationAuth, err, reqLogger)
174175
}
@@ -190,6 +191,7 @@ func syncApplicationAuth(
190191
authMode string,
191192
authSecret AuthSecret,
192193
threescaleClient *threescaleapi.ThreeScaleClient,
194+
logger logr.Logger,
193195
) error {
194196
switch authMode {
195197
case "1":
@@ -209,27 +211,42 @@ func syncApplicationAuth(
209211
}
210212
}
211213
case "2":
214+
desiredKeys := strings.Split(authSecret.ApplicationKey, ",")
215+
if len(desiredKeys) > 5 {
216+
return fmt.Errorf("secret contains more than 5 application_key")
217+
}
218+
212219
// get the existing value from the portal
213-
existingKeys, err := threescaleClient.ApplicationKeys(developerAccountID, applicationID)
220+
applicationKeys, err := threescaleClient.ApplicationKeys(developerAccountID, applicationID)
214221
if err != nil {
215222
return err
216223
}
217224

218-
// pre-existing keys
219-
if len(existingKeys) > 0 {
220-
// Nothing to do, return early
221-
if existingKeys[0].Value == authSecret.ApplicationKey {
222-
return nil
223-
}
225+
existingKeys := make([]string, 0, len(applicationKeys))
226+
for _, key := range applicationKeys {
227+
existingKeys = append(existingKeys, key.Value)
228+
}
224229

225-
// if the key is not match, delete it
226-
if err := threescaleClient.DeleteApplicationKey(developerAccountID, applicationID, existingKeys[0].Value); err != nil {
227-
return err
230+
// delete existing and not desired
231+
notDesiredExistingKeys := helper.ArrayStringDifference(existingKeys, desiredKeys)
232+
logger.V(1).Info("syncApplicationAuth", "notDesiredExistingKeys", notDesiredExistingKeys)
233+
for _, key := range notDesiredExistingKeys {
234+
// key is expected to exist
235+
// notDesiredExistingKeys is a subset of the existingMap key set
236+
if err := threescaleClient.DeleteApplicationKey(developerAccountID, applicationID, key); err != nil {
237+
return fmt.Errorf("error sync applicationAuth for developerAccountID: %d, applicationID: %d, error: %w", developerAccountID, applicationID, err)
228238
}
229239
}
230240

231-
if _, err := threescaleClient.CreateApplicationKey(developerAccountID, applicationID, authSecret.ApplicationKey); err != nil {
232-
return err
241+
// Create not existing and desired
242+
desiredNewKeys := helper.ArrayStringDifference(desiredKeys, existingKeys)
243+
logger.V(1).Info("syncApplicationPlans", "desiredNewKeys", desiredNewKeys)
244+
for _, key := range desiredNewKeys {
245+
// key is expected to exist
246+
// desiredNewKeys is a subset of the Spec.ApplicationPlans map key set
247+
if _, err := threescaleClient.CreateApplicationKey(developerAccountID, applicationID, key); err != nil {
248+
return fmt.Errorf("error sync applicationAuth for developerAccountID: %d, applicationID: %d, error: %w", developerAccountID, applicationID, err)
249+
}
233250
}
234251
}
235252

controllers/capabilities/applicationauth_controller_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
func TestApplicationAuthReconciler_syncApplicationAuth(t *testing.T) {
2424
appID := int64(3)
2525
userAccountID := int64(3)
26+
logger := logf.Log.WithName("ApplicationAuth reconciler")
2627

2728
tests := []struct {
2829
name string
@@ -122,14 +123,42 @@ func TestApplicationAuthReconciler_syncApplicationAuth(t *testing.T) {
122123
expectedKey: "testkey",
123124
wantErr: false,
124125
},
126+
{
127+
name: "update existing app_key with the same value should not return error",
128+
mockServer: &mockApplicationAuthServer{
129+
authMode: "2",
130+
keys: []string{"testkey"},
131+
userAccountID: appID,
132+
appID: userAccountID,
133+
},
134+
authMode: "2",
135+
authSecret: getAuthSecret(),
136+
expectedKey: "testkey",
137+
wantErr: false,
138+
},
139+
{
140+
name: "update with secret contains multiple app_key",
141+
mockServer: &mockApplicationAuthServer{
142+
authMode: "2",
143+
keys: []string{"testkey"},
144+
userAccountID: appID,
145+
appID: userAccountID,
146+
},
147+
authMode: "2",
148+
authSecret: AuthSecret{
149+
ApplicationKey: "testkey1,testkey2,testkey3",
150+
},
151+
expectedKey: "testkey1,testkey2,testkey3",
152+
wantErr: false,
153+
},
125154
}
126155
for _, tt := range tests {
127156
t.Run(tt.name, func(t *testing.T) {
128157
srv := tt.mockServer.GetServer()
129158
ap, _ := threescaleapi.NewAdminPortalFromStr(srv.URL)
130159
threescaleClient := threescaleapi.NewThreeScale(ap, "test", srv.Client())
131160

132-
err := syncApplicationAuth(userAccountID, appID, tt.authMode, tt.authSecret, threescaleClient)
161+
err := syncApplicationAuth(userAccountID, appID, tt.authMode, tt.authSecret, threescaleClient, logger)
133162
if (err != nil) != tt.wantErr {
134163
t.Errorf("syncApplicationAuth() error = %v, wantErr %v", err, tt.wantErr)
135164
return

0 commit comments

Comments
 (0)