Skip to content

Commit

Permalink
fix(controller): make creator label DNS compliant. Fixes #4880 (#4881)
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Meaney <emeaney@motus.com>
  • Loading branch information
ermeaney authored and simster7 committed Jan 19, 2021
1 parent 84b44cf commit 9063a94
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions USERS.md
Expand Up @@ -69,6 +69,7 @@ Currently, the following organizations are **officially** using Argo Workflows:
1. [Microblink](https://microblink.com/)
1. [Mirantis](https://mirantis.com/)
1. [Mixpanel](https://mixpanel.com)
1. [Motus](https://www.motus.com)
1. [New Relic](https://newrelic.com/)
1. [Nikkei](https://www.nikkei.co.jp/nikkeiinfo/en/)
1. [Norwegian Refugee Council](https://www.nrc.no/)
Expand Down
12 changes: 11 additions & 1 deletion workflow/creator/creator.go
Expand Up @@ -15,11 +15,21 @@ import (
func Label(ctx context.Context, obj metav1.Object) {
claims := auth.GetClaims(ctx)
if claims != nil {
value := regexp.MustCompile("[^-_.a-z0-9A-Z]").ReplaceAllString(claims.Subject, "-")
value := dnsFriendly(claims.Subject)
if len(value) > 63 {
value = value[len(value)-63:]
}
value = strings.TrimLeft(value, "-")
labels.Label(obj, common.LabelKeyCreator, value)
}
}

func dnsFriendly(s string) string {
value := regexp.MustCompile("[^-_.a-z0-9A-Z]").ReplaceAllString(s, "-")
value = regexp.MustCompile("^[^a-z0-9A-Z]*").ReplaceAllString(value, "")
value = regexp.MustCompile("[^a-z0-9A-Z]*$").ReplaceAllString(value, "")
if len(value) > 63 {
value = value[len(value)-63:]
}
return value
}
7 changes: 7 additions & 0 deletions workflow/creator/creator_test.go
Expand Up @@ -42,4 +42,11 @@ func TestLabel(t *testing.T) {
assert.Equal(t, "y", wf.Labels[common.LabelKeyCreator])
}
})
t.Run("InvalidDNSNames", func(t *testing.T) {
wf := &wfv1.Workflow{}
Label(context.WithValue(context.TODO(), auth.ClaimsKey, &types.Claims{Claims: jwt.Claims{Subject: "!@#$%^&*()--__" + strings.Repeat("y", 35) + "__--!@#$%^&*()"}}), wf)
if assert.NotEmpty(t, wf.Labels) {
assert.Equal(t, strings.Repeat("y", 35), wf.Labels[common.LabelKeyCreator])
}
})
}

0 comments on commit 9063a94

Please sign in to comment.