Skip to content

Commit 05351b1

Browse files
authored
[Feature] Ensure token in JWT exists (#593)
1 parent 3fbc205 commit 05351b1

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

pkg/deployment/reconcile/action_jwt_set_active.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"context"
2727
"encoding/base64"
2828

29+
"github.com/arangodb/kube-arangodb/pkg/util/constants"
30+
2931
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
3032
"github.com/arangodb/kube-arangodb/pkg/deployment/patch"
3133
"github.com/arangodb/kube-arangodb/pkg/deployment/pod"
@@ -85,8 +87,9 @@ func (a *jwtSetActiveAction) Start(ctx context.Context) (bool, error) {
8587
}
8688

8789
activeKeyData, active := f.Data[pod.ActiveJWTKey]
90+
tokenKeyData, token := f.Data[constants.SecretKeyToken]
8891

89-
if util.SHA256(activeKeyData) == toActiveChecksum {
92+
if util.SHA256(activeKeyData) == toActiveChecksum && util.SHA256(activeKeyData) == util.SHA256(tokenKeyData) {
9093
a.log.Info().Msgf("Desired JWT is already active")
9194
return true, nil
9295
}
@@ -99,6 +102,13 @@ func (a *jwtSetActiveAction) Start(ctx context.Context) (bool, error) {
99102
p.ItemReplace(path, base64.StdEncoding.EncodeToString(toActiveData))
100103
}
101104

105+
path = patch.NewPath("data", constants.SecretKeyToken)
106+
if !token {
107+
p.ItemAdd(path, base64.StdEncoding.EncodeToString(toActiveData))
108+
} else {
109+
p.ItemReplace(path, base64.StdEncoding.EncodeToString(toActiveData))
110+
}
111+
102112
patch, err := p.Marshal()
103113
if err != nil {
104114
a.log.Error().Err(err).Msgf("Unable to encrypt patch")

pkg/deployment/reconcile/action_jwt_status_update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (a *jwtStatusUpdateAction) Start(ctx context.Context) (bool, error) {
152152
var keys []string
153153

154154
for key := range f.Data {
155-
if key == pod.ActiveJWTKey || key == activeKeyShort {
155+
if key == pod.ActiveJWTKey || key == activeKeyShort || key == constants.SecretKeyToken {
156156
continue
157157
}
158158

pkg/deployment/reconcile/plan_builder_jwt.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ func createJWTKeyUpdate(ctx context.Context,
7878
return addJWTPropagatedPlanAction(status, api.NewAction(api.ActionTypeJWTSetActive, api.ServerGroupUnknown, "", "Set active key").AddParam(checksum, jwtSha))
7979
}
8080

81+
tokenKey, ok := folder.Data[constants.SecretKeyToken]
82+
if !ok || util.SHA256(activeKey) != util.SHA256(tokenKey) {
83+
return addJWTPropagatedPlanAction(status, api.NewAction(api.ActionTypeJWTSetActive, api.ServerGroupUnknown, "", "Set active key and add token field").AddParam(checksum, jwtSha))
84+
}
85+
8186
plan, failed := areJWTTokensUpToDate(ctx, log, apiObject, spec, status, cachedStatus, context, folder)
8287
if len(plan) > 0 {
8388
return plan
@@ -93,7 +98,7 @@ func createJWTKeyUpdate(ctx context.Context,
9398
}
9499

95100
for key := range folder.Data {
96-
if key == pod.ActiveJWTKey {
101+
if key == pod.ActiveJWTKey || key == constants.SecretKeyToken {
97102
continue
98103
}
99104

@@ -184,7 +189,7 @@ func createJWTStatusUpdateRequired(ctx context.Context,
184189
var keys []string
185190

186191
for key := range f.Data {
187-
if key == pod.ActiveJWTKey || key == activeKeyShort {
192+
if key == pod.ActiveJWTKey || key == activeKeyShort || key == constants.SecretKeyToken {
188193
continue
189194
}
190195

@@ -309,7 +314,7 @@ func isMemberJWTTokenInvalid(ctx context.Context, c client.Client, data map[stri
309314

310315
func compareJWTKeys(e client.Entries, keys map[string][]byte) bool {
311316
for k := range keys {
312-
if k == pod.ActiveJWTKey {
317+
if k == pod.ActiveJWTKey || k == constants.SecretKeyToken {
313318
continue
314319
}
315320

pkg/deployment/resources/secrets.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func (r *Resources) ensureTokenSecretFolder(cachedStatus inspector.Inspector, se
205205
if err := r.createSecretWithMod(secrets, folderSecretName, func(s *core.Secret) {
206206
s.Data[util.SHA256(token)] = token
207207
s.Data[pod.ActiveJWTKey] = token
208+
s.Data[constants.SecretKeyToken] = token
208209
}); err != nil {
209210
return err
210211
}

0 commit comments

Comments
 (0)