|
| 1 | +// Copyright The Notary Project Authors. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package policy |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/json" |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + |
| 21 | + "github.com/notaryproject/notation-go/dir" |
| 22 | + "github.com/notaryproject/notation-go/verifier/trustpolicy" |
| 23 | + "github.com/notaryproject/notation/v2/cmd/notation/internal/display" |
| 24 | + "github.com/notaryproject/notation/v2/cmd/notation/internal/display/output" |
| 25 | + "github.com/notaryproject/notation/v2/internal/osutil" |
| 26 | + "github.com/spf13/cobra" |
| 27 | +) |
| 28 | + |
| 29 | +// wildcardRegistryScope is the registry scope that matches any registry. It is |
| 30 | +// the default scope for a starter policy so verification applies everywhere |
| 31 | +// until the user narrows it down. |
| 32 | +const wildcardRegistryScope = "*" |
| 33 | + |
| 34 | +type initOpts struct { |
| 35 | + printer *output.Printer |
| 36 | + name string |
| 37 | + registryScopes []string |
| 38 | + trustStores []string |
| 39 | + trustedIdentities []string |
| 40 | + force bool |
| 41 | +} |
| 42 | + |
| 43 | +func initCmd() *cobra.Command { |
| 44 | + opts := initOpts{} |
| 45 | + command := &cobra.Command{ |
| 46 | + Use: `init [flags] --name <policy_name> --trust-store "<store_type>:<store_name>" --trusted-identity "<trusted_identity>"`, |
| 47 | + Short: "Initialize OCI trust policy configuration", |
| 48 | + Long: `Initialize OCI trust policy configuration. |
| 49 | +
|
| 50 | +The generated policy statement applies to all registries by default (registry scope "*"). Use --registry-scope to pin it to specific repositories. |
| 51 | +
|
| 52 | +Example - init an OCI trust policy configuration with a trust store and a trusted identity: |
| 53 | + notation policy init --name examplePolicy --trust-store ca:exampleStore --trusted-identity "x509.subject: C=US, ST=WA, O=acme-rockets.io" |
| 54 | +
|
| 55 | +Example - init an OCI trust policy configuration scoped to specific repositories: |
| 56 | + notation policy init --name examplePolicy --registry-scope registry.acme-rockets.io/software/net-monitor --trust-store ca:exampleStore --trusted-identity "x509.subject: C=US, ST=WA, O=acme-rockets.io" |
| 57 | +
|
| 58 | +Example - init an OCI trust policy configuration with multiple trust stores and trusted identities: |
| 59 | + notation policy init --name examplePolicy --trust-store ca:exampleStore --trust-store ca:exampleStore2 --trusted-identity "x509.subject: C=US, ST=WA, O=acme-rockets.io" --trusted-identity "x509.subject: C=US, ST=WA, L=Seattle, O=wabbit-networks.io" |
| 60 | +
|
| 61 | +Example - init an OCI trust policy configuration with any trusted identity: |
| 62 | + notation policy init --name examplePolicy --trust-store ca:exampleStore --trusted-identity "*" |
| 63 | +
|
| 64 | +Example - init an OCI trust policy configuration without prompt: |
| 65 | + notation policy init --name examplePolicy --trust-store ca:exampleStore --trusted-identity "x509.subject: C=US, ST=WA, O=acme-rockets.io" --force |
| 66 | +`, |
| 67 | + Args: cobra.ExactArgs(0), |
| 68 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 69 | + opts.printer = output.NewPrinter(cmd.OutOrStdout(), cmd.OutOrStderr()) |
| 70 | + }, |
| 71 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 72 | + return runInit(&opts) |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + command.Flags().StringVarP(&opts.name, "name", "n", "", "name of the OCI trust policy") |
| 77 | + command.Flags().StringArrayVar(&opts.registryScopes, "registry-scope", []string{wildcardRegistryScope}, "registry scope the policy applies to, e.g. \"registry.acme-rockets.io/software/net-monitor\"; defaults to \"*\" for all registries") |
| 78 | + command.Flags().StringArrayVar(&opts.trustStores, "trust-store", nil, "trust store in the format \"<store_type>:<store_name>\"") |
| 79 | + command.Flags().StringArrayVar(&opts.trustedIdentities, "trusted-identity", nil, "trusted identity, use the format \"x509.subject:<subject_of_signing_certificate>\" for x509 CA scheme and \"<signing_authority_identity>\" for x509 signingAuthority scheme") |
| 80 | + command.Flags().BoolVar(&opts.force, "force", false, "override the existing OCI trust policy configuration, never prompt (default --force=false)") |
| 81 | + command.MarkFlagRequired("name") |
| 82 | + command.MarkFlagRequired("trust-store") |
| 83 | + command.MarkFlagRequired("trusted-identity") |
| 84 | + return command |
| 85 | +} |
| 86 | + |
| 87 | +func runInit(opts *initOpts) error { |
| 88 | + ociPolicy := trustpolicy.OCIDocument{ |
| 89 | + Version: "1.0", |
| 90 | + TrustPolicies: []trustpolicy.OCITrustPolicy{ |
| 91 | + { |
| 92 | + Name: opts.name, |
| 93 | + SignatureVerification: trustpolicy.SignatureVerification{ |
| 94 | + VerificationLevel: trustpolicy.LevelStrict.Name, |
| 95 | + }, |
| 96 | + RegistryScopes: opts.registryScopes, |
| 97 | + TrustStores: opts.trustStores, |
| 98 | + TrustedIdentities: opts.trustedIdentities, |
| 99 | + }, |
| 100 | + }, |
| 101 | + } |
| 102 | + if err := ociPolicy.Validate(); err != nil { |
| 103 | + return fmt.Errorf("invalid OCI policy: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + // optional confirmation |
| 107 | + if _, err := trustpolicy.LoadOCIDocument(); err == nil { |
| 108 | + if !opts.force { |
| 109 | + confirmed, err := display.AskForConfirmation(os.Stdin, "The OCI trust policy configuration already exists, do you want to overwrite it?", opts.force) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + if !confirmed { |
| 114 | + return nil |
| 115 | + } |
| 116 | + } else { |
| 117 | + opts.printer.PrintErrorf("Warning: existing OCI trust policy configuration will be overwritten\n") |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + policyPath, err := dir.ConfigFS().SysPath(dir.PathOCITrustPolicy) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("failed to obtain path of OCI trust policy configuration: %w", err) |
| 124 | + } |
| 125 | + policyJSON, err := json.MarshalIndent(ociPolicy, "", " ") |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("failed to marshal OCI trust policy: %w", err) |
| 128 | + } |
| 129 | + if err = osutil.WriteFile(policyPath, policyJSON); err != nil { |
| 130 | + return fmt.Errorf("failed to write OCI trust policy configuration: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + return opts.printer.Printf("Successfully initialized OCI trust policy file to %s.\n", policyPath) |
| 134 | +} |
0 commit comments