Skip to content

Commit

Permalink
Add an optional Message to Static actions for custom fail message.
Browse files Browse the repository at this point in the history
Signed-off-by: Ville Aikas <vaikas@chainguard.dev>
  • Loading branch information
vaikas committed Mar 8, 2023
1 parent 9c96f55 commit 4f2d853
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 5 deletions.
6 changes: 6 additions & 0 deletions config/300-clusterimagepolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ spec:
action:
description: Action defines how to handle a matching policy.
type: string
message:
description: For fail actions, emit an optional custom message
type: string
images:
description: Images defines the patterns of image names that should be subject to this policy.
type: array
Expand Down Expand Up @@ -570,6 +573,9 @@ spec:
action:
description: Action defines how to handle a matching policy.
type: string
message:
description: For fail actions, emit an optional custom message
type: string
images:
description: Images defines the patterns of image names that should be subject to this policy.
type: array
Expand Down
1 change: 1 addition & 0 deletions docs/api-types/index-v1alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ StaticRef specifies that signatures / attestations are not validated but instead
| Field | Description | Scheme | Required |
| ----- | ----------- | ------ | -------- |
| action | Action defines how to handle a matching policy. | string | true |
| message | For fail actions, emit an optional custom message. This only makes sense for 'fail' action because on 'pass' there's no place to jot down the message. | string | false |

[Back to TOC](#table-of-contents)

Expand Down
1 change: 1 addition & 0 deletions docs/api-types/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ StaticRef specifies that signatures / attestations are not validated but instead
| Field | Description | Scheme | Required |
| ----- | ----------- | ------ | -------- |
| action | Action defines how to handle a matching policy. | string | true |
| message | For fail actions, emit an optional custom message | string | false |

[Back to TOC](#table-of-contents)

Expand Down
8 changes: 6 additions & 2 deletions pkg/apis/policy/v1alpha1/clusterimagepolicy_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ func (authority *Authority) ConvertTo(ctx context.Context, sink *v1beta1.Authori
}
if authority.Static != nil {
sink.Static = &v1beta1.StaticRef{
Action: authority.Static.Action,
Action: authority.Static.Action,
Message: authority.Static.Message,
}
}
return nil
Expand Down Expand Up @@ -293,7 +294,10 @@ func (authority *Authority) ConvertFrom(ctx context.Context, source *v1beta1.Aut
}
}
if source.Static != nil {
authority.Static = &StaticRef{Action: source.Static.Action}
authority.Static = &StaticRef{
Action: source.Static.Action,
Message: source.Static.Message,
}
}
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/policy/v1alpha1/clusterimagepolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ type KeyRef struct {
type StaticRef struct {
// Action defines how to handle a matching policy.
Action string `json:"action"`
// For fail actions, emit an optional custom message. This only makes
// sense for 'fail' action because on 'pass' there's no place to jot down
// the message.
Message string `json:"message,omitempty"`
}

// Source specifies the location of the signature / attestations.
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/policy/v1beta1/clusterimagepolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ type KeyRef struct {
type StaticRef struct {
// Action defines how to handle a matching policy.
Action string `json:"action"`
// For fail actions, emit an optional custom message
Message string `json:"message,omitempty"`
}

// Source specifies the location of the signature / attestations.
Expand Down
6 changes: 4 additions & 2 deletions pkg/webhook/clusterimagepolicy/clusterimagepolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ type KeylessRef struct {
}

type StaticRef struct {
Action string `json:"action"`
Action string `json:"action"`
Message string `json:"message,omitempty"`
}

type AttestationPolicy struct {
Expand Down Expand Up @@ -406,6 +407,7 @@ func convertStaticRefV1Alpha1ToWebhook(in *v1alpha1.StaticRef) *StaticRef {
}

return &StaticRef{
Action: in.Action,
Action: in.Action,
Message: in.Message,
}
}
2 changes: 1 addition & 1 deletion pkg/webhook/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func ValidatePolicy(ctx context.Context, namespace string, ref name.Reference, c
switch {
case authority.Static != nil:
if authority.Static.Action == "fail" {
result.err = cosign.NewVerificationError("disallowed by static policy")
result.err = cosign.NewVerificationError("disallowed by static policy: " + authority.Static.Message)
results <- result
return
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/webhook/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,18 @@ func TestValidatePolicy(t *testing.T) {
}},
},
wantErrs: []string{"disallowed by static policy"},
}, {
name: "simple, static set to fail with custom message",
policy: webhookcip.ClusterImagePolicy{
Authorities: []webhookcip.Authority{{
Name: "authority-0",
Static: &webhookcip.StaticRef{
Action: "fail",
Message: "test custom message here",
},
}},
},
wantErrs: []string{"disallowed by static policy: test custom message here"},
}, {
name: "simple, public key, no error",
policy: webhookcip.ClusterImagePolicy{
Expand Down

0 comments on commit 4f2d853

Please sign in to comment.