Skip to content
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

Add --check-policy option to skopeo inspect #2153

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion cmd/skopeo/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type inspectOptions struct {
raw bool // Output the raw manifest instead of parsing information about the image
config bool // Output the raw config blob instead of parsing information about the image
doNotListTags bool // Do not list all tags available in the same repository
checkPolicy bool // Error out if image is not allowed to run
}

func inspectCmd(global *globalOptions) *cobra.Command {
Expand Down Expand Up @@ -61,6 +62,7 @@ skopeo inspect --format "Name: {{.Name}} Digest: {{.Digest}}" docker://registry.
flags.BoolVar(&opts.config, "config", false, "output configuration")
flags.StringVarP(&opts.format, "format", "f", "", "Format the output to a Go template")
flags.BoolVarP(&opts.doNotListTags, "no-tags", "n", false, "Do not list the available tags from the repository in the output")
flags.BoolVar(&opts.checkPolicy, "check-policy", false, "Verify that the image pass the policy check")
flags.AddFlagSet(&sharedFlags)
flags.AddFlagSet(&imageFlags)
flags.AddFlagSet(&retryFlags)
Expand Down Expand Up @@ -106,6 +108,24 @@ func (opts *inspectOptions) run(args []string, stdout io.Writer) (retErr error)
}
}()

unparsedImage := image.UnparsedInstance(src, nil)
if opts.checkPolicy {
policyContext, err := opts.global.getPolicyContext()
if err != nil {
return fmt.Errorf("Error loading trust policy: %v", err)
}
defer func() {
if err := policyContext.Destroy(); err != nil {
retErr = noteCloseFailure(retErr, "tearing down policy context", err)
}
}()

// Be paranoid and fail if either return value indicates so.
if allowed, err := policyContext.IsRunningImageAllowed(ctx, unparsedImage); !allowed || err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be inside retry.IfNecessary because it contacts remote servers to fetch the signatures.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, being security-critical, this should have at least minimal tests that the option works and policy is enforced (not that all possible kinds of policy are enforced), maybe somewhere around copySuite.TestCopySignatures which already creates correctly/incorrectly signed images.

return fmt.Errorf("Image rejected: %w", err)
}
}

if err := retry.IfNecessary(ctx, func() error {
rawManifest, _, err = src.GetManifest(ctx, nil)
return err
Expand All @@ -122,7 +142,7 @@ func (opts *inspectOptions) run(args []string, stdout io.Writer) (retErr error)
return nil
}

img, err := image.FromUnparsedImage(ctx, sys, image.UnparsedInstance(src, nil))
img, err := image.FromUnparsedImage(ctx, sys, unparsedImage)
if err != nil {
return fmt.Errorf("Error parsing manifest for image: %w", err)
}
Expand Down
4 changes: 4 additions & 0 deletions docs/skopeo-inspect.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ If the authorization state is not found there, $HOME/.docker/config.json is chec

Use certificates at _path_ (\*.crt, \*.cert, \*.key) to connect to the registry.

**--check-policy**

Verify that the image pass the policy check.

**--config**

Output configuration in OCI format, default is to format in JSON format.
Expand Down