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

Centralize DN Canoicalization and minor improvements #74

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ password: `admin`
After you login you can create new resources to be synced by baton.

After creating new resources on the LDAP server, use the `baton-ldap` cli to sync the data from the LDAP server with the example command below.
`baton-ldap --base-dn dc=example,dc=org --user-dn cn=admin,dc=example,dc=org --password admin --domain localhost`
`baton-ldap --base-dn dc=example,dc=org --bind-dn cn=admin,dc=example,dc=org --password admin --domain localhost`

After successfully syncing data, use the baton CLI to list the resources and see the synced data.
`baton resources`
Expand Down
25 changes: 13 additions & 12 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"fmt"
"net/url"

"github.com/conductorone/baton-ldap/pkg/ldap"
"github.com/conductorone/baton-sdk/pkg/field"
"github.com/go-ldap/ldap/v3"
ldap3 "github.com/go-ldap/ldap/v3"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -87,23 +88,23 @@ func New(ctx context.Context, v *viper.Viper) (*Config, error) {
}

if baseDNValue := v.GetString(baseDNField.FieldName); baseDNValue != "" {
baseDN, err := ldap.ParseDN(baseDNValue)
baseDN, err := ldap.CanonicalizeDN(baseDNValue)
if err != nil {
return nil, fmt.Errorf("error parsing base-dn: %w", err)
}
rv.BaseDN = baseDN
pquerna marked this conversation as resolved.
Show resolved Hide resolved
}

if userDNValue := v.GetString(userDNField.FieldName); userDNValue != "" {
userDN, err := ldap.ParseDN(userDNValue)
userDN, err := ldap.CanonicalizeDN(userDNValue)
if err != nil {
return nil, fmt.Errorf("error parsing user-dn: %w", err)
}
rv.BindDN = userDN
pquerna marked this conversation as resolved.
Show resolved Hide resolved
}

if bindDNValue := v.GetString(bindDNField.FieldName); bindDNValue != "" {
bindDN, err := ldap.ParseDN(bindDNValue)
bindDN, err := ldap.CanonicalizeDN(bindDNValue)
if err != nil {
return nil, fmt.Errorf("error parsing bind-dn: %w", err)
}
Expand All @@ -115,7 +116,7 @@ func New(ctx context.Context, v *viper.Viper) (*Config, error) {
}

if userSearchDNValue := v.GetString(userSearchDNField.FieldName); userSearchDNValue != "" {
userSearchDN, err := ldap.ParseDN(userSearchDNValue)
userSearchDN, err := ldap.CanonicalizeDN(userSearchDNValue)
if err != nil {
return nil, fmt.Errorf("error parsing user-search-dn: %w", err)
}
Expand All @@ -125,7 +126,7 @@ func New(ctx context.Context, v *viper.Viper) (*Config, error) {
}

if groupSearchDNValue := v.GetString(groupSearchDNField.FieldName); groupSearchDNValue != "" {
groupSearchDN, err := ldap.ParseDN(groupSearchDNValue)
groupSearchDN, err := ldap.CanonicalizeDN(groupSearchDNValue)
if err != nil {
return nil, fmt.Errorf("error parsing group-search-dn: %w", err)
}
Expand All @@ -135,7 +136,7 @@ func New(ctx context.Context, v *viper.Viper) (*Config, error) {
}

if roleSearchDNValue := v.GetString(roleSearchDNField.FieldName); roleSearchDNValue != "" {
roleSearchDN, err := ldap.ParseDN(roleSearchDNValue)
roleSearchDN, err := ldap.CanonicalizeDN(roleSearchDNValue)
if err != nil {
return nil, fmt.Errorf("error parsing role-search-dn: %w", err)
}
Expand All @@ -157,14 +158,14 @@ func New(ctx context.Context, v *viper.Viper) (*Config, error) {

type Config struct {
ServerURL *url.URL
BaseDN *ldap.DN
BaseDN *ldap3.DN

BindPassword string
BindDN *ldap.DN
BindDN *ldap3.DN

UserSearchDN *ldap.DN
GroupSearchDN *ldap.DN
RoleSearchDN *ldap.DN
UserSearchDN *ldap3.DN
GroupSearchDN *ldap3.DN
RoleSearchDN *ldap3.DN
pquerna marked this conversation as resolved.
Show resolved Hide resolved

DisableOperationalAttrs bool
InsecureSkipVerify bool
Expand Down
18 changes: 12 additions & 6 deletions pkg/connector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,13 @@ func createConnector(ctx context.Context, t *testing.T, fixtureName string) (*LD
return nil, err
}

bindDN, err := ldap.ParseDN("cn=admin,dc=example,dc=org")
if err != nil {
return nil, err
}

cf := &config.Config{
ServerURL: sux,
BindDN: bindDN,
BindDN: mustParseDN(t, "cn=admin,dc=example,dc=org"),
BaseDN: mustParseDN(t, "dc=example,dc=org"),
GroupSearchDN: mustParseDN(t, "ou=groups,dc=example,dc=org"),
UserSearchDN: mustParseDN(t, "ou=users,dc=example,dc=org"),
RoleSearchDN: mustParseDN(t, "ou=roles,dc=example,dc=org"),
BindPassword: "hunter2",
}
return New(ctx, cf)
Expand All @@ -102,3 +98,13 @@ func mustParseDN(t *testing.T, input string) *ldap.DN {
require.NoError(t, err)
return dn
}

func pluck[T any](slice []T, fn func(v T) bool) T {
var emptyT T
for _, v := range slice {
if fn(v) {
return v
}
}
return emptyT
}
Loading
Loading