fix(staterpc): an org-scoped caller cannot read or write another org's harness secret
Issue: archie-core-jo15w
This commit is contained in:
1 parent
cbb6f1db21
commit
046da82c25
2 files changed
+55
-1
No files matched your search
@@ -0,0 +1,37 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
)
|
||||
|
||||
func TestConfineHarnessOrg(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
scope org.OrgID
|
||||
target string
|
||||
denied bool
|
||||
}{
|
||||
{"internal service names any org", "", "acme", false},
|
||||
{"org caller reads its own org", "acme", "acme", false},
|
||||
{"org caller cannot name another org", "acme", "other", true},
|
||||
{"system org operates the instance", org.DefaultOrgID, "other", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
if tt.scope != "" {
|
||||
ctx = org.WithScope(ctx, tt.scope)
|
||||
}
|
||||
err := confineHarnessOrg(ctx, tt.target)
|
||||
if denied := status.Code(err) == codes.PermissionDenied; denied != tt.denied {
|
||||
t.Fatalf("denied = %v, want %v (err %v)", denied, tt.denied, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -376,10 +376,23 @@ func (s *server) ListSteps(ctx context.Context, r *pb.ListStepsRequest) (*pb.Lis
|
||||
return &pb.ListStepsResponse{Steps: mapValues(steps, stepExecutionProto)}, nil
|
||||
}
|
||||
|
||||
// confineHarnessOrg refuses a harness secret request that names an org other
|
||||
// than the one the caller is confined to. Internal services act unscoped and
|
||||
// name the task's org; the system org operates the whole instance.
|
||||
func confineHarnessOrg(ctx context.Context, target string) error {
|
||||
if scope, ok := org.Scope(ctx); ok && !org.InstanceScope(ctx) && string(scope) != target {
|
||||
return status.Error(codes.PermissionDenied, "harness secret belongs to another org")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *server) GetHarnessSecret(ctx context.Context, r *pb.GetHarnessSecretRequest) (*pb.GetHarnessSecretResponse, error) {
|
||||
if s.deps.HarnessSecrets == nil {
|
||||
return nil, status.Error(codes.Unavailable, "harness secret store unavailable")
|
||||
}
|
||||
if err := confineHarnessOrg(ctx, r.Org); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
secret, err := s.deps.HarnessSecrets.GetHarnessSecret(ctx, r.Org, r.Service)
|
||||
if err != nil {
|
||||
return nil, s.logErr("GetHarnessSecret", err)
|
||||
@@ -391,7 +404,11 @@ func (s *server) PutHarnessSecret(ctx context.Context, r *pb.PutHarnessSecretReq
|
||||
if s.deps.HarnessSecrets == nil {
|
||||
return nil, status.Error(codes.Unavailable, "harness secret store unavailable")
|
||||
}
|
||||
if err := s.deps.HarnessSecrets.PutHarnessSecret(ctx, harnessSecretValue(r.Secret)); err != nil {
|
||||
value := harnessSecretValue(r.Secret)
|
||||
if err := confineHarnessOrg(ctx, value.Org); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.deps.HarnessSecrets.PutHarnessSecret(ctx, value); err != nil {
|
||||
return nil, s.logErr("PutHarnessSecret", err)
|
||||
}
|
||||
return &pb.PutHarnessSecretResponse{}, nil
|
||||
|
||||
Reference in new issue
Block a user