From 88a8b7d2fa602b02db84b4d4de8100c1daea3e32 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 27 May 2022 14:28:26 +0000 Subject: [PATCH] outposts/ldap: fix type assertion after upgrading to new API Client Signed-off-by: Jens Langhammer --- internal/outpost/ldap/group/group.go | 2 +- internal/outpost/ldap/utils/utils.go | 5 ++- internal/outpost/ldap/utils/utils_test.go | 40 +++++++++++------------ 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/internal/outpost/ldap/group/group.go b/internal/outpost/ldap/group/group.go index 0f1f5d48f..94a1ae913 100644 --- a/internal/outpost/ldap/group/group.go +++ b/internal/outpost/ldap/group/group.go @@ -16,7 +16,7 @@ type LDAPGroup struct { Member []string IsSuperuser bool IsVirtualGroup bool - AKAttributes interface{} + AKAttributes map[string]interface{} } func (lg *LDAPGroup) Entry() *ldap.Entry { diff --git a/internal/outpost/ldap/utils/utils.go b/internal/outpost/ldap/utils/utils.go index 55bff48df..25611033f 100644 --- a/internal/outpost/ldap/utils/utils.go +++ b/internal/outpost/ldap/utils/utils.go @@ -36,13 +36,12 @@ func ldapResolveTypeSingle(in interface{}) *string { } } -func AKAttrsToLDAP(attrs interface{}) []*ldap.EntryAttribute { +func AKAttrsToLDAP(attrs map[string]interface{}) []*ldap.EntryAttribute { attrList := []*ldap.EntryAttribute{} if attrs == nil { return attrList } - a := attrs.(*map[string]interface{}) - for attrKey, attrValue := range *a { + for attrKey, attrValue := range attrs { entry := &ldap.EntryAttribute{Name: attrKey} switch t := attrValue.(type) { case []string: diff --git a/internal/outpost/ldap/utils/utils_test.go b/internal/outpost/ldap/utils/utils_test.go index 05649ac7d..5a06561a8 100644 --- a/internal/outpost/ldap/utils/utils_test.go +++ b/internal/outpost/ldap/utils/utils_test.go @@ -13,45 +13,45 @@ func Test_ldapResolveTypeSingle_nil(t *testing.T) { } func TestAKAttrsToLDAP_String(t *testing.T) { - var d *map[string]interface{} + u := api.User{} // normal string - d = &map[string]interface{}{ + u.Attributes = map[string]interface{}{ "foo": "bar", } - assert.Equal(t, 1, len(AKAttrsToLDAP(d))) - assert.Equal(t, "foo", AKAttrsToLDAP(d)[0].Name) - assert.Equal(t, []string{"bar"}, AKAttrsToLDAP(d)[0].Values) + assert.Equal(t, 1, len(AKAttrsToLDAP(u.Attributes))) + assert.Equal(t, "foo", AKAttrsToLDAP(u.Attributes)[0].Name) + assert.Equal(t, []string{"bar"}, AKAttrsToLDAP(u.Attributes)[0].Values) // pointer string - d = &map[string]interface{}{ + u.Attributes = map[string]interface{}{ "foo": api.PtrString("bar"), } - assert.Equal(t, 1, len(AKAttrsToLDAP(d))) - assert.Equal(t, "foo", AKAttrsToLDAP(d)[0].Name) - assert.Equal(t, []string{"bar"}, AKAttrsToLDAP(d)[0].Values) + assert.Equal(t, 1, len(AKAttrsToLDAP(u.Attributes))) + assert.Equal(t, "foo", AKAttrsToLDAP(u.Attributes)[0].Name) + assert.Equal(t, []string{"bar"}, AKAttrsToLDAP(u.Attributes)[0].Values) } func TestAKAttrsToLDAP_String_List(t *testing.T) { - var d *map[string]interface{} + u := api.User{} // string list - d = &map[string]interface{}{ + u.Attributes = map[string]interface{}{ "foo": []string{"bar"}, } - assert.Equal(t, 1, len(AKAttrsToLDAP(d))) - assert.Equal(t, "foo", AKAttrsToLDAP(d)[0].Name) - assert.Equal(t, []string{"bar"}, AKAttrsToLDAP(d)[0].Values) + assert.Equal(t, 1, len(AKAttrsToLDAP(u.Attributes))) + assert.Equal(t, "foo", AKAttrsToLDAP(u.Attributes)[0].Name) + assert.Equal(t, []string{"bar"}, AKAttrsToLDAP(u.Attributes)[0].Values) // pointer string list - d = &map[string]interface{}{ + u.Attributes = map[string]interface{}{ "foo": &[]string{"bar"}, } - assert.Equal(t, 1, len(AKAttrsToLDAP(d))) - assert.Equal(t, "foo", AKAttrsToLDAP(d)[0].Name) - assert.Equal(t, []string{"bar"}, AKAttrsToLDAP(d)[0].Values) + assert.Equal(t, 1, len(AKAttrsToLDAP(u.Attributes))) + assert.Equal(t, "foo", AKAttrsToLDAP(u.Attributes)[0].Name) + assert.Equal(t, []string{"bar"}, AKAttrsToLDAP(u.Attributes)[0].Values) } func TestAKAttrsToLDAP_Dict(t *testing.T) { // dict - d := &map[string]interface{}{ + d := map[string]interface{}{ "foo": map[string]string{ "foo": "bar", }, @@ -64,7 +64,7 @@ func TestAKAttrsToLDAP_Dict(t *testing.T) { func TestAKAttrsToLDAP_Mixed(t *testing.T) { // dict - d := &map[string]interface{}{ + d := map[string]interface{}{ "foo": []interface{}{ "foo", 6,