Skip to content

Commit c3c59c2

Browse files
committed
Use mapping for LDAP sync/prune w/ Openshift group
When syncing LDAP groups with --type=openshift or when pruning groups, the LDAPGroupUIDToOpenShiftGroupNameMapping should be taken into consideration since: 1. The system of truth in both flows is openshift groups 2. The mapping was probably used to name said openshift groups Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1484831 Signed-off-by: Monis Khan <[email protected]>
1 parent 6cec671 commit c3c59c2

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

pkg/cmd/server/api/validation/ldap.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ func ValidateLDAPSyncConfig(config *api.LDAPSyncConfig) ValidationResults {
1919
bindPassword, _ := api.ResolveStringValue(config.BindPassword)
2020
validationResults.Append(ValidateLDAPClientConfig(config.URL, config.BindDN, bindPassword, config.CA, config.Insecure, nil))
2121

22+
for ldapGroupUID, openShiftGroupName := range config.LDAPGroupUIDToOpenShiftGroupNameMapping {
23+
if len(ldapGroupUID) == 0 || len(openShiftGroupName) == 0 {
24+
validationResults.AddErrors(field.Invalid(field.NewPath("groupUIDNameMapping").Key(ldapGroupUID), openShiftGroupName, "has empty key or value"))
25+
}
26+
}
27+
2228
schemaConfigsFound := []string{}
2329

2430
if config.RFC2307Config != nil {

pkg/oc/admin/groups/sync/cli/prune.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,18 @@ func NewCmdPrune(name, fullName string, f *clientcmd.Factory, out io.Writer) *co
132132

133133
func (o *PruneOptions) Complete(whitelistFile, blacklistFile, configFile string, args []string, f *clientcmd.Factory) error {
134134
var err error
135-
o.Whitelist, err = buildOpenShiftGroupNameList(args, whitelistFile)
135+
136+
o.Config, err = decodeSyncConfigFromFile(configFile)
136137
if err != nil {
137138
return err
138139
}
139140

140-
o.Blacklist, err = buildOpenShiftGroupNameList([]string{}, blacklistFile)
141+
o.Whitelist, err = buildOpenShiftGroupNameList(args, whitelistFile, o.Config.LDAPGroupUIDToOpenShiftGroupNameMapping)
141142
if err != nil {
142143
return err
143144
}
144145

145-
o.Config, err = decodeSyncConfigFromFile(configFile)
146+
o.Blacklist, err = buildOpenShiftGroupNameList([]string{}, blacklistFile, o.Config.LDAPGroupUIDToOpenShiftGroupNameMapping)
146147
if err != nil {
147148
return err
148149
}

pkg/oc/admin/groups/sync/cli/sync.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,18 @@ func (o *SyncOptions) Complete(typeArg, whitelistFile, blacklistFile, configFile
195195
}
196196

197197
var err error
198+
199+
o.Config, err = decodeSyncConfigFromFile(configFile)
200+
if err != nil {
201+
return err
202+
}
203+
198204
if o.Source == GroupSyncSourceOpenShift {
199-
o.Whitelist, err = buildOpenShiftGroupNameList(args, whitelistFile)
205+
o.Whitelist, err = buildOpenShiftGroupNameList(args, whitelistFile, o.Config.LDAPGroupUIDToOpenShiftGroupNameMapping)
200206
if err != nil {
201207
return err
202208
}
203-
o.Blacklist, err = buildOpenShiftGroupNameList([]string{}, blacklistFile)
209+
o.Blacklist, err = buildOpenShiftGroupNameList([]string{}, blacklistFile, o.Config.LDAPGroupUIDToOpenShiftGroupNameMapping)
204210
if err != nil {
205211
return err
206212
}
@@ -215,11 +221,6 @@ func (o *SyncOptions) Complete(typeArg, whitelistFile, blacklistFile, configFile
215221
}
216222
}
217223

218-
o.Config, err = decodeSyncConfigFromFile(configFile)
219-
if err != nil {
220-
return err
221-
}
222-
223224
osClient, _, err := f.Clients()
224225
if err != nil {
225226
return err
@@ -230,13 +231,28 @@ func (o *SyncOptions) Complete(typeArg, whitelistFile, blacklistFile, configFile
230231
}
231232

232233
// buildOpenShiftGroupNameList builds a list of OpenShift names from file and args
233-
func buildOpenShiftGroupNameList(args []string, file string) ([]string, error) {
234+
// nameMapping is used to override the OpenShift names built from file and args
235+
func buildOpenShiftGroupNameList(args []string, file string, nameMapping map[string]string) ([]string, error) {
234236
rawList, err := buildNameList(args, file)
235237
if err != nil {
236238
return nil, err
237239
}
238240

239-
return openshiftGroupNamesOnlyList(rawList)
241+
namesList, err := openshiftGroupNamesOnlyList(rawList)
242+
if err != nil {
243+
return nil, err
244+
}
245+
246+
// override items in namesList if present in mapping
247+
if len(nameMapping) > 0 {
248+
for i, name := range namesList {
249+
if nameOverride, ok := nameMapping[name]; ok {
250+
namesList[i] = nameOverride
251+
}
252+
}
253+
}
254+
255+
return namesList, nil
240256
}
241257

242258
// buildNameLists builds a list from file and args

test/extended/ldap_groups.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ schema=('rfc2307' 'ad' 'augmented-ad')
105105
for (( i=0; i<${#schema[@]}; i++ )); do
106106
current_schema=${schema[$i]}
107107
os::log::info "Testing schema: ${current_schema}"
108+
os::test::junit::declare_suite_start "extended/ldap-groups/${current_schema}"
108109

109110
WORKINGDIR=${BASETMPDIR}/${current_schema}
110111
mkdir ${WORKINGDIR}
@@ -209,6 +210,14 @@ for (( i=0; i<${#schema[@]}; i++ )); do
209210
oc adm groups sync --sync-config=sync-config-dn-everywhere.yaml --confirm
210211
compare_and_cleanup valid_all_ldap_sync_dn_everywhere.yaml
211212

213+
echo -e "\tTEST: Sync based on OpenShift groups respecting OpenShift mappings and whitelist file"
214+
os::cmd::expect_success_and_text 'oc adm groups sync --whitelist=ldapgroupuids.txt --sync-config=sync-config-user-defined.yaml --confirm' 'group/'
215+
os::cmd::expect_success_and_text 'oc get group -o jsonpath={.items[*].metadata.name}' 'firstgroup secondgroup thirdgroup'
216+
os::cmd::expect_success_and_text 'oc adm groups sync --type=openshift --whitelist=ldapgroupuids.txt --sync-config=sync-config-user-defined.yaml --confirm' 'group/'
217+
os::cmd::expect_success_and_text 'oc get group -o jsonpath={.items[*].metadata.name}' 'firstgroup secondgroup thirdgroup'
218+
os::cmd::expect_success_and_text 'oc delete groups --all' 'deleted'
219+
os::cmd::expect_success_and_text 'oc get group -o jsonpath={.items[*].metadata.name} | wc -l' '0'
220+
212221

213222
# PRUNING
214223
echo -e "\tTEST: Sync all LDAP groups from LDAP server, change LDAP UID, then prune OpenShift groups"
@@ -217,11 +226,25 @@ for (( i=0; i<${#schema[@]}; i++ )); do
217226
oc adm groups prune --sync-config=sync-config.yaml --confirm
218227
compare_and_cleanup valid_all_ldap_sync_prune.yaml
219228

229+
echo -e "\tTEST: Sync all LDAP groups from LDAP server using whitelist file, then prune OpenShift groups using the same whitelist file"
230+
os::cmd::expect_success_and_text 'oc adm groups sync --whitelist=ldapgroupuids.txt --sync-config=sync-config-user-defined.yaml --confirm' 'group/'
231+
os::cmd::expect_success_and_text 'oc get group -o jsonpath={.items[*].metadata.name}' 'firstgroup secondgroup thirdgroup'
232+
os::cmd::expect_success_and_text 'oc adm groups prune --whitelist=ldapgroupuids.txt --sync-config=sync-config-user-defined.yaml --confirm | wc -l' '0'
233+
os::cmd::expect_success_and_text 'oc get group -o jsonpath={.items[*].metadata.name}' 'firstgroup secondgroup thirdgroup'
234+
os::cmd::expect_success_and_text 'oc patch group secondgroup -p "{\"metadata\":{\"annotations\":{\"openshift.io/ldap.uid\":\"cn=garbage\"}}}"' 'group "secondgroup" patched'
235+
os::cmd::expect_success_and_text 'oc adm groups prune --whitelist=ldapgroupuids.txt --sync-config=sync-config-user-defined.yaml --confirm' 'group/secondgroup'
236+
os::cmd::expect_success_and_text 'oc get group -o jsonpath={.items[*].metadata.name}' 'firstgroup thirdgroup'
237+
os::cmd::expect_success_and_text 'oc delete groups --all' 'deleted'
238+
os::cmd::expect_success_and_text 'oc get group -o jsonpath={.items[*].metadata.name} | wc -l' '0'
239+
240+
220241
# PAGING
221242
echo -e "\tTEST: Sync all LDAP groups from LDAP server using paged queries"
222243
oc adm groups sync --sync-config=sync-config-paging.yaml --confirm
223244
compare_and_cleanup valid_all_ldap_sync.yaml
224245

246+
247+
os::test::junit::declare_suite_end
225248
popd > /dev/null
226249
done
227250

0 commit comments

Comments
 (0)