@@ -179,23 +179,35 @@ type AuthenticationSpec struct {
179
179
JWTSecretName string `json:"jwtSecretName,omitempty"`
180
180
}
181
181
182
+ const (
183
+ // JWTSecretNameDisabled is the value of JWTSecretName to use for disabling authentication.
184
+ JWTSecretNameDisabled = "None"
185
+ )
186
+
187
+ // IsAuthenticated returns true if authentication is enabled.
188
+ // Returns false other (when JWTSecretName == "None").
189
+ func (s AuthenticationSpec ) IsAuthenticated () bool {
190
+ return s .JWTSecretName != JWTSecretNameDisabled
191
+ }
192
+
182
193
// Validate the given spec
183
- func (s AuthenticationSpec ) Validate (required , allowed bool ) error {
184
- if required && s . JWTSecretName == "" {
185
- return maskAny (errors .Wrap (ValidationError , "Missing JWT secret" ))
194
+ func (s AuthenticationSpec ) Validate (required bool ) error {
195
+ if required && ! s . IsAuthenticated () {
196
+ return maskAny (errors .Wrap (ValidationError , "JWT secret is required " ))
186
197
}
187
- if ! allowed && s .JWTSecretName != "" {
188
- return maskAny (errors .Wrap (ValidationError , "Non-empty JWT secret name is not allowed" ))
189
- }
190
- if err := k8sutil .ValidateOptionalResourceName (s .JWTSecretName ); err != nil {
191
- return maskAny (err )
198
+ if s .IsAuthenticated () {
199
+ if err := k8sutil .ValidateResourceName (s .JWTSecretName ); err != nil {
200
+ return maskAny (err )
201
+ }
192
202
}
193
203
return nil
194
204
}
195
205
196
206
// SetDefaults fills in missing defaults
197
- func (s * AuthenticationSpec ) SetDefaults () {
198
- // Nothing needed
207
+ func (s * AuthenticationSpec ) SetDefaults (defaultJWTSecretName string ) {
208
+ if s .JWTSecretName == "" {
209
+ s .JWTSecretName = defaultJWTSecretName
210
+ }
199
211
}
200
212
201
213
// SSLSpec holds SSL specific configuration settings
@@ -256,7 +268,7 @@ func (s SyncSpec) Validate(mode DeploymentMode) error {
256
268
if s .Image == "" {
257
269
return maskAny (errors .Wrapf (ValidationError , "image must be set" ))
258
270
}
259
- if err := s .Authentication .Validate (s .Enabled , s . Enabled ); err != nil {
271
+ if err := s .Authentication .Validate (s .Enabled ); err != nil {
260
272
return maskAny (err )
261
273
}
262
274
if err := s .Monitoring .Validate (); err != nil {
@@ -266,14 +278,14 @@ func (s SyncSpec) Validate(mode DeploymentMode) error {
266
278
}
267
279
268
280
// SetDefaults fills in missing defaults
269
- func (s * SyncSpec ) SetDefaults (defaultImage string , defaulPullPolicy v1.PullPolicy ) {
281
+ func (s * SyncSpec ) SetDefaults (defaultImage string , defaulPullPolicy v1.PullPolicy , defaultJWTSecretName string ) {
270
282
if s .Image == "" {
271
283
s .Image = defaultImage
272
284
}
273
285
if s .ImagePullPolicy == "" {
274
286
s .ImagePullPolicy = defaulPullPolicy
275
287
}
276
- s .Authentication .SetDefaults ()
288
+ s .Authentication .SetDefaults (defaultJWTSecretName )
277
289
s .Monitoring .SetDefaults ()
278
290
}
279
291
@@ -420,7 +432,7 @@ type DeploymentSpec struct {
420
432
421
433
// IsAuthenticated returns true when authentication is enabled
422
434
func (s DeploymentSpec ) IsAuthenticated () bool {
423
- return s .Authentication .JWTSecretName != ""
435
+ return s .Authentication .IsAuthenticated ()
424
436
}
425
437
426
438
// IsSecure returns true when SSL is enabled
@@ -429,7 +441,7 @@ func (s DeploymentSpec) IsSecure() bool {
429
441
}
430
442
431
443
// SetDefaults fills in default values when a field is not specified.
432
- func (s * DeploymentSpec ) SetDefaults () {
444
+ func (s * DeploymentSpec ) SetDefaults (deploymentName string ) {
433
445
if s .Mode == "" {
434
446
s .Mode = DeploymentModeCluster
435
447
}
@@ -446,9 +458,9 @@ func (s *DeploymentSpec) SetDefaults() {
446
458
s .ImagePullPolicy = v1 .PullIfNotPresent
447
459
}
448
460
s .RocksDB .SetDefaults ()
449
- s .Authentication .SetDefaults ()
461
+ s .Authentication .SetDefaults (deploymentName + "-jwt" )
450
462
s .SSL .SetDefaults ()
451
- s .Sync .SetDefaults (s .Image , s .ImagePullPolicy )
463
+ s .Sync .SetDefaults (s .Image , s .ImagePullPolicy , deploymentName + "-sync-jwt" )
452
464
s .Single .SetDefaults (ServerGroupSingle , s .Mode .HasSingleServers (), s .Mode )
453
465
s .Agents .SetDefaults (ServerGroupAgents , s .Mode .HasAgents (), s .Mode )
454
466
s .DBServers .SetDefaults (ServerGroupDBServers , s .Mode .HasDBServers (), s .Mode )
@@ -461,31 +473,31 @@ func (s *DeploymentSpec) SetDefaults() {
461
473
// Return errors when validation fails, nil on success.
462
474
func (s * DeploymentSpec ) Validate () error {
463
475
if err := s .Mode .Validate (); err != nil {
464
- return maskAny (err )
476
+ return maskAny (errors . Wrap ( err , "spec.mode" ) )
465
477
}
466
478
if err := s .Environment .Validate (); err != nil {
467
- return maskAny (err )
479
+ return maskAny (errors . Wrap ( err , "spec.environment" ) )
468
480
}
469
481
if err := s .StorageEngine .Validate (); err != nil {
470
- return maskAny (err )
482
+ return maskAny (errors . Wrap ( err , "spec.storageEngine" ) )
471
483
}
472
484
if err := validatePullPolicy (s .ImagePullPolicy ); err != nil {
473
- return maskAny (err )
485
+ return maskAny (errors . Wrap ( err , "spec.imagePullPolicy" ) )
474
486
}
475
487
if s .Image == "" {
476
- return maskAny (errors .Wrapf (ValidationError , "image must be set" ))
488
+ return maskAny (errors .Wrapf (ValidationError , "spec. image must be set" ))
477
489
}
478
490
if err := s .RocksDB .Validate (); err != nil {
479
- return maskAny (err )
491
+ return maskAny (errors . Wrap ( err , "spec.rocksdb" ) )
480
492
}
481
- if err := s .Authentication .Validate (false , true ); err != nil {
482
- return maskAny (err )
493
+ if err := s .Authentication .Validate (false ); err != nil {
494
+ return maskAny (errors . Wrap ( err , "spec.auth" ) )
483
495
}
484
496
if err := s .SSL .Validate (); err != nil {
485
- return maskAny (err )
497
+ return maskAny (errors . Wrap ( err , "spec.ssl" ) )
486
498
}
487
499
if err := s .Sync .Validate (s .Mode ); err != nil {
488
- return maskAny (err )
500
+ return maskAny (errors . Wrap ( err , "spec.sync" ) )
489
501
}
490
502
if err := s .Single .Validate (ServerGroupSingle , s .Mode .HasSingleServers (), s .Mode ); err != nil {
491
503
return maskAny (err )
0 commit comments