-
Notifications
You must be signed in to change notification settings - Fork 582
Start of the necessary pieces to get #1418 and #1419 implemented #1562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fba0b22
This is the start of the necessary pieces to get #1418 and #1419 impl…
vaikas 7aa7fde
Add placeholder configmap with an example.
vaikas 67f1585
Address lint.
vaikas a8d4221
Use namespaced sharedinformerfactory so that we have the right permis…
vaikas d7c5e58
Check error, duh.
vaikas 95ed2c9
Just trying to remove the files that verify-codegen is complaining.
vaikas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright 2022 The Sigstore Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: config-image-policies | ||
namespace: cosign-system | ||
|
||
data: | ||
_example: | | ||
################################ | ||
# # | ||
# EXAMPLE CONFIGURATION # | ||
# # | ||
################################ | ||
cluster-image-policy-json: "{\"images\":[{\"glob\":\"ghcr.io/example/*\",\"regex\":\"\",\"authorities\":[{\"key\":{\"data\":\"-----BEGIN PUBLIC KEY-----\\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAExB6+H6054/W1SJgs5JR6AJr6J35J\\nRCTfQ5s1kD+hGMSE1rH7s46hmXEeyhnlRnaGF8eMU/SBJE/2NKPnxE7WzQ==\\n-----END PUBLIC KEY-----\"}}]}]}" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// Copyright 2022 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +k8s:deepcopy-gen=package | ||
|
||
// Package config holds the typed objects that define the schemas for | ||
// ConfigMap objects that pertain to our API objects. | ||
// This ConfigMap gets created by the Reconciler by combining all the | ||
// ClusterImagePolicy CR into a single ConfigMap so that the AdmissionController | ||
// only needs to deal with a single resource when validationg. | ||
package config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// | ||
// Copyright 2022 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/sigstore/cosign/pkg/apis/cosigned/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
const ( | ||
// ImagePoliciesConfigName is the name of ConfigMap created by the | ||
// reconciler and consumed by the admission webhook. | ||
ImagePoliciesConfigName = "config-image-policies" | ||
) | ||
|
||
type ImagePolicyConfig struct { | ||
// This is the list of ImagePolicies that a admission controller uses | ||
// to make policy decisions. | ||
// TODO(vaikas): Revisit the datastructure. For now seems fine to use the | ||
// same definitions from the API definition. The _only_ difference is that | ||
// we do not use SecretReference fields, they get resolved and inlined | ||
// in the Secret. So it seemed unnecessary to mirror those all over. | ||
// Key in the map is the name of the ClusterImagePolicy where the Policy | ||
// was received from. | ||
Policies map[string]v1alpha1.ClusterImagePolicySpec | ||
} | ||
|
||
// NewImagePoliciesConfigFromMap creates an ImagePolicyConfig from the supplied | ||
// Map | ||
func NewImagePoliciesConfigFromMap(data map[string]string) (*ImagePolicyConfig, error) { | ||
ret := &ImagePolicyConfig{Policies: make(map[string]v1alpha1.ClusterImagePolicySpec, len(data))} | ||
// Spin through the ConfigMap. Each key will point to resolved | ||
// ImagePatterns. | ||
for k, v := range data { | ||
// This is the example that we use to document / test the ConfigMap. | ||
if k == "_example" { | ||
continue | ||
} | ||
if v == "" { | ||
return nil, fmt.Errorf("configmap has an entry %q but no value", k) | ||
} | ||
clusterImagePolicy := &v1alpha1.ClusterImagePolicySpec{} | ||
|
||
if err := parseEntry(v, clusterImagePolicy); err != nil { | ||
return nil, fmt.Errorf("failed to parse the entry %q : %q : %w", k, v, err) | ||
} | ||
ret.Policies[k] = *clusterImagePolicy | ||
} | ||
return ret, nil | ||
} | ||
|
||
// NewImagePoliciesConfigFromConfigMap creates a Features from the supplied ConfigMap | ||
func NewImagePoliciesConfigFromConfigMap(config *corev1.ConfigMap) (*ImagePolicyConfig, error) { | ||
return NewImagePoliciesConfigFromMap(config.Data) | ||
} | ||
|
||
func parseEntry(entry string, out interface{}) error { | ||
j, err := yaml.YAMLToJSON([]byte(entry)) | ||
if err != nil { | ||
return fmt.Errorf("config's value could not be converted to JSON: %w : %s", err, entry) | ||
} | ||
return json.Unmarshal(j, &out) | ||
} | ||
|
||
// GetAuthorities returns all matching Authorities that need to be matched for | ||
// the given Image. | ||
func (p *ImagePolicyConfig) GetAuthorities(image string) ([]v1alpha1.Authority, error) { | ||
if p == nil { | ||
return nil, errors.New("config is nil") | ||
} | ||
|
||
ret := []v1alpha1.Authority{} | ||
|
||
// TODO(vaikas): this is very inefficient, we should have a better | ||
// way to go from image to Authorities, but just seeing if this is even | ||
// workable so fine for now. | ||
for _, v := range p.Policies { | ||
for _, pattern := range v.Images { | ||
// TODO(vaikas): do the actual glob match. | ||
if image == pattern.Glob { | ||
ret = append(ret, pattern.Authorities...) | ||
} | ||
} | ||
} | ||
return ret, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2022 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
. "knative.dev/pkg/configmap/testing" | ||
_ "knative.dev/pkg/system/testing" | ||
) | ||
|
||
const ( | ||
// Just some public key that was laying around, only format matters. | ||
inlineKeyData = `-----BEGIN PUBLIC KEY----- | ||
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAExB6+H6054/W1SJgs5JR6AJr6J35J | ||
RCTfQ5s1kD+hGMSE1rH7s46hmXEeyhnlRnaGF8eMU/SBJE/2NKPnxE7WzQ== | ||
-----END PUBLIC KEY-----` | ||
) | ||
|
||
func TestDefaultsConfigurationFromFile(t *testing.T) { | ||
_, example := ConfigMapsFromTestFile(t, ImagePoliciesConfigName) | ||
if _, err := NewImagePoliciesConfigFromConfigMap(example); err != nil { | ||
t.Error("NewImagePoliciesConfigFromConfigMap(example) =", err) | ||
} | ||
} | ||
|
||
func TestGetAuthorities(t *testing.T) { | ||
_, example := ConfigMapsFromTestFile(t, ImagePoliciesConfigName) | ||
defaults, err := NewImagePoliciesConfigFromConfigMap(example) | ||
if err != nil { | ||
t.Error("NewImagePoliciesConfigFromConfigMap(example) =", err) | ||
} | ||
c, err := defaults.GetAuthorities("rando") | ||
if err != nil { | ||
t.Error("GetMatches Failed =", err) | ||
} | ||
if len(c) == 0 { | ||
t.Error("Wanted a config, got none.") | ||
} | ||
want := "inlinedata here" | ||
if got := c[0].Key.Data; got != want { | ||
t.Errorf("Did not get what I wanted %q, got %+v", want, c[0].Key.Data) | ||
} | ||
c, err = defaults.GetAuthorities("rando*") | ||
if err != nil { | ||
t.Error("GetMatches Failed =", err) | ||
} | ||
if len(c) == 0 { | ||
t.Error("Wanted a config, got none.") | ||
} | ||
want = "otherinline here" | ||
if got := c[0].Key.Data; got != want { | ||
t.Errorf("Did not get what I wanted %q, got %+v", want, c[0].Key.Data) | ||
} | ||
c, err = defaults.GetAuthorities("rando3") | ||
if err != nil { | ||
t.Error("GetMatches Failed =", err) | ||
} | ||
if len(c) == 0 { | ||
t.Error("Wanted a config, got none.") | ||
} | ||
want = "cakey chilling here" | ||
if got := c[0].Keyless.CAKey.Data; got != want { | ||
t.Errorf("Did not get what I wanted %q, got %+v", want, c[0].Keyless.CAKey.Data) | ||
} | ||
want = "issuer" | ||
if got := c[0].Keyless.Identities[0].Issuer; got != want { | ||
t.Errorf("Did not get what I wanted %q, got %+v", want, c[0].Keyless.Identities[0].Issuer) | ||
} | ||
want = "subject" | ||
if got := c[0].Keyless.Identities[0].Subject; got != want { | ||
t.Errorf("Did not get what I wanted %q, got %+v", want, c[0].Keyless.Identities[0].Subject) | ||
} | ||
// Test multiline yaml cert | ||
c, err = defaults.GetAuthorities("inlinecert") | ||
if err != nil { | ||
t.Error("GetMatches Failed =", err) | ||
} | ||
if len(c) == 0 { | ||
t.Error("Wanted a config, got none.") | ||
} | ||
want = inlineKeyData | ||
if got := c[0].Key.Data; got != want { | ||
t.Errorf("Did not get what I wanted %q, got %+v", want, c[0].Key.Data) | ||
} | ||
// Test multiline cert but json encoded | ||
c, err = defaults.GetAuthorities("ghcr.io/example/*") | ||
if err != nil { | ||
t.Error("GetMatches Failed =", err) | ||
} | ||
if len(c) == 0 { | ||
t.Error("Wanted a config, got none.") | ||
} | ||
want = inlineKeyData | ||
if got := c[0].Key.Data; got != want { | ||
t.Errorf("Did not get what I wanted %q, got %+v", want, c[0].Key.Data) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vaikas Shouldn't you do the same for the NewMutatingAdmisisonController ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, I took it out as per previous PR feedback :) Is there anything in the policy that gets used by mutating?
#1562 (comment)