Skip to content

Commit cce0bc4

Browse files
authored
chore(policies): validate remote policies on contract creation (#1240)
Signed-off-by: Jose I. Paris <[email protected]>
1 parent 15a2c46 commit cce0bc4

File tree

5 files changed

+123
-14
lines changed

5 files changed

+123
-14
lines changed

app/controlplane/internal/service/workflowcontract.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020

2121
pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
22+
"github.com/chainloop-dev/chainloop/app/controlplane/internal/usercontext"
2223
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz"
2324
errors "github.com/go-kratos/kratos/v2/errors"
2425
"google.golang.org/protobuf/types/known/timestamppb"
@@ -91,6 +92,15 @@ func (s *WorkflowContractService) Create(ctx context.Context, req *pb.WorkflowCo
9192
return nil, err
9293
}
9394

95+
token, err := usercontext.GetRawToken(ctx)
96+
if err != nil {
97+
return nil, err
98+
}
99+
100+
if err = s.contractUseCase.ValidateContractPolicies(req.RawContract, token); err != nil {
101+
return nil, handleUseCaseErr(err, s.log)
102+
}
103+
94104
// Currently supporting only v1 version
95105
schema, err := s.contractUseCase.Create(ctx, &biz.WorkflowContractCreateOpts{
96106
OrgID: currentOrg.ID,
@@ -109,6 +119,15 @@ func (s *WorkflowContractService) Update(ctx context.Context, req *pb.WorkflowCo
109119
return nil, err
110120
}
111121

122+
token, err := usercontext.GetRawToken(ctx)
123+
if err != nil {
124+
return nil, err
125+
}
126+
127+
if err = s.contractUseCase.ValidateContractPolicies(req.RawContract, token); err != nil {
128+
return nil, handleUseCaseErr(err, s.log)
129+
}
130+
112131
schemaWithVersion, err := s.contractUseCase.Update(ctx, currentOrg.ID, req.Name,
113132
&biz.WorkflowContractUpdateOpts{
114133
Description: req.Description,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// Copyright 2024 The Chainloop 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 usercontext
17+
18+
import (
19+
"context"
20+
"strings"
21+
22+
"github.com/go-kratos/kratos/v2/middleware/auth/jwt"
23+
"github.com/go-kratos/kratos/v2/transport"
24+
)
25+
26+
// GetRawToken takes whatever Bearer token is in the request
27+
func GetRawToken(ctx context.Context) (string, error) {
28+
header, ok := transport.FromServerContext(ctx)
29+
if !ok {
30+
return "", jwt.ErrMissingJwtToken
31+
}
32+
33+
auths := strings.SplitN(header.RequestHeader().Get("Authorization"), " ", 2)
34+
if len(auths) != 2 || !strings.EqualFold(auths[0], "Bearer") {
35+
return "", jwt.ErrMissingJwtToken
36+
}
37+
return auths[1], nil
38+
}

app/controlplane/pkg/biz/workflowcontract.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/bufbuild/protoyaml-go"
2828
schemav1 "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1"
2929
"github.com/chainloop-dev/chainloop/app/controlplane/internal/policies"
30+
loader "github.com/chainloop-dev/chainloop/pkg/policies"
3031
"github.com/go-kratos/kratos/v2/log"
3132
"github.com/google/uuid"
3233
"google.golang.org/protobuf/encoding/protojson"
@@ -299,6 +300,47 @@ func (uc *WorkflowContractUseCase) Update(ctx context.Context, orgID, name strin
299300
return c, nil
300301
}
301302

303+
func (uc *WorkflowContractUseCase) ValidateContractPolicies(rawSchema []byte, token string) error {
304+
// Validate that externally provided policies exist
305+
c, err := identifyUnMarshalAndValidateRawContract(rawSchema)
306+
if err != nil {
307+
return NewErrValidation(err)
308+
}
309+
for _, att := range c.Schema.GetPolicies().GetAttestation() {
310+
_, err := uc.findPolicy(att, token)
311+
if err != nil {
312+
return NewErrValidation(err)
313+
}
314+
}
315+
for _, att := range c.Schema.GetPolicies().GetMaterials() {
316+
_, err := uc.findPolicy(att, token)
317+
if err != nil {
318+
return NewErrValidation(err)
319+
}
320+
}
321+
return nil
322+
}
323+
324+
func (uc *WorkflowContractUseCase) findPolicy(att *schemav1.PolicyAttachment, token string) (*schemav1.Policy, error) {
325+
if att.GetEmbedded() != nil {
326+
return att.GetEmbedded(), nil
327+
}
328+
329+
// if it should come from a provider, check that it's available
330+
// chainloop://[provider/]name
331+
if loader.IsProviderScheme(att.GetRef()) {
332+
provider, name := loader.ProviderParts(att.GetRef())
333+
policy, err := uc.GetPolicy(provider, name, token)
334+
if err != nil {
335+
return nil, fmt.Errorf("failed to get policy '%s': %w", name, err)
336+
}
337+
return policy, nil
338+
}
339+
340+
// Otherwise, don't return an error, as it might consist of a local policy, not available in this context
341+
return nil, nil
342+
}
343+
302344
// Delete soft-deletes the entry
303345
func (uc *WorkflowContractUseCase) Delete(ctx context.Context, orgID, contractID string) error {
304346
orgUUID, err := uuid.Parse(orgID)

pkg/policies/loader.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (l *BlobLoader) Load(_ context.Context, attachment *v1.PolicyAttachment) (*
6666
return &spec, nil
6767
}
6868

69-
const chainloopScheme = "chainloop"
69+
const ChainloopScheme = "chainloop"
7070

7171
// ChainloopLoader loads policies referenced with chainloop://provider/name URLs
7272
type ChainloopLoader struct {
@@ -91,20 +91,11 @@ func (c *ChainloopLoader) Load(ctx context.Context, attachment *v1.PolicyAttachm
9191
return remotePolicyCache[ref], nil
9292
}
9393

94-
parts := strings.SplitN(ref, "://", 2)
95-
if len(parts) != 2 || parts[0] != chainloopScheme {
94+
if !IsProviderScheme(ref) {
9695
return nil, fmt.Errorf("invalid policy reference %q", ref)
9796
}
9897

99-
pn := strings.SplitN(parts[1], "/", 2)
100-
var (
101-
name = pn[0]
102-
provider string
103-
)
104-
if len(pn) == 2 {
105-
provider = pn[0]
106-
name = pn[1]
107-
}
98+
name, provider := ProviderParts(ref)
10899

109100
resp, err := c.Client.GetPolicy(ctx, &pb.AttestationServiceGetPolicyRequest{
110101
Provider: provider,
@@ -119,3 +110,23 @@ func (c *ChainloopLoader) Load(ctx context.Context, attachment *v1.PolicyAttachm
119110

120111
return resp.GetPolicy(), nil
121112
}
113+
114+
// IsProviderScheme takes a policy reference and returns whether it's referencing to an external provider or not
115+
func IsProviderScheme(ref string) bool {
116+
parts := strings.SplitN(ref, "://", 2)
117+
return len(parts) == 2 && parts[0] == ChainloopScheme
118+
}
119+
120+
func ProviderParts(ref string) (string, string) {
121+
parts := strings.SplitN(ref, "://", 2)
122+
pn := strings.SplitN(parts[1], "/", 2)
123+
var (
124+
name = pn[0]
125+
provider string
126+
)
127+
if len(pn) == 2 {
128+
provider = pn[0]
129+
name = pn[1]
130+
}
131+
return provider, name
132+
}

pkg/policies/policies.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ func (pv *PolicyVerifier) getLoader(attachment *v1.PolicyAttachment) (Loader, er
198198
if emb != nil {
199199
loader = new(EmbeddedLoader)
200200
} else {
201-
parts := strings.SplitN(ref, "://", 2)
202-
if len(parts) == 2 && parts[0] == chainloopScheme {
201+
if IsProviderScheme(ref) {
203202
loader = NewChainloopLoader(pv.client)
204203
} else {
205204
loader = new(BlobLoader)

0 commit comments

Comments
 (0)