Skip to content

Commit

Permalink
Merge pull request #68 from arangodb/store-accepted-spec
Browse files Browse the repository at this point in the history
Store accepted spec
  • Loading branch information
ewoutp authored Mar 23, 2018
2 parents bb57fc5 + 0909a77 commit 2af40ca
Show file tree
Hide file tree
Showing 43 changed files with 1,023 additions and 406 deletions.
27 changes: 21 additions & 6 deletions pkg/apis/deployment/v1alpha/authentication_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,29 @@ package v1alpha
import (
"github.com/pkg/errors"

"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
)

// AuthenticationSpec holds authentication specific configuration settings
type AuthenticationSpec struct {
JWTSecretName string `json:"jwtSecretName,omitempty"`
JWTSecretName *string `json:"jwtSecretName,omitempty"`
}

const (
// JWTSecretNameDisabled is the value of JWTSecretName to use for disabling authentication.
JWTSecretNameDisabled = "None"
)

// GetJWTSecretName returns the value of jwtSecretName.
func (s AuthenticationSpec) GetJWTSecretName() string {
return util.StringOrDefault(s.JWTSecretName)
}

// IsAuthenticated returns true if authentication is enabled.
// Returns false other (when JWTSecretName == "None").
func (s AuthenticationSpec) IsAuthenticated() bool {
return s.JWTSecretName != JWTSecretNameDisabled
return s.GetJWTSecretName() != JWTSecretNameDisabled
}

// Validate the given spec
Expand All @@ -50,7 +56,7 @@ func (s AuthenticationSpec) Validate(required bool) error {
return maskAny(errors.Wrap(ValidationError, "JWT secret is required"))
}
if s.IsAuthenticated() {
if err := k8sutil.ValidateResourceName(s.JWTSecretName); err != nil {
if err := k8sutil.ValidateResourceName(s.GetJWTSecretName()); err != nil {
return maskAny(err)
}
}
Expand All @@ -59,8 +65,17 @@ func (s AuthenticationSpec) Validate(required bool) error {

// SetDefaults fills in missing defaults
func (s *AuthenticationSpec) SetDefaults(defaultJWTSecretName string) {
if s.JWTSecretName == "" {
s.JWTSecretName = defaultJWTSecretName
if s.GetJWTSecretName() == "" {
// Note that we don't check for nil here, since even a specified, but empty
// string should result in the default value.
s.JWTSecretName = util.NewString(defaultJWTSecretName)
}
}

// SetDefaultsFrom fills unspecified fields with a value from given source spec.
func (s *AuthenticationSpec) SetDefaultsFrom(source AuthenticationSpec) {
if s.JWTSecretName == nil {
s.JWTSecretName = util.NewStringOrNil(source.JWTSecretName)
}
}

Expand All @@ -71,7 +86,7 @@ func (s AuthenticationSpec) ResetImmutableFields(fieldPrefix string, target *Aut
var resetFields []string
if s.IsAuthenticated() != target.IsAuthenticated() {
// Note: You can change the name, but not from empty to non-empty (or reverse).
target.JWTSecretName = s.JWTSecretName
target.JWTSecretName = util.NewStringOrNil(s.JWTSecretName)
resetFields = append(resetFields, fieldPrefix+".jwtSecretName")
}
return resetFields
Expand Down
49 changes: 25 additions & 24 deletions pkg/apis/deployment/v1alpha/authentication_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,24 @@ package v1alpha
import (
"testing"

"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/stretchr/testify/assert"
)

func TestAuthenticationSpecValidate(t *testing.T) {
// Valid
assert.Nil(t, AuthenticationSpec{JWTSecretName: "None"}.Validate(false))
assert.Nil(t, AuthenticationSpec{JWTSecretName: "foo"}.Validate(false))
assert.Nil(t, AuthenticationSpec{JWTSecretName: "foo"}.Validate(true))
assert.Nil(t, AuthenticationSpec{JWTSecretName: util.NewString("None")}.Validate(false))
assert.Nil(t, AuthenticationSpec{JWTSecretName: util.NewString("foo")}.Validate(false))
assert.Nil(t, AuthenticationSpec{JWTSecretName: util.NewString("foo")}.Validate(true))

// Not valid
assert.Error(t, AuthenticationSpec{JWTSecretName: "Foo"}.Validate(false))
assert.Error(t, AuthenticationSpec{JWTSecretName: util.NewString("Foo")}.Validate(false))
}

func TestAuthenticationSpecIsAuthenticated(t *testing.T) {
assert.False(t, AuthenticationSpec{JWTSecretName: "None"}.IsAuthenticated())
assert.True(t, AuthenticationSpec{JWTSecretName: "foo"}.IsAuthenticated())
assert.True(t, AuthenticationSpec{JWTSecretName: ""}.IsAuthenticated())
assert.False(t, AuthenticationSpec{JWTSecretName: util.NewString("None")}.IsAuthenticated())
assert.True(t, AuthenticationSpec{JWTSecretName: util.NewString("foo")}.IsAuthenticated())
assert.True(t, AuthenticationSpec{JWTSecretName: util.NewString("")}.IsAuthenticated())
}

func TestAuthenticationSpecSetDefaults(t *testing.T) {
Expand All @@ -50,8 +51,8 @@ func TestAuthenticationSpecSetDefaults(t *testing.T) {
return spec
}

assert.Equal(t, "test-jwt", def(AuthenticationSpec{}).JWTSecretName)
assert.Equal(t, "foo", def(AuthenticationSpec{JWTSecretName: "foo"}).JWTSecretName)
assert.Equal(t, "test-jwt", def(AuthenticationSpec{}).GetJWTSecretName())
assert.Equal(t, "foo", def(AuthenticationSpec{JWTSecretName: util.NewString("foo")}).GetJWTSecretName())
}

func TestAuthenticationSpecResetImmutableFields(t *testing.T) {
Expand All @@ -63,35 +64,35 @@ func TestAuthenticationSpecResetImmutableFields(t *testing.T) {
}{
// Valid "changes"
{
AuthenticationSpec{JWTSecretName: "None"},
AuthenticationSpec{JWTSecretName: "None"},
AuthenticationSpec{JWTSecretName: "None"},
AuthenticationSpec{JWTSecretName: util.NewString("None")},
AuthenticationSpec{JWTSecretName: util.NewString("None")},
AuthenticationSpec{JWTSecretName: util.NewString("None")},
nil,
},
{
AuthenticationSpec{JWTSecretName: "foo"},
AuthenticationSpec{JWTSecretName: "foo"},
AuthenticationSpec{JWTSecretName: "foo"},
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
nil,
},
{
AuthenticationSpec{JWTSecretName: "foo"},
AuthenticationSpec{JWTSecretName: "foo2"},
AuthenticationSpec{JWTSecretName: "foo2"},
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
AuthenticationSpec{JWTSecretName: util.NewString("foo2")},
AuthenticationSpec{JWTSecretName: util.NewString("foo2")},
nil,
},

// Invalid changes
{
AuthenticationSpec{JWTSecretName: "foo"},
AuthenticationSpec{JWTSecretName: "None"},
AuthenticationSpec{JWTSecretName: "foo"},
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
AuthenticationSpec{JWTSecretName: util.NewString("None")},
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
[]string{"test.jwtSecretName"},
},
{
AuthenticationSpec{JWTSecretName: "None"},
AuthenticationSpec{JWTSecretName: "foo"},
AuthenticationSpec{JWTSecretName: "None"},
AuthenticationSpec{JWTSecretName: util.NewString("None")},
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
AuthenticationSpec{JWTSecretName: util.NewString("None")},
[]string{"test.jwtSecretName"},
},
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/apis/deployment/v1alpha/deployment_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,27 @@ func (m DeploymentMode) HasCoordinators() bool {
func (m DeploymentMode) SupportsSync() bool {
return m == DeploymentModeCluster
}

// NewMode returns a reference to a string with given value.
func NewMode(input DeploymentMode) *DeploymentMode {
return &input
}

// NewModeOrNil returns nil if input is nil, otherwise returns a clone of the given value.
func NewModeOrNil(input *DeploymentMode) *DeploymentMode {
if input == nil {
return nil
}
return NewMode(*input)
}

// ModeOrDefault returns the default value (or empty string) if input is nil, otherwise returns the referenced value.
func ModeOrDefault(input *DeploymentMode, defaultValue ...DeploymentMode) DeploymentMode {
if input == nil {
if len(defaultValue) > 0 {
return defaultValue[0]
}
return ""
}
return *input
}
Loading

0 comments on commit 2af40ca

Please sign in to comment.