Skip to content

Commit

Permalink
[v17] Fix unmarshalling of CreateDatabaseUserMode (#50147)
Browse files Browse the repository at this point in the history
* Fix unmarshalling of CreateDatabaseUserMode

* Review
  • Loading branch information
bl-nero authored Dec 12, 2024
1 parent f834f17 commit 7d06e93
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 15 deletions.
24 changes: 24 additions & 0 deletions api/types/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -2227,8 +2227,23 @@ func (h CreateDatabaseUserMode) encode() (string, error) {
func (h *CreateDatabaseUserMode) decode(val any) error {
var str string
switch val := val.(type) {
case int32:
return trace.Wrap(h.setFromEnum(val))
case int64:
return trace.Wrap(h.setFromEnum(int32(val)))
case int:
return trace.Wrap(h.setFromEnum(int32(val)))
case float64:
return trace.Wrap(h.setFromEnum(int32(val)))
case float32:
return trace.Wrap(h.setFromEnum(int32(val)))
case string:
str = val
case bool:
if val {
return trace.BadParameter("create_database_user_mode cannot be true, got %v", val)
}
str = createHostUserModeOffString
default:
return trace.BadParameter("bad value type %T, expected string", val)
}
Expand All @@ -2249,6 +2264,15 @@ func (h *CreateDatabaseUserMode) decode(val any) error {
return nil
}

// setFromEnum sets the value from enum value as int32.
func (h *CreateDatabaseUserMode) setFromEnum(val int32) error {
if _, ok := CreateDatabaseUserMode_name[val]; !ok {
return trace.BadParameter("invalid database user creation mode %v", val)
}
*h = CreateDatabaseUserMode(val)
return nil
}

// UnmarshalYAML supports parsing CreateDatabaseUserMode from string.
func (h *CreateDatabaseUserMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
var val interface{}
Expand Down
73 changes: 58 additions & 15 deletions api/types/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,8 @@ func TestMarshallCreateHostUserModeYAML(t *testing.T) {
input CreateHostUserMode
expected string
}{
{input: CreateHostUserMode_HOST_USER_MODE_OFF, expected: "\"off\""},
{input: CreateHostUserMode_HOST_USER_MODE_UNSPECIFIED, expected: "\"\""},
{input: CreateHostUserMode_HOST_USER_MODE_OFF, expected: `"off"`},
{input: CreateHostUserMode_HOST_USER_MODE_UNSPECIFIED, expected: `""`},
{input: CreateHostUserMode_HOST_USER_MODE_KEEP, expected: "keep"},
{input: CreateHostUserMode_HOST_USER_MODE_INSECURE_DROP, expected: "insecure-drop"},
} {
Expand All @@ -606,15 +606,15 @@ func TestMarshallCreateHostUserModeYAML(t *testing.T) {

func TestUnmarshallCreateHostUserModeJSON(t *testing.T) {
for _, tc := range []struct {
expected CreateHostUserMode
input any
expected CreateHostUserMode
}{
{expected: CreateHostUserMode_HOST_USER_MODE_OFF, input: "\"off\""},
{expected: CreateHostUserMode_HOST_USER_MODE_UNSPECIFIED, input: "\"\""},
{expected: CreateHostUserMode_HOST_USER_MODE_KEEP, input: "\"keep\""},
{expected: CreateHostUserMode_HOST_USER_MODE_KEEP, input: 3},
{expected: CreateHostUserMode_HOST_USER_MODE_OFF, input: 1},
{expected: CreateHostUserMode_HOST_USER_MODE_INSECURE_DROP, input: 4},
{input: `"off"`, expected: CreateHostUserMode_HOST_USER_MODE_OFF},
{input: `""`, expected: CreateHostUserMode_HOST_USER_MODE_UNSPECIFIED},
{input: `"keep"`, expected: CreateHostUserMode_HOST_USER_MODE_KEEP},
{input: 3, expected: CreateHostUserMode_HOST_USER_MODE_KEEP},
{input: 1, expected: CreateHostUserMode_HOST_USER_MODE_OFF},
{input: 4, expected: CreateHostUserMode_HOST_USER_MODE_INSECURE_DROP},
} {
var got CreateHostUserMode
err := json.Unmarshal([]byte(fmt.Sprintf("%v", tc.input)), &got)
Expand All @@ -625,14 +625,14 @@ func TestUnmarshallCreateHostUserModeJSON(t *testing.T) {

func TestUnmarshallCreateHostUserModeYAML(t *testing.T) {
for _, tc := range []struct {
expected CreateHostUserMode
input string
expected CreateHostUserMode
}{
{expected: CreateHostUserMode_HOST_USER_MODE_OFF, input: "\"off\""},
{expected: CreateHostUserMode_HOST_USER_MODE_OFF, input: "off"},
{expected: CreateHostUserMode_HOST_USER_MODE_UNSPECIFIED, input: "\"\""},
{expected: CreateHostUserMode_HOST_USER_MODE_KEEP, input: "keep"},
{expected: CreateHostUserMode_HOST_USER_MODE_INSECURE_DROP, input: "insecure-drop"},
{input: `"off"`, expected: CreateHostUserMode_HOST_USER_MODE_OFF},
{input: "off", expected: CreateHostUserMode_HOST_USER_MODE_OFF},
{input: `""`, expected: CreateHostUserMode_HOST_USER_MODE_UNSPECIFIED},
{input: "keep", expected: CreateHostUserMode_HOST_USER_MODE_KEEP},
{input: "insecure-drop", expected: CreateHostUserMode_HOST_USER_MODE_INSECURE_DROP},
} {
var got CreateHostUserMode
err := yaml.Unmarshal([]byte(tc.input), &got)
Expand All @@ -641,6 +641,49 @@ func TestUnmarshallCreateHostUserModeYAML(t *testing.T) {
}
}

func TestUnmarshallCreateDatabaseUserModeJSON(t *testing.T) {
for _, tc := range []struct {
input any
expected CreateDatabaseUserMode
}{
{input: `""`, expected: CreateDatabaseUserMode_DB_USER_MODE_UNSPECIFIED},
{input: `"off"`, expected: CreateDatabaseUserMode_DB_USER_MODE_OFF},
{input: `"keep"`, expected: CreateDatabaseUserMode_DB_USER_MODE_KEEP},
{input: `"best_effort_drop"`, expected: CreateDatabaseUserMode_DB_USER_MODE_BEST_EFFORT_DROP},
{input: 0, expected: CreateDatabaseUserMode_DB_USER_MODE_UNSPECIFIED},
{input: 1, expected: CreateDatabaseUserMode_DB_USER_MODE_OFF},
{input: 2, expected: CreateDatabaseUserMode_DB_USER_MODE_KEEP},
{input: 3, expected: CreateDatabaseUserMode_DB_USER_MODE_BEST_EFFORT_DROP},
} {
var got CreateDatabaseUserMode
err := json.Unmarshal([]byte(fmt.Sprintf("%v", tc.input)), &got)
require.NoError(t, err)
require.Equalf(t, tc.expected, got, "for input: %v", tc.input)
}
}

func TestUnmarshallCreateDatabaseUserModeYAML(t *testing.T) {
for _, tc := range []struct {
input any
expected CreateDatabaseUserMode
}{
{input: `""`, expected: CreateDatabaseUserMode_DB_USER_MODE_UNSPECIFIED},
{input: `"off"`, expected: CreateDatabaseUserMode_DB_USER_MODE_OFF},
{input: "off", expected: CreateDatabaseUserMode_DB_USER_MODE_OFF},
{input: `"keep"`, expected: CreateDatabaseUserMode_DB_USER_MODE_KEEP},
{input: `"best_effort_drop"`, expected: CreateDatabaseUserMode_DB_USER_MODE_BEST_EFFORT_DROP},
{input: 0, expected: CreateDatabaseUserMode_DB_USER_MODE_UNSPECIFIED},
{input: 1, expected: CreateDatabaseUserMode_DB_USER_MODE_OFF},
{input: 2, expected: CreateDatabaseUserMode_DB_USER_MODE_KEEP},
{input: 3, expected: CreateDatabaseUserMode_DB_USER_MODE_BEST_EFFORT_DROP},
} {
var got CreateDatabaseUserMode
err := yaml.Unmarshal([]byte(fmt.Sprintf("%v", tc.input)), &got)
require.NoError(t, err)
require.Equalf(t, tc.expected, got, "for input: %v", tc.input)
}
}

func TestRoleV6_CheckAndSetDefaults(t *testing.T) {
t.Parallel()
requireBadParameterContains := func(contains string) require.ErrorAssertionFunc {
Expand Down

0 comments on commit 7d06e93

Please sign in to comment.