forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admission.go
108 lines (87 loc) · 2.94 KB
/
admission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package secretinjector
import (
"io"
"net/url"
"strings"
"github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/admission"
restclient "k8s.io/client-go/rest"
api "k8s.io/kubernetes/pkg/apis/core"
authclient "github.com/openshift/origin/pkg/auth/client"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
"github.com/openshift/origin/pkg/util/urlpattern"
)
func Register(plugins *admission.Plugins) {
plugins.Register("openshift.io/BuildConfigSecretInjector",
func(config io.Reader) (admission.Interface, error) {
return &secretInjector{
Handler: admission.NewHandler(admission.Create),
}, nil
})
}
type secretInjector struct {
*admission.Handler
restClientConfig restclient.Config
}
var _ = oadmission.WantsRESTClientConfig(&secretInjector{})
func (si *secretInjector) Admit(attr admission.Attributes) (err error) {
bc, ok := attr.GetObject().(*buildapi.BuildConfig)
if !ok {
return nil
}
if bc.Spec.Source.SourceSecret != nil || bc.Spec.Source.Git == nil {
return nil
}
client, err := authclient.NewImpersonatingKubernetesClientset(attr.GetUserInfo(), si.restClientConfig)
if err != nil {
glog.V(2).Infof("secretinjector: could not create client: %v", err)
return nil
}
namespace := attr.GetNamespace()
url, err := url.Parse(bc.Spec.Source.Git.URI)
if err != nil {
glog.V(2).Infof(`secretinjector: buildconfig "%s/%s": URI %q parse failed: %v`, namespace, bc.GetName(), bc.Spec.Source.Git.URI, err)
return nil
}
secrets, err := client.Core().Secrets(namespace).List(metav1.ListOptions{})
if err != nil {
glog.V(2).Infof("secretinjector: failed to list Secrets: %v", err)
return nil
}
patterns := []*urlpattern.URLPattern{}
for _, secret := range secrets.Items {
if secret.Type == api.SecretTypeBasicAuth && url.Scheme == "ssh" ||
secret.Type == api.SecretTypeSSHAuth && url.Scheme != "ssh" {
continue
}
for k, v := range secret.GetAnnotations() {
if strings.HasPrefix(k, buildapi.BuildSourceSecretMatchURIAnnotationPrefix) {
v = strings.TrimSpace(v)
if v == "" {
continue
}
pattern, err := urlpattern.NewURLPattern(v)
if err != nil {
glog.V(2).Infof(`secretinjector: buildconfig "%s/%s": unparseable annotation %q: %v`, namespace, bc.GetName(), k, err)
continue
}
pattern.Cookie = secret.GetName()
patterns = append(patterns, pattern)
}
}
}
if match := urlpattern.Match(patterns, url); match != nil {
secretName := match.Cookie.(string)
glog.V(4).Infof(`secretinjector: matched secret "%s/%s" to buildconfig "%s"`, namespace, secretName, bc.GetName())
bc.Spec.Source.SourceSecret = &api.LocalObjectReference{Name: secretName}
}
return nil
}
func (si *secretInjector) SetRESTClientConfig(restClientConfig restclient.Config) {
si.restClientConfig = restClientConfig
}
func (si *secretInjector) ValidateInitialization() error {
return nil
}