Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store accepted spec #68

Merged
merged 11 commits into from
Mar 23, 2018
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