From 9b6e47e6b8c3ba3f10a6aa22a71b4c9cfa70c8a8 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 11 Apr 2022 15:52:55 +0200 Subject: [PATCH] outposts/ldap: fix panic in type conversion when value is nil Signed-off-by: Jens Langhammer --- internal/outpost/ldap/utils/utils.go | 4 +++- internal/outpost/ldap/utils/utils_test.go | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/outpost/ldap/utils/utils.go b/internal/outpost/ldap/utils/utils.go index 2e5147335..55bff48df 100644 --- a/internal/outpost/ldap/utils/utils.go +++ b/internal/outpost/ldap/utils/utils.go @@ -29,7 +29,9 @@ func ldapResolveTypeSingle(in interface{}) *string { s := BoolToString(*t) return &s default: - log.WithField("type", reflect.TypeOf(in).String()).Warning("Type can't be mapped to LDAP yet") + if in != nil { + log.WithField("type", reflect.TypeOf(in).String()).Warning("Type can't be mapped to LDAP yet") + } return nil } } diff --git a/internal/outpost/ldap/utils/utils_test.go b/internal/outpost/ldap/utils/utils_test.go index 07eba41f3..05649ac7d 100644 --- a/internal/outpost/ldap/utils/utils_test.go +++ b/internal/outpost/ldap/utils/utils_test.go @@ -7,6 +7,11 @@ import ( "goauthentik.io/api/v3" ) +func Test_ldapResolveTypeSingle_nil(t *testing.T) { + var ex *string + assert.Equal(t, ex, ldapResolveTypeSingle(nil)) +} + func TestAKAttrsToLDAP_String(t *testing.T) { var d *map[string]interface{} @@ -54,7 +59,7 @@ func TestAKAttrsToLDAP_Dict(t *testing.T) { assert.Equal(t, 1, len(AKAttrsToLDAP(d))) assert.Equal(t, "foo", AKAttrsToLDAP(d)[0].Name) // Dicts are currently unsupported, but make sure we don't crash - // assert.Equal(t, []string{nil}, AKAttrsToLDAP(d)[0].Values) + assert.Equal(t, []string([]string(nil)), AKAttrsToLDAP(d)[0].Values) } func TestAKAttrsToLDAP_Mixed(t *testing.T) { @@ -68,5 +73,5 @@ func TestAKAttrsToLDAP_Mixed(t *testing.T) { assert.Equal(t, 1, len(AKAttrsToLDAP(d))) assert.Equal(t, "foo", AKAttrsToLDAP(d)[0].Name) // Dicts are currently unsupported, but make sure we don't crash - // assert.Equal(t, []string{nil}, AKAttrsToLDAP(d)[0].Values) + assert.Equal(t, []string{"foo", ""}, AKAttrsToLDAP(d)[0].Values) }