Skip to content

Commit

Permalink
Disallow @ character in URL patterns, so that people don't mistakenly…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
Jim Minter committed Mar 7, 2017
1 parent 11034b2 commit 85a5ea3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
43 changes: 39 additions & 4 deletions pkg/build/admission/secretinjector/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (

"k8s.io/kubernetes/pkg/admission"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
"k8s.io/kubernetes/pkg/client/restclient"
"k8s.io/kubernetes/pkg/util/validation/field"

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

func (si *secretInjector) Admit(attr admission.Attributes) (err error) {
bc, ok := attr.GetObject().(*buildapi.BuildConfig)
if !ok {
return nil
obj := attr.GetObject()

if bc, ok := obj.(*buildapi.BuildConfig); ok && attr.GetOperation() == admission.Create {
return si.admitNewBuildConfig(attr, bc)
}

if secret, ok := obj.(*api.Secret); ok {
return si.admitSecret(attr, secret)
}

return nil
}

func (si *secretInjector) admitNewBuildConfig(attr admission.Attributes, bc *buildapi.BuildConfig) (err error) {
if bc.Spec.Source.SourceSecret != nil || bc.Spec.Source.Git == nil {
return nil
}
Expand Down Expand Up @@ -105,6 +116,30 @@ func (si *secretInjector) Admit(attr admission.Attributes) (err error) {
return nil
}

func (si *secretInjector) admitSecret(attr admission.Attributes, secret *api.Secret) (err error) {
errs := field.ErrorList{}

for k, v := range secret.GetAnnotations() {
if strings.HasPrefix(k, buildapi.BuildSourceSecretMatchURIAnnotationPrefix) {
v = strings.TrimSpace(v)
if v == "" {
continue
}

_, err := urlpattern.NewURLPattern(v)
if err != nil {
errs = append(errs, field.Invalid(field.NewPath("metadata.annotations", k), v, err.Error()))
}
}
}

if len(errs) > 0 {
return errors.NewInvalid(api.Kind("secret"), secret.Name, errs)
}

return nil
}

func (si *secretInjector) SetRESTClientConfig(restClientConfig restclient.Config) {
si.restClientConfig = restClientConfig
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/urlpattern/urlpattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var InvalidPatternError = errors.New("invalid pattern")

var urlPatternRegex = regexp.MustCompile(`^` +
`(?:(\*|git|http|https|ssh)://)` +
`(\*|(?:\*\.)?[^/*]+)` +
`(\*|(?:\*\.)?[^@/*]+)` +
`(/.*)` +
`$`)

Expand Down
4 changes: 4 additions & 0 deletions pkg/util/urlpattern/urlpattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ func TestMatchPattern(t *testing.T) {
pattern: `https://git*hub.com/*`,
expectedErr: true,
},
{
pattern: `*://git@github.com/*`,
expectedErr: true,
},
{
pattern: `https://github.com/`,
expectedScheme: `^https$`,
Expand Down

0 comments on commit 85a5ea3

Please sign in to comment.