outposts/ldap: increase compatibility with different types in user and group attributes

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-10-16 22:32:18 +02:00
parent 96a30af0eb
commit b864de7721
4 changed files with 22 additions and 20 deletions

View File

@ -1,6 +1,8 @@
package ldap package ldap
import ( import (
"strconv"
"github.com/nmcclain/ldap" "github.com/nmcclain/ldap"
"goauthentik.io/api/v3" "goauthentik.io/api/v3"
"goauthentik.io/internal/outpost/ldap/constants" "goauthentik.io/internal/outpost/ldap/constants"
@ -19,8 +21,8 @@ func (pi *ProviderInstance) UserEntry(u api.User) *ldap.Entry {
} }
attrs = utils.EnsureAttributes(attrs, map[string][]string{ attrs = utils.EnsureAttributes(attrs, map[string][]string{
"memberOf": pi.GroupsForUser(u), "memberOf": pi.GroupsForUser(u),
"goauthentik.io/ldap/active": {utils.BoolToString(*u.IsActive)}, "goauthentik.io/ldap/active": {strconv.FormatBool(*u.IsActive)},
"goauthentik.io/ldap/superuser": {utils.BoolToString(u.IsSuperuser)}, "goauthentik.io/ldap/superuser": {strconv.FormatBool(u.IsSuperuser)},
"cn": {u.Username}, "cn": {u.Username},
"sAMAccountName": {u.Username}, "sAMAccountName": {u.Username},
"uid": {u.Uid}, "uid": {u.Uid},

View File

@ -1,6 +1,8 @@
package group package group
import ( import (
"strconv"
"github.com/nmcclain/ldap" "github.com/nmcclain/ldap"
"goauthentik.io/api/v3" "goauthentik.io/api/v3"
"goauthentik.io/internal/outpost/ldap/constants" "goauthentik.io/internal/outpost/ldap/constants"
@ -30,7 +32,7 @@ func (lg *LDAPGroup) Entry() *ldap.Entry {
attrs = utils.EnsureAttributes(attrs, map[string][]string{ attrs = utils.EnsureAttributes(attrs, map[string][]string{
"objectClass": objectClass, "objectClass": objectClass,
"member": lg.Member, "member": lg.Member,
"goauthentik.io/ldap/superuser": {utils.BoolToString(lg.IsSuperuser)}, "goauthentik.io/ldap/superuser": {strconv.FormatBool(lg.IsSuperuser)},
"cn": {lg.CN}, "cn": {lg.CN},
"uid": {lg.Uid}, "uid": {lg.Uid},
"sAMAccountName": {lg.CN}, "sAMAccountName": {lg.CN},

View File

@ -1,21 +1,14 @@
package utils package utils
import ( import (
"reflect" "fmt"
"strconv"
"strings" "strings"
"github.com/nmcclain/ldap" "github.com/nmcclain/ldap"
log "github.com/sirupsen/logrus"
ldapConstants "goauthentik.io/internal/outpost/ldap/constants" ldapConstants "goauthentik.io/internal/outpost/ldap/constants"
) )
func BoolToString(in bool) string {
if in {
return "true"
}
return "false"
}
func ldapResolveTypeSingle(in interface{}) *string { func ldapResolveTypeSingle(in interface{}) *string {
switch t := in.(type) { switch t := in.(type) {
case string: case string:
@ -23,14 +16,21 @@ func ldapResolveTypeSingle(in interface{}) *string {
case *string: case *string:
return t return t
case bool: case bool:
s := BoolToString(t) s := strconv.FormatBool(t)
return &s return &s
case *bool: case float32:
s := BoolToString(*t) s := strconv.FormatFloat(float64(t), 'f', -1, 64)
return &s
case float64:
s := strconv.FormatFloat(t, 'f', -1, 64)
return &s
case int:
s := strconv.FormatInt(int64(t), 10)
return &s return &s
default: default:
if in != nil { if in != nil {
log.WithField("type", reflect.TypeOf(in).String()).Warning("Type can't be mapped to LDAP yet") s := fmt.Sprintf("%s", in)
return &s
} }
return nil return nil
} }

View File

@ -58,8 +58,7 @@ func TestAKAttrsToLDAP_Dict(t *testing.T) {
} }
assert.Equal(t, 1, len(AKAttrsToLDAP(d))) assert.Equal(t, 1, len(AKAttrsToLDAP(d)))
assert.Equal(t, "foo", AKAttrsToLDAP(d)[0].Name) assert.Equal(t, "foo", AKAttrsToLDAP(d)[0].Name)
// Dicts are currently unsupported, but make sure we don't crash assert.Equal(t, []string{"map[foo:bar]"}, AKAttrsToLDAP(d)[0].Values)
assert.Equal(t, []string([]string(nil)), AKAttrsToLDAP(d)[0].Values)
} }
func TestAKAttrsToLDAP_Mixed(t *testing.T) { func TestAKAttrsToLDAP_Mixed(t *testing.T) {
@ -72,6 +71,5 @@ func TestAKAttrsToLDAP_Mixed(t *testing.T) {
} }
assert.Equal(t, 1, len(AKAttrsToLDAP(d))) assert.Equal(t, 1, len(AKAttrsToLDAP(d)))
assert.Equal(t, "foo", AKAttrsToLDAP(d)[0].Name) assert.Equal(t, "foo", AKAttrsToLDAP(d)[0].Name)
// Dicts are currently unsupported, but make sure we don't crash assert.Equal(t, []string{"foo", "6"}, AKAttrsToLDAP(d)[0].Values)
assert.Equal(t, []string{"foo", ""}, AKAttrsToLDAP(d)[0].Values)
} }