@@ -16,6 +16,7 @@ import (
16
16
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
17
17
"k8s.io/kubernetes/pkg/util/sets"
18
18
19
+ "github.com/openshift/origin/pkg/api/latest"
19
20
"github.com/openshift/origin/pkg/api/meta"
20
21
"github.com/openshift/origin/pkg/client"
21
22
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
@@ -124,6 +125,34 @@ func (a *imagePolicyPlugin) Validate() error {
124
125
return nil
125
126
}
126
127
128
+ // mutateAttributesToLegacyResources mutates the admission attributes in a way where the
129
+ // Origin API groups are converted to "legacy" or "core" group.
130
+ // This provides a backward compatibility with existing configurations and also closes the
131
+ // hole where clients might bypass the admission by using API group endpoint and API group
132
+ // resource instead of legacy one.
133
+ func mutateAttributesToLegacyResources (attr admission.Attributes ) admission.Attributes {
134
+ resource := attr .GetResource ()
135
+ if len (resource .Group ) > 0 && latest .IsOriginAPIGroup (resource .Group ) {
136
+ resource .Group = ""
137
+ }
138
+ kind := attr .GetKind ()
139
+ if len (kind .Group ) > 0 && latest .IsOriginAPIGroup (kind .Group ) {
140
+ kind .Group = ""
141
+ }
142
+ attrs := admission .NewAttributesRecord (
143
+ attr .GetObject (),
144
+ attr .GetOldObject (),
145
+ attr .GetKind (),
146
+ attr .GetNamespace (),
147
+ attr .GetName (),
148
+ resource ,
149
+ attr .GetSubresource (),
150
+ attr .GetOperation (),
151
+ attr .GetUserInfo (),
152
+ )
153
+ return attrs
154
+ }
155
+
127
156
// Admit attempts to apply the image policy to the incoming resource.
128
157
func (a * imagePolicyPlugin ) Admit (attr admission.Attributes ) error {
129
158
switch attr .GetOperation () {
@@ -138,27 +167,31 @@ func (a *imagePolicyPlugin) Admit(attr admission.Attributes) error {
138
167
return nil
139
168
}
140
169
141
- gr := attr .GetResource ().GroupResource ()
170
+ newAttr := mutateAttributesToLegacyResources (attr )
171
+
172
+ // This will convert any non-legacy Origin resource to a legacy resource, so specifying
173
+ // a 'builds.build.openshift.io' is converted to 'builds'.
174
+ gr := newAttr .GetResource ().GroupResource ()
142
175
if ! a .accepter .Covers (gr ) {
143
176
return nil
144
177
}
145
178
146
- m , err := meta .GetImageReferenceMutator (attr .GetObject ())
179
+ m , err := meta .GetImageReferenceMutator (newAttr .GetObject ())
147
180
if err != nil {
148
- return apierrs .NewForbidden (gr , attr .GetName (), fmt .Errorf ("unable to apply image policy against objects of type %T: %v" , attr .GetObject (), err ))
181
+ return apierrs .NewForbidden (gr , newAttr .GetName (), fmt .Errorf ("unable to apply image policy against objects of type %T: %v" , newAttr .GetObject (), err ))
149
182
}
150
183
151
184
// load exclusion rules from the namespace cache
152
185
var excluded sets.String
153
- if ns := attr .GetNamespace (); len (ns ) > 0 {
186
+ if ns := newAttr .GetNamespace (); len (ns ) > 0 {
154
187
if ns , err := a .projectCache .GetNamespace (ns ); err == nil {
155
188
if value := ns .Annotations [api .IgnorePolicyRulesAnnotation ]; len (value ) > 0 {
156
189
excluded = sets .NewString (strings .Split (value , "," )... )
157
190
}
158
191
}
159
192
}
160
193
161
- if err := accept (a .accepter , a .config .ResolveImages , a .resolver , m , attr , excluded ); err != nil {
194
+ if err := accept (a .accepter , a .config .ResolveImages , a .resolver , m , newAttr , excluded ); err != nil {
162
195
return err
163
196
}
164
197
0 commit comments