Skip to content

Commit 85a5ea3

Browse files
author
Jim Minter
committed
Disallow @ character in URL patterns, so that people don't mistakenly try
to add URL patterns of the form username@hostname/path. Extend admission controller to reject invalid URL patterns on secrets to provide early feedback to end users when their patterns are wrong.
1 parent 11034b2 commit 85a5ea3

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

pkg/build/admission/secretinjector/admission.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010

1111
"k8s.io/kubernetes/pkg/admission"
1212
"k8s.io/kubernetes/pkg/api"
13+
"k8s.io/kubernetes/pkg/api/errors"
1314
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
1415
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
1516
"k8s.io/kubernetes/pkg/client/restclient"
17+
"k8s.io/kubernetes/pkg/util/validation/field"
1618

1719
authclient "github.com/openshift/origin/pkg/auth/client"
1820
buildapi "github.com/openshift/origin/pkg/build/api"
@@ -23,7 +25,7 @@ import (
2325
func init() {
2426
admission.RegisterPlugin("openshift.io/BuildConfigSecretInjector", func(c clientset.Interface, config io.Reader) (admission.Interface, error) {
2527
return &secretInjector{
26-
Handler: admission.NewHandler(admission.Create),
28+
Handler: admission.NewHandler(admission.Create, admission.Update),
2729
}, nil
2830
})
2931
}
@@ -36,11 +38,20 @@ type secretInjector struct {
3638
var _ = oadmission.WantsRESTClientConfig(&secretInjector{})
3739

3840
func (si *secretInjector) Admit(attr admission.Attributes) (err error) {
39-
bc, ok := attr.GetObject().(*buildapi.BuildConfig)
40-
if !ok {
41-
return nil
41+
obj := attr.GetObject()
42+
43+
if bc, ok := obj.(*buildapi.BuildConfig); ok && attr.GetOperation() == admission.Create {
44+
return si.admitNewBuildConfig(attr, bc)
4245
}
4346

47+
if secret, ok := obj.(*api.Secret); ok {
48+
return si.admitSecret(attr, secret)
49+
}
50+
51+
return nil
52+
}
53+
54+
func (si *secretInjector) admitNewBuildConfig(attr admission.Attributes, bc *buildapi.BuildConfig) (err error) {
4455
if bc.Spec.Source.SourceSecret != nil || bc.Spec.Source.Git == nil {
4556
return nil
4657
}
@@ -105,6 +116,30 @@ func (si *secretInjector) Admit(attr admission.Attributes) (err error) {
105116
return nil
106117
}
107118

119+
func (si *secretInjector) admitSecret(attr admission.Attributes, secret *api.Secret) (err error) {
120+
errs := field.ErrorList{}
121+
122+
for k, v := range secret.GetAnnotations() {
123+
if strings.HasPrefix(k, buildapi.BuildSourceSecretMatchURIAnnotationPrefix) {
124+
v = strings.TrimSpace(v)
125+
if v == "" {
126+
continue
127+
}
128+
129+
_, err := urlpattern.NewURLPattern(v)
130+
if err != nil {
131+
errs = append(errs, field.Invalid(field.NewPath("metadata.annotations", k), v, err.Error()))
132+
}
133+
}
134+
}
135+
136+
if len(errs) > 0 {
137+
return errors.NewInvalid(api.Kind("secret"), secret.Name, errs)
138+
}
139+
140+
return nil
141+
}
142+
108143
func (si *secretInjector) SetRESTClientConfig(restClientConfig restclient.Config) {
109144
si.restClientConfig = restClientConfig
110145
}

pkg/util/urlpattern/urlpattern.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var InvalidPatternError = errors.New("invalid pattern")
1212

1313
var urlPatternRegex = regexp.MustCompile(`^` +
1414
`(?:(\*|git|http|https|ssh)://)` +
15-
`(\*|(?:\*\.)?[^/*]+)` +
15+
`(\*|(?:\*\.)?[^@/*]+)` +
1616
`(/.*)` +
1717
`$`)
1818

pkg/util/urlpattern/urlpattern_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ func TestMatchPattern(t *testing.T) {
108108
pattern: `https://git*hub.com/*`,
109109
expectedErr: true,
110110
},
111+
{
112+
pattern: `*://[email protected]/*`,
113+
expectedErr: true,
114+
},
111115
{
112116
pattern: `https://github.com/`,
113117
expectedScheme: `^https$`,

0 commit comments

Comments
 (0)