Skip to content
Snippets Groups Projects
Commit b1f4bd01 authored by Oded Ben-Ozer's avatar Oded Ben-Ozer
Browse files

Address issues raised in review:

- Add missing json tag.
- Control delimiter cleaning with a configuration key.
- Use better variable names
- concatenate string using slice and join

Signed-off-by: default avatarOded Ben-Ozer <obenozer@wayfair.com>
parent a5284841
No related branches found
No related tags found
No related merge requests found
...@@ -91,7 +91,7 @@ type Config struct { ...@@ -91,7 +91,7 @@ type Config struct {
// ClaimModifications holds all claim modifications options, current has only newGroupsFromClaims // ClaimModifications holds all claim modifications options, current has only newGroupsFromClaims
ClaimModifications struct { ClaimModifications struct {
NewGroupsFromClaims []NewGroupsFromClaims `json:"newGroupsFromClaims"` NewGroupsFromClaims []NewGroupsFromClaims `json:"newGroupsFromClaims"`
} } `json:"claimModifications"`
} }
// List of groups claim elements to create by concatenating other claims // List of groups claim elements to create by concatenating other claims
...@@ -102,6 +102,10 @@ type NewGroupsFromClaims struct { ...@@ -102,6 +102,10 @@ type NewGroupsFromClaims struct {
// String to separate the claims // String to separate the claims
Delimiter string `json:"delimiter"` Delimiter string `json:"delimiter"`
// Should Dex remove the Delimiter string from claim values
// This is done to keep resulting claim structure in full control of the Dex operator
ClearDelimiter bool `json:"clearDelimiter"`
// String to place before the first claim // String to place before the first claim
Prefix string `json:"prefix"` Prefix string `json:"prefix"`
} }
...@@ -446,23 +450,26 @@ func (c *oidcConnector) createIdentity(ctx context.Context, identity connector.I ...@@ -446,23 +450,26 @@ func (c *oidcConnector) createIdentity(ctx context.Context, identity connector.I
} }
} }
for _, cc := range c.newGroupsFromClaims { for _, newGroupsElementConfig := range c.newGroupsFromClaims {
newElement := "" newGroupsElement := []string{
for _, clm := range cc.ClaimList { newGroupsElementConfig.Prefix,
}
for _, claimName := range newGroupsElementConfig.ClaimList {
// Non string claim value are ignored, concatenating them doesn't really make any sense // Non string claim value are ignored, concatenating them doesn't really make any sense
if claimValue, ok := claims[clm].(string); ok { if claimValue, ok := claims[claimName].(string); ok {
// Removing the delimiier string from the concatenated claim to ensure resulting claim structure
// is in full control of Dex operator if newGroupsElementConfig.ClearDelimiter {
claimCleanedValue := strings.ReplaceAll(claimValue, cc.Delimiter, "") // Removing the delimiier string from the concatenated claim to ensure resulting claim structure
if newElement == "" { // is in full control of Dex operator
newElement = cc.Prefix + cc.Delimiter + claimCleanedValue claimCleanedValue := strings.ReplaceAll(claimValue, newGroupsElementConfig.Delimiter, "")
newGroupsElement = append(newGroupsElement, claimCleanedValue)
} else { } else {
newElement = newElement + cc.Delimiter + claimCleanedValue newGroupsElement = append(newGroupsElement, claimValue)
} }
} }
} }
if newElement != "" { if len(newGroupsElement) > 1 {
groups = append(groups, newElement) groups = append(groups, strings.Join(newGroupsElement, newGroupsElementConfig.Delimiter))
} }
} }
......
...@@ -295,7 +295,7 @@ func TestHandleCallback(t *testing.T) { ...@@ -295,7 +295,7 @@ func TestHandleCallback(t *testing.T) {
userNameKey: "", // not configured userNameKey: "", // not configured
expectUserID: "subvalue", expectUserID: "subvalue",
expectUserName: "namevalue", expectUserName: "namevalue",
expectGroups: []string{"group1", "gh::acme::pipeline-one", "tfe-acme-foobar", "bk-emailvalue"}, expectGroups: []string{"group1", "gh::acme::pipeline-one", "clr_delim-acme-foobar", "keep_delim-acme-foo-bar", "bk-emailvalue"},
expectedEmailField: "emailvalue", expectedEmailField: "emailvalue",
newGroupsFromClaims: []NewGroupsFromClaims{ newGroupsFromClaims: []NewGroupsFromClaims{
{ // The basic functionality, should create "gh::acme::pipeline-one". { // The basic functionality, should create "gh::acme::pipeline-one".
...@@ -316,13 +316,24 @@ func TestHandleCallback(t *testing.T) { ...@@ -316,13 +316,24 @@ func TestHandleCallback(t *testing.T) {
}, },
{ // In this case the delimiter character("-") should be removed removed from "claim-with-delimiter" claim to ensure the resulting { // In this case the delimiter character("-") should be removed removed from "claim-with-delimiter" claim to ensure the resulting
// claim structure is in full control of the Dex operator and not the person creating a new pipeline. // claim structure is in full control of the Dex operator and not the person creating a new pipeline.
// Should create "tfe-acme-foobar" and not "tfe-acme-foo-bar". // Should create "clr_delim-acme-foobar" and not "tfe-acme-foo-bar".
ClaimList: []string{
"organization",
"claim-with-delimiter",
},
Delimiter: "-",
ClearDelimiter: true,
Prefix: "clr_delim",
},
{ // In this case the delimiter character("-") should be NOT removed from "claim-with-delimiter" claim.
// Should create "keep_delim-acme-foo-bar".
ClaimList: []string{ ClaimList: []string{
"organization", "organization",
"claim-with-delimiter", "claim-with-delimiter",
}, },
Delimiter: "-", Delimiter: "-",
Prefix: "tfe", // ClearDelimiter: false,
Prefix: "keep_delim",
}, },
{ // Ignore non string claims (like arrays), this should result in "bk-emailvalue". { // Ignore non string claims (like arrays), this should result in "bk-emailvalue".
ClaimList: []string{ ClaimList: []string{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment