|
| 1 | +// |
| 2 | +// Copyright 2022 The Sigstore Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "io/ioutil" |
| 23 | + "log" |
| 24 | + "os" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/google/go-containerregistry/pkg/authn" |
| 28 | + "github.com/google/go-containerregistry/pkg/name" |
| 29 | + "github.com/google/go-containerregistry/pkg/v1/remote" |
| 30 | + ociremote "github.com/sigstore/cosign/pkg/oci/remote" |
| 31 | + "go.uber.org/zap" |
| 32 | + "knative.dev/pkg/logging" |
| 33 | + "sigs.k8s.io/yaml" |
| 34 | + |
| 35 | + "github.com/sigstore/policy-controller/pkg/apis/policy/v1alpha1" |
| 36 | + "github.com/sigstore/policy-controller/pkg/webhook" |
| 37 | + webhookcip "github.com/sigstore/policy-controller/pkg/webhook/clusterimagepolicy" |
| 38 | +) |
| 39 | + |
| 40 | +var ( |
| 41 | + ns = "unused" |
| 42 | + |
| 43 | + remoteOpts = []ociremote.Option{ |
| 44 | + ociremote.WithRemoteOptions( |
| 45 | + remote.WithAuthFromKeychain(authn.DefaultKeychain), |
| 46 | + ), |
| 47 | + } |
| 48 | + |
| 49 | + ctx = logging.WithLogger(context.Background(), func() *zap.SugaredLogger { |
| 50 | + x, _ := zap.NewDevelopmentConfig().Build() |
| 51 | + return x.Sugar() |
| 52 | + }()) |
| 53 | +) |
| 54 | + |
| 55 | +type output struct { |
| 56 | + Errors []string `json:"errors"` |
| 57 | + Result *webhook.PolicyResult `json:"result"` |
| 58 | +} |
| 59 | + |
| 60 | +func main() { |
| 61 | + args := os.Args[1:] |
| 62 | + if len(args) != 2 { |
| 63 | + fmt.Println("Usage: policy-tester cluster-image-policy.yaml r.example.com/myrepo/myimage:mytag") |
| 64 | + os.Exit(1) |
| 65 | + } |
| 66 | + cipFilePath := args[0] |
| 67 | + image := args[1] |
| 68 | + cipRaw, err := ioutil.ReadFile(cipFilePath) |
| 69 | + if err != nil { |
| 70 | + log.Fatal(err) |
| 71 | + } |
| 72 | + |
| 73 | + // TODO(jdolitsky): should this use v1beta1? |
| 74 | + var tmp v1alpha1.ClusterImagePolicy |
| 75 | + if err := yaml.Unmarshal(cipRaw, &tmp); err != nil { |
| 76 | + log.Fatal(err) |
| 77 | + } |
| 78 | + cip := webhookcip.ConvertClusterImagePolicyV1alpha1ToWebhook(&tmp) |
| 79 | + ref, err := name.ParseReference(image) |
| 80 | + if err != nil { |
| 81 | + log.Fatal(err) |
| 82 | + } |
| 83 | + |
| 84 | + result, errs := webhook.ValidatePolicy(ctx, ns, ref, *cip, remoteOpts...) |
| 85 | + errStrings := []string{} |
| 86 | + for _, err := range errs { |
| 87 | + errStrings = append(errStrings, strings.Trim(err.Error(), "\n")) |
| 88 | + } |
| 89 | + o, err := json.Marshal(&output{ |
| 90 | + Errors: errStrings, |
| 91 | + Result: result, |
| 92 | + }) |
| 93 | + if err != nil { |
| 94 | + log.Fatal(err) |
| 95 | + } |
| 96 | + |
| 97 | + fmt.Println(string(o)) |
| 98 | + if len(errs) > 0 { |
| 99 | + os.Exit(1) |
| 100 | + } |
| 101 | +} |
0 commit comments