fix(staterpc): derive harness secret org from caller grants
This commit is contained in:
15 files changed
+437
-92
No files matched your search
@@ -19,6 +19,7 @@ import (
|
|||||||
"github.com/samcharles93/archie-core/internal/container"
|
"github.com/samcharles93/archie-core/internal/container"
|
||||||
daemonpb "github.com/samcharles93/archie-core/internal/contracts/daemon/v1"
|
daemonpb "github.com/samcharles93/archie-core/internal/contracts/daemon/v1"
|
||||||
pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1"
|
pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1"
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||||
"github.com/samcharles93/archie-core/internal/domain/presence"
|
"github.com/samcharles93/archie-core/internal/domain/presence"
|
||||||
"github.com/samcharles93/archie-core/internal/infrastructure/harnesssession"
|
"github.com/samcharles93/archie-core/internal/infrastructure/harnesssession"
|
||||||
"github.com/samcharles93/archie-core/internal/infrastructure/kitrun"
|
"github.com/samcharles93/archie-core/internal/infrastructure/kitrun"
|
||||||
@@ -87,6 +88,9 @@ func (h *harnessServer) Open(stream pb.HarnessService_OpenServer) error {
|
|||||||
if target == nil || target.GetOrg() == "" {
|
if target == nil || target.GetOrg() == "" {
|
||||||
return status.Error(codes.InvalidArgument, "the first frame must name a session target with an org")
|
return status.Error(codes.InvalidArgument, "the first frame must name a session target with an org")
|
||||||
}
|
}
|
||||||
|
if target.GetOrg() != string(org.OrgFromContext(stream.Context())) {
|
||||||
|
return status.Error(codes.PermissionDenied, "harness session belongs to another org")
|
||||||
|
}
|
||||||
switch t := target.GetTarget().(type) {
|
switch t := target.GetTarget().(type) {
|
||||||
case *pb.OpenTarget_Setup:
|
case *pb.OpenTarget_Setup:
|
||||||
if t.Setup.GetProfile() == "" {
|
if t.Setup.GetProfile() == "" {
|
||||||
@@ -204,7 +208,7 @@ func (b *boot) setupDaemonRPC(ctx context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
callers := rpcidentity.Callers{Principals: b.d.Principals}
|
callers := rpcidentity.Callers{Principals: b.d.Principals}
|
||||||
srv := grpc.NewServer(append(servicekit.ServerLimits(), creds, grpc.ChainUnaryInterceptor(callers.Unary()))...)
|
srv := grpc.NewServer(append(servicekit.ServerLimits(), creds, grpc.ChainUnaryInterceptor(callers.Unary()), grpc.ChainStreamInterceptor(callers.Stream()))...)
|
||||||
daemonpb.RegisterDaemonServiceServer(srv, &repositoryServer{boot: b})
|
daemonpb.RegisterDaemonServiceServer(srv, &repositoryServer{boot: b})
|
||||||
// Terminal sessions still require certificates and a container launcher.
|
// Terminal sessions still require certificates and a container launcher.
|
||||||
if id != nil && b.kitLauncher != nil && b.containerPool != nil {
|
if id != nil && b.kitLauncher != nil && b.containerPool != nil {
|
||||||
|
|||||||
@@ -63,3 +63,33 @@ func TestRunViewOpen(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type harnessOpenStream struct {
|
||||||
|
pb.HarnessService_OpenServer
|
||||||
|
ctx context.Context //nolint:containedctx // gRPC streams expose their context through Context().
|
||||||
|
target *pb.OpenTarget
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s harnessOpenStream) Context() context.Context { return s.ctx }
|
||||||
|
func (s harnessOpenStream) Recv() (*pb.OpenRequest, error) {
|
||||||
|
return &pb.OpenRequest{Frame: &pb.OpenRequest_Target{Target: s.target}}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHarnessSessionCallerOrg(t *testing.T) {
|
||||||
|
for _, tt := range []struct {
|
||||||
|
name string
|
||||||
|
caller org.OrgID
|
||||||
|
target string
|
||||||
|
}{
|
||||||
|
{"tenant cannot open another tenant", "acme", "victim"},
|
||||||
|
{"system caller cannot name another tenant", org.DefaultOrgID, "victim"},
|
||||||
|
{"unattributed caller cannot name a tenant", "", "victim"},
|
||||||
|
} {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
stream := harnessOpenStream{ctx: org.WithOrg(t.Context(), tt.caller), target: &pb.OpenTarget{Org: tt.target, Target: &pb.OpenTarget_Setup{Setup: &pb.SetupTarget{Profile: "kit"}}}}
|
||||||
|
if err := (&harnessServer{}).Open(stream); status.Code(err) != codes.PermissionDenied {
|
||||||
|
t.Fatalf("error %v, want PermissionDenied", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5008,7 +5008,6 @@ func (x *ListStepsResponse) GetSteps() []*StepExecution {
|
|||||||
// (docs/prds/external-agent-harness.md, Credentials).
|
// (docs/prds/external-agent-harness.md, Credentials).
|
||||||
type HarnessSecret struct {
|
type HarnessSecret struct {
|
||||||
state protoimpl.MessageState `protogen:"open.v1"`
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
Org string `protobuf:"bytes,1,opt,name=org,proto3" json:"org,omitempty"`
|
|
||||||
Service string `protobuf:"bytes,2,opt,name=service,proto3" json:"service,omitempty"`
|
Service string `protobuf:"bytes,2,opt,name=service,proto3" json:"service,omitempty"`
|
||||||
AccessToken string `protobuf:"bytes,3,opt,name=access_token,json=accessToken,proto3" json:"access_token,omitempty"`
|
AccessToken string `protobuf:"bytes,3,opt,name=access_token,json=accessToken,proto3" json:"access_token,omitempty"`
|
||||||
RefreshToken string `protobuf:"bytes,4,opt,name=refresh_token,json=refreshToken,proto3" json:"refresh_token,omitempty"`
|
RefreshToken string `protobuf:"bytes,4,opt,name=refresh_token,json=refreshToken,proto3" json:"refresh_token,omitempty"`
|
||||||
@@ -5050,13 +5049,6 @@ func (*HarnessSecret) Descriptor() ([]byte, []int) {
|
|||||||
return file_state_v1_state_proto_rawDescGZIP(), []int{74}
|
return file_state_v1_state_proto_rawDescGZIP(), []int{74}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *HarnessSecret) GetOrg() string {
|
|
||||||
if x != nil {
|
|
||||||
return x.Org
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *HarnessSecret) GetService() string {
|
func (x *HarnessSecret) GetService() string {
|
||||||
if x != nil {
|
if x != nil {
|
||||||
return x.Service
|
return x.Service
|
||||||
@@ -5108,8 +5100,8 @@ func (x *HarnessSecret) GetScopes() []string {
|
|||||||
|
|
||||||
type GetHarnessSecretRequest struct {
|
type GetHarnessSecretRequest struct {
|
||||||
state protoimpl.MessageState `protogen:"open.v1"`
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
Org string `protobuf:"bytes,1,opt,name=org,proto3" json:"org,omitempty"`
|
|
||||||
Service string `protobuf:"bytes,2,opt,name=service,proto3" json:"service,omitempty"`
|
Service string `protobuf:"bytes,2,opt,name=service,proto3" json:"service,omitempty"`
|
||||||
|
RunCredential string `protobuf:"bytes,3,opt,name=run_credential,json=runCredential,proto3" json:"run_credential,omitempty"`
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
}
|
}
|
||||||
@@ -5144,13 +5136,6 @@ func (*GetHarnessSecretRequest) Descriptor() ([]byte, []int) {
|
|||||||
return file_state_v1_state_proto_rawDescGZIP(), []int{75}
|
return file_state_v1_state_proto_rawDescGZIP(), []int{75}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *GetHarnessSecretRequest) GetOrg() string {
|
|
||||||
if x != nil {
|
|
||||||
return x.Org
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *GetHarnessSecretRequest) GetService() string {
|
func (x *GetHarnessSecretRequest) GetService() string {
|
||||||
if x != nil {
|
if x != nil {
|
||||||
return x.Service
|
return x.Service
|
||||||
@@ -5158,9 +5143,17 @@ func (x *GetHarnessSecretRequest) GetService() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (x *GetHarnessSecretRequest) GetRunCredential() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.RunCredential
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
type GetHarnessSecretResponse struct {
|
type GetHarnessSecretResponse struct {
|
||||||
state protoimpl.MessageState `protogen:"open.v1"`
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
Secret *HarnessSecret `protobuf:"bytes,1,opt,name=secret,proto3" json:"secret,omitempty"`
|
Secret *HarnessSecret `protobuf:"bytes,1,opt,name=secret,proto3" json:"secret,omitempty"`
|
||||||
|
Org string `protobuf:"bytes,2,opt,name=org,proto3" json:"org,omitempty"`
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
}
|
}
|
||||||
@@ -5202,9 +5195,17 @@ func (x *GetHarnessSecretResponse) GetSecret() *HarnessSecret {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (x *GetHarnessSecretResponse) GetOrg() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.Org
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
type PutHarnessSecretRequest struct {
|
type PutHarnessSecretRequest struct {
|
||||||
state protoimpl.MessageState `protogen:"open.v1"`
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
Secret *HarnessSecret `protobuf:"bytes,1,opt,name=secret,proto3" json:"secret,omitempty"`
|
Secret *HarnessSecret `protobuf:"bytes,1,opt,name=secret,proto3" json:"secret,omitempty"`
|
||||||
|
RunCredential string `protobuf:"bytes,2,opt,name=run_credential,json=runCredential,proto3" json:"run_credential,omitempty"`
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
}
|
}
|
||||||
@@ -5246,6 +5247,13 @@ func (x *PutHarnessSecretRequest) GetSecret() *HarnessSecret {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (x *PutHarnessSecretRequest) GetRunCredential() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.RunCredential
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
type PutHarnessSecretResponse struct {
|
type PutHarnessSecretResponse struct {
|
||||||
state protoimpl.MessageState `protogen:"open.v1"`
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
@@ -19809,9 +19817,8 @@ const file_state_v1_state_proto_rawDesc = "" +
|
|||||||
"\fexecution_id\x18\x01 \x01(\x03R\vexecutionId\x12\x18\n" +
|
"\fexecution_id\x18\x01 \x01(\x03R\vexecutionId\x12\x18\n" +
|
||||||
"\aattempt\x18\x02 \x01(\x03R\aattempt\"B\n" +
|
"\aattempt\x18\x02 \x01(\x03R\aattempt\"B\n" +
|
||||||
"\x11ListStepsResponse\x12-\n" +
|
"\x11ListStepsResponse\x12-\n" +
|
||||||
"\x05steps\x18\x01 \x03(\v2\x17.state.v1.StepExecutionR\x05steps\"\xb0\x02\n" +
|
"\x05steps\x18\x01 \x03(\v2\x17.state.v1.StepExecutionR\x05steps\"\xa9\x02\n" +
|
||||||
"\rHarnessSecret\x12\x10\n" +
|
"\rHarnessSecret\x12\x18\n" +
|
||||||
"\x03org\x18\x01 \x01(\tR\x03org\x12\x18\n" +
|
|
||||||
"\aservice\x18\x02 \x01(\tR\aservice\x12!\n" +
|
"\aservice\x18\x02 \x01(\tR\aservice\x12!\n" +
|
||||||
"\faccess_token\x18\x03 \x01(\tR\vaccessToken\x12#\n" +
|
"\faccess_token\x18\x03 \x01(\tR\vaccessToken\x12#\n" +
|
||||||
"\rrefresh_token\x18\x04 \x01(\tR\frefreshToken\x12\x1d\n" +
|
"\rrefresh_token\x18\x04 \x01(\tR\frefreshToken\x12\x1d\n" +
|
||||||
@@ -19821,14 +19828,16 @@ const file_state_v1_state_proto_rawDesc = "" +
|
|||||||
"expires_at\x18\x06 \x01(\v2\x1a.google.protobuf.TimestampR\texpiresAt\x129\n" +
|
"expires_at\x18\x06 \x01(\v2\x1a.google.protobuf.TimestampR\texpiresAt\x129\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
"updated_at\x18\a \x01(\v2\x1a.google.protobuf.TimestampR\tupdatedAt\x12\x16\n" +
|
"updated_at\x18\a \x01(\v2\x1a.google.protobuf.TimestampR\tupdatedAt\x12\x16\n" +
|
||||||
"\x06scopes\x18\b \x03(\tR\x06scopes\"E\n" +
|
"\x06scopes\x18\b \x03(\tR\x06scopesJ\x04\b\x01\x10\x02R\x03org\"e\n" +
|
||||||
"\x17GetHarnessSecretRequest\x12\x10\n" +
|
"\x17GetHarnessSecretRequest\x12\x18\n" +
|
||||||
"\x03org\x18\x01 \x01(\tR\x03org\x12\x18\n" +
|
"\aservice\x18\x02 \x01(\tR\aservice\x12%\n" +
|
||||||
"\aservice\x18\x02 \x01(\tR\aservice\"K\n" +
|
"\x0erun_credential\x18\x03 \x01(\tR\rrunCredentialJ\x04\b\x01\x10\x02R\x03org\"]\n" +
|
||||||
"\x18GetHarnessSecretResponse\x12/\n" +
|
"\x18GetHarnessSecretResponse\x12/\n" +
|
||||||
"\x06secret\x18\x01 \x01(\v2\x17.state.v1.HarnessSecretR\x06secret\"J\n" +
|
"\x06secret\x18\x01 \x01(\v2\x17.state.v1.HarnessSecretR\x06secret\x12\x10\n" +
|
||||||
|
"\x03org\x18\x02 \x01(\tR\x03org\"q\n" +
|
||||||
"\x17PutHarnessSecretRequest\x12/\n" +
|
"\x17PutHarnessSecretRequest\x12/\n" +
|
||||||
"\x06secret\x18\x01 \x01(\v2\x17.state.v1.HarnessSecretR\x06secret\"\x1a\n" +
|
"\x06secret\x18\x01 \x01(\v2\x17.state.v1.HarnessSecretR\x06secret\x12%\n" +
|
||||||
|
"\x0erun_credential\x18\x02 \x01(\tR\rrunCredential\"\x1a\n" +
|
||||||
"\x18PutHarnessSecretResponse\"3\n" +
|
"\x18PutHarnessSecretResponse\"3\n" +
|
||||||
"\rUpdateRequest\x12\"\n" +
|
"\rUpdateRequest\x12\"\n" +
|
||||||
"\x04task\x18\x01 \x01(\v2\x0e.state.v1.TaskR\x04task\"\x10\n" +
|
"\x04task\x18\x01 \x01(\v2\x0e.state.v1.TaskR\x04task\"\x10\n" +
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package harnesssecret
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
type runCredentialKey struct{}
|
||||||
|
|
||||||
|
// WithRunCredential binds a proxy or launcher secret operation to its task grant.
|
||||||
|
func WithRunCredential(ctx context.Context, credential string) context.Context {
|
||||||
|
return context.WithValue(ctx, runCredentialKey{}, credential)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunCredential returns the task grant authorizing a secret operation.
|
||||||
|
func RunCredential(ctx context.Context) string {
|
||||||
|
credential, _ := ctx.Value(runCredentialKey{}).(string)
|
||||||
|
return credential
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
|
|
||||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||||
|
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||||
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -127,6 +128,7 @@ func (p *Proxy) granted(ctx context.Context, s *Session, rule oauthRule) (bool,
|
|||||||
// request to one of a granted rule's resource hosts. A request not carrying
|
// request to one of a granted rule's resource hosts. A request not carrying
|
||||||
// the sentinel never touches the store.
|
// the sentinel never touches the store.
|
||||||
func (p *Proxy) injectOAuth(ctx context.Context, s *Session, r *http.Request, host string, port int) error {
|
func (p *Proxy) injectOAuth(ctx context.Context, s *Session, r *http.Request, host string, port int) error {
|
||||||
|
ctx = s.oauthContext(ctx)
|
||||||
phase := s.phase()
|
phase := s.phase()
|
||||||
for _, rule := range s.oauth {
|
for _, rule := range s.oauth {
|
||||||
if !slices.Contains(rule.phases, phase) || rule.sentinels.AccessToken == "" ||
|
if !slices.Contains(rule.phases, phase) || rule.sentinels.AccessToken == "" ||
|
||||||
@@ -157,6 +159,7 @@ func (p *Proxy) injectOAuth(ctx context.Context, s *Session, r *http.Request, ho
|
|||||||
// refresh token for the stored one. Successful responses are stored and
|
// refresh token for the stored one. Successful responses are stored and
|
||||||
// returned to the container with sentinels.
|
// returned to the container with sentinels.
|
||||||
func (p *Proxy) interceptOAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, s *Session, rule oauthRule, scheme, target string) {
|
func (p *Proxy) interceptOAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, s *Session, rule oauthRule, scheme, target string) {
|
||||||
|
ctx = s.oauthContext(ctx)
|
||||||
body, err := io.ReadAll(io.LimitReader(r.Body, maxTokenExchange))
|
body, err := io.ReadAll(io.LimitReader(r.Body, maxTokenExchange))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "read token request: "+err.Error(), http.StatusBadGateway)
|
http.Error(w, "read token request: "+err.Error(), http.StatusBadGateway)
|
||||||
@@ -304,3 +307,10 @@ func headerCarries(h http.Header, value string) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Session) oauthContext(ctx context.Context) context.Context {
|
||||||
|
if s.principal.IdentityID != "" {
|
||||||
|
ctx = access.WithPrincipal(ctx, s.principal)
|
||||||
|
}
|
||||||
|
return harnesssecret.WithRunCredential(ctx, s.runCredential)
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
package egress
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||||
|
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
||||||
|
)
|
||||||
|
|
||||||
|
type identityOAuthStore struct {
|
||||||
|
t *testing.T
|
||||||
|
token string
|
||||||
|
principal access.Principal
|
||||||
|
reads, writes int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *identityOAuthStore) check(ctx context.Context) {
|
||||||
|
s.t.Helper()
|
||||||
|
p, _ := access.PrincipalFromContext(ctx)
|
||||||
|
if harnesssecret.RunCredential(ctx) != s.token || p.IdentityID != s.principal.IdentityID {
|
||||||
|
s.t.Fatal("OAuth operation lost the session's grant or principal")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *identityOAuthStore) GetHarnessSecret(ctx context.Context, _, _ string) (harnesssecret.Secret, error) {
|
||||||
|
s.check(ctx)
|
||||||
|
s.reads++
|
||||||
|
return harnesssecret.Secret{AccessToken: "real-access", RefreshToken: "real-refresh"}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *identityOAuthStore) PutHarnessSecret(ctx context.Context, _ harnesssecret.Secret) error {
|
||||||
|
s.check(ctx)
|
||||||
|
s.writes++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOAuthUsesSessionAuthority(t *testing.T) {
|
||||||
|
for _, setup := range []bool{false, true} {
|
||||||
|
t.Run(map[bool]string{false: "task grant", true: "setup principal"}[setup], func(t *testing.T) {
|
||||||
|
opts := SessionOptions{Token: "proxy-token", RunCredential: "run-grant", Org: "acme", Credentials: []spec.CredentialCapability{{Service: "provider", Phase: spec.Phases{"runtime"}, OAuth: &spec.OAuth{TokenEndpoint: &spec.TokenEndpoint{Host: "token.example"}, ResourceHosts: []string{"api.example"}, Sentinels: &spec.Sentinels{AccessToken: "access-sentinel", RefreshToken: "refresh-sentinel"}}}}}
|
||||||
|
if setup {
|
||||||
|
opts.RunCredential = ""
|
||||||
|
opts.Principal = access.Principal{IdentityID: "alice", Org: "acme"}
|
||||||
|
}
|
||||||
|
store := &identityOAuthStore{t: t, token: opts.RunCredential, principal: opts.Principal}
|
||||||
|
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if string(body) != "real-refresh" {
|
||||||
|
t.Errorf("refresh body %q", body)
|
||||||
|
}
|
||||||
|
_, _ = io.WriteString(w, `{"access_token":"new-access","refresh_token":"new-refresh"}`)
|
||||||
|
}))
|
||||||
|
defer upstream.Close()
|
||||||
|
proxy := NewProxy(nil, ProxyOptions{OAuthStore: store, Resolver: ResolverFunc(func(context.Context, string, string) (string, error) { return "", nil }), Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||||
|
return (&net.Dialer{}).DialContext(ctx, "tcp", upstream.Listener.Addr().String())
|
||||||
|
}})
|
||||||
|
session, err := proxy.Register(opts)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
session.EnterRuntime()
|
||||||
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "http://api.example/data", nil)
|
||||||
|
req.Header.Set("Authorization", "Bearer access-sentinel")
|
||||||
|
if err := proxy.injectOAuth(req.Context(), session, req, "api.example", 80); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if req.Header.Get("Authorization") != "Bearer real-access" {
|
||||||
|
t.Fatal("access token not injected")
|
||||||
|
}
|
||||||
|
refresh := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "http://token.example/token", strings.NewReader("refresh-sentinel"))
|
||||||
|
response := httptest.NewRecorder()
|
||||||
|
proxy.interceptOAuth(refresh.Context(), response, refresh, session, session.oauth[0], "http", "token.example")
|
||||||
|
if response.Code != http.StatusOK || store.reads != 2 || store.writes != 1 {
|
||||||
|
t.Fatalf("status %d, reads %d, writes %d", response.Code, store.reads, store.writes)
|
||||||
|
}
|
||||||
|
if strings.Contains(response.Body.String(), "new-access") {
|
||||||
|
t.Fatal("real token escaped to container")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,6 +18,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||||
|
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||||
)
|
)
|
||||||
|
|
||||||
// proxyUser is the fixed user name in a sandbox's proxy URL; the session
|
// proxyUser is the fixed user name in a sandbox's proxy URL; the session
|
||||||
@@ -35,6 +37,8 @@ var cgnat = &net.IPNet{IP: net.IPv4(100, 64, 0, 0), Mask: net.CIDRMask(10, 32)}
|
|||||||
// it to runtime before the workload's entrypoint starts.
|
// it to runtime before the workload's entrypoint starts.
|
||||||
type Session struct {
|
type Session struct {
|
||||||
token string
|
token string
|
||||||
|
runCredential string
|
||||||
|
principal access.Principal
|
||||||
org string
|
org string
|
||||||
install rules
|
install rules
|
||||||
runtime rules
|
runtime rules
|
||||||
@@ -86,9 +90,12 @@ type ProxyOptions struct {
|
|||||||
type SessionOptions struct {
|
type SessionOptions struct {
|
||||||
// Token is the run credential: the container's proxy password, and what
|
// Token is the run credential: the container's proxy password, and what
|
||||||
// the Resolver resolves the run from.
|
// the Resolver resolves the run from.
|
||||||
Token string
|
Token string
|
||||||
Org string
|
// RunCredential scopes task OAuth operations; setup sessions forward Principal.
|
||||||
Network *spec.PhasedNetwork
|
RunCredential string
|
||||||
|
Principal access.Principal
|
||||||
|
Org string
|
||||||
|
Network *spec.PhasedNetwork
|
||||||
// Credentials are what the Kit declares; Bound maps each service the run
|
// Credentials are what the Kit declares; Bound maps each service the run
|
||||||
// carries to its kind. A rule fires only for a service bound with the same
|
// carries to its kind. A rule fires only for a service bound with the same
|
||||||
// kind.
|
// kind.
|
||||||
@@ -134,7 +141,7 @@ func (p *Proxy) Register(opts SessionOptions) (*Session, error) {
|
|||||||
return nil, errors.New("egress session needs the run credential")
|
return nil, errors.New("egress session needs the run credential")
|
||||||
}
|
}
|
||||||
s := &Session{
|
s := &Session{
|
||||||
token: opts.Token, org: opts.Org,
|
token: opts.Token, org: opts.Org, runCredential: opts.RunCredential, principal: opts.Principal,
|
||||||
injections: compileInjections(opts.Credentials, opts.Bound), oauth: compileOAuthRules(opts.Credentials, opts.Bound),
|
injections: compileInjections(opts.Credentials, opts.Bound), oauth: compileOAuthRules(opts.Credentials, opts.Bound),
|
||||||
substitutions: compileSubstitutions(opts.Substitutions),
|
substitutions: compileSubstitutions(opts.Substitutions),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
|
|
||||||
pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1"
|
pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1"
|
||||||
"github.com/samcharles93/archie-core/internal/domain/presence"
|
"github.com/samcharles93/archie-core/internal/domain/presence"
|
||||||
|
"github.com/samcharles93/archie-core/internal/infrastructure/rpcidentity"
|
||||||
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
|
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -53,7 +54,7 @@ func Dial(target string, id *rpctls.Identity, options ...grpc.DialOption) (*Clie
|
|||||||
// Read.
|
// Read.
|
||||||
func (c *Client) Open(ctx context.Context, orgID, profile string, rows, cols int) (io.ReadWriteCloser, error) {
|
func (c *Client) Open(ctx context.Context, orgID, profile string, rows, cols int) (io.ReadWriteCloser, error) {
|
||||||
streamCtx, cancel := context.WithCancel(ctx)
|
streamCtx, cancel := context.WithCancel(ctx)
|
||||||
stream, err := c.svc.Open(streamCtx)
|
stream, err := c.svc.Open(rpcidentity.Outgoing(streamCtx, presence.UI))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -79,7 +80,7 @@ func (c *Client) Open(ctx context.Context, orgID, profile string, rows, cols int
|
|||||||
// map. Writes to the returned stream are not sent: a view has no stdin.
|
// map. Writes to the returned stream are not sent: a view has no stdin.
|
||||||
func (c *Client) OpenRun(ctx context.Context, orgID string, taskID, attempt int64) (io.ReadCloser, error) {
|
func (c *Client) OpenRun(ctx context.Context, orgID string, taskID, attempt int64) (io.ReadCloser, error) {
|
||||||
streamCtx, cancel := context.WithCancel(ctx)
|
streamCtx, cancel := context.WithCancel(ctx)
|
||||||
stream, err := c.svc.Open(streamCtx)
|
stream, err := c.svc.Open(rpcidentity.Outgoing(streamCtx, presence.UI))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import (
|
|||||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||||
"github.com/samcharles93/archie-core/internal/config"
|
"github.com/samcharles93/archie-core/internal/config"
|
||||||
"github.com/samcharles93/archie-core/internal/container"
|
"github.com/samcharles93/archie-core/internal/container"
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||||
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
||||||
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
||||||
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
||||||
@@ -167,7 +168,8 @@ func (l *Launcher) LaunchSetup(ctx context.Context, req SetupRequest) (*Run, err
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
session, err := l.Proxy.Register(egress.SessionOptions{Token: token, Org: req.Org, Network: k.network, Credentials: k.creds, Bound: bound})
|
principal, _ := access.PrincipalFromContext(ctx)
|
||||||
|
session, err := l.Proxy.Register(egress.SessionOptions{Principal: principal, Token: token, Org: req.Org, Network: k.network, Credentials: k.creds, Bound: bound})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -272,11 +274,11 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
kinds := l.boundKinds(req, k.creds)
|
kinds := l.boundKinds(req, k.creds)
|
||||||
facts, err := l.oauthFacts(ctx, req.Org, k.creds, kinds)
|
facts, err := l.oauthFacts(harnesssecret.WithRunCredential(ctx, req.RunCredential), req.Org, k.creds, kinds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
session, err := l.Proxy.Register(egress.SessionOptions{Token: req.RunCredential, Org: req.Org, Network: k.network, Credentials: k.creds, Bound: kinds})
|
session, err := l.Proxy.Register(egress.SessionOptions{RunCredential: req.RunCredential, Token: req.RunCredential, Org: req.Org, Network: k.network, Credentials: k.creds, Bound: kinds})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
|
||||||
"github.com/samcharles93/archie-core/internal/config"
|
"github.com/samcharles93/archie-core/internal/config"
|
||||||
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||||
@@ -877,17 +879,22 @@ func (c *Client) TaskLogContent(ctx context.Context, taskID int64, attempt int,
|
|||||||
// GetHarnessSecret returns the stored OAuth token set for an org/service
|
// GetHarnessSecret returns the stored OAuth token set for an org/service
|
||||||
// credential binding, or storecontract.ErrHarnessSecretNotFound.
|
// credential binding, or storecontract.ErrHarnessSecretNotFound.
|
||||||
func (c *Client) GetHarnessSecret(ctx context.Context, org, service string) (harnesssecret.Secret, error) {
|
func (c *Client) GetHarnessSecret(ctx context.Context, org, service string) (harnesssecret.Secret, error) {
|
||||||
r, err := c.client.GetHarnessSecret(ctx, &pb.GetHarnessSecretRequest{Org: org, Service: service})
|
r, err := c.client.GetHarnessSecret(ctx, &pb.GetHarnessSecretRequest{Service: service, RunCredential: harnesssecret.RunCredential(ctx)})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return harnesssecret.Secret{}, unmapError(err)
|
return harnesssecret.Secret{}, unmapError(err)
|
||||||
}
|
}
|
||||||
return harnessSecretValue(r.Secret), nil
|
if r.Org != org {
|
||||||
|
return harnesssecret.Secret{}, status.Error(codes.PermissionDenied, "harness secret belongs to another org")
|
||||||
|
}
|
||||||
|
value := harnessSecretValue(r.Secret)
|
||||||
|
value.Org = r.Org
|
||||||
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PutHarnessSecret upserts an org/service credential binding's OAuth token
|
// PutHarnessSecret upserts an org/service credential binding's OAuth token
|
||||||
// set, called by the setup terminal's first capture and by the egress
|
// set, called by the setup terminal's first capture and by the egress
|
||||||
// proxy's every refresh.
|
// proxy's every refresh.
|
||||||
func (c *Client) PutHarnessSecret(ctx context.Context, s harnesssecret.Secret) error {
|
func (c *Client) PutHarnessSecret(ctx context.Context, s harnesssecret.Secret) error {
|
||||||
_, err := c.client.PutHarnessSecret(ctx, &pb.PutHarnessSecretRequest{Secret: harnessSecretProto(s)})
|
_, err := c.client.PutHarnessSecret(ctx, &pb.PutHarnessSecretRequest{Secret: harnessSecretProto(s), RunCredential: harnesssecret.RunCredential(ctx)})
|
||||||
return unmapError(err)
|
return unmapError(err)
|
||||||
}
|
}
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,175 @@
|
|||||||
|
package staterpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
"google.golang.org/protobuf/encoding/protowire"
|
||||||
|
"google.golang.org/protobuf/proto"
|
||||||
|
|
||||||
|
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||||
|
)
|
||||||
|
|
||||||
|
type harnessCapture struct{ got harnesssecret.Secret }
|
||||||
|
|
||||||
|
func (h *harnessCapture) GetHarnessSecret(_ context.Context, org, service string) (harnesssecret.Secret, error) {
|
||||||
|
h.got = harnesssecret.Secret{Org: org, Service: service}
|
||||||
|
return h.got, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *harnessCapture) PutHarnessSecret(_ context.Context, value harnesssecret.Secret) error {
|
||||||
|
h.got = value
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHarnessSecretsUseCallerOrg(t *testing.T) {
|
||||||
|
for _, caller := range []org.OrgID{"acme", org.DefaultOrgID, ""} {
|
||||||
|
t.Run(string(caller), func(t *testing.T) {
|
||||||
|
capture := &harnessCapture{}
|
||||||
|
s := &server{deps: Deps{HarnessSecrets: capture}}
|
||||||
|
ctx := org.WithOrg(context.Background(), caller)
|
||||||
|
// A stale or malicious client still sending field 1 cannot choose the org.
|
||||||
|
wire := protowire.AppendString(protowire.AppendTag(nil, 1, protowire.BytesType), "victim")
|
||||||
|
req := &pb.GetHarnessSecretRequest{}
|
||||||
|
if err := proto.Unmarshal(wire, req); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
req.Service = "provider"
|
||||||
|
if _, err := s.GetHarnessSecret(ctx, req); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if capture.got.Org != string(org.OrgFromContext(ctx)) {
|
||||||
|
t.Fatalf("read org = %q, want caller org %q", capture.got.Org, org.OrgFromContext(ctx))
|
||||||
|
}
|
||||||
|
secret := &pb.HarnessSecret{}
|
||||||
|
if err := proto.Unmarshal(wire, secret); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
secret.Service = "provider"
|
||||||
|
if _, err := s.PutHarnessSecret(ctx, &pb.PutHarnessSecretRequest{Secret: secret}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if capture.got.Org != string(org.OrgFromContext(ctx)) {
|
||||||
|
t.Fatalf("write org = %q, want caller org %q", capture.got.Org, org.OrgFromContext(ctx))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type harnessTasks struct {
|
||||||
|
storecontract.TaskStore
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h harnessTasks) TaskByID(context.Context, int64) (*task.Task, error) {
|
||||||
|
return &task.Task{ID: 7, Org: "acme"}, h.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHarnessSecretsUseTaskGrant(t *testing.T) {
|
||||||
|
for _, tt := range []struct {
|
||||||
|
name, token string
|
||||||
|
scope org.OrgID
|
||||||
|
taskErr error
|
||||||
|
want codes.Code
|
||||||
|
}{
|
||||||
|
{name: "live grant", token: strings.Repeat("ab", 32)},
|
||||||
|
{name: "unknown grant", token: strings.Repeat("cd", 32), want: codes.Unauthenticated},
|
||||||
|
{name: "another org", token: strings.Repeat("ab", 32), scope: "other", want: codes.PermissionDenied},
|
||||||
|
{name: "system principal cannot cross org", token: strings.Repeat("ab", 32), scope: org.DefaultOrgID, want: codes.PermissionDenied},
|
||||||
|
{name: "task lookup failure", token: strings.Repeat("ab", 32), taskErr: errors.New("store unavailable"), want: codes.Internal},
|
||||||
|
} {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
grants := &TaskGrants{Store: credentials{}}
|
||||||
|
if err := grants.register(t.Context(), 7, strings.Repeat("ab", 32), time.Hour); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
capture := &harnessCapture{}
|
||||||
|
s := &server{deps: Deps{Grants: grants, Tasks: harnessTasks{err: tt.taskErr}, HarnessSecrets: capture}}
|
||||||
|
ctx := org.WithScope(t.Context(), tt.scope)
|
||||||
|
for _, write := range []bool{false, true} {
|
||||||
|
capture.got = harnesssecret.Secret{}
|
||||||
|
var err error
|
||||||
|
if write {
|
||||||
|
_, err = s.PutHarnessSecret(ctx, &pb.PutHarnessSecretRequest{RunCredential: tt.token, Secret: &pb.HarnessSecret{Service: "provider"}})
|
||||||
|
} else {
|
||||||
|
_, err = s.GetHarnessSecret(ctx, &pb.GetHarnessSecretRequest{RunCredential: tt.token, Service: "provider"})
|
||||||
|
}
|
||||||
|
if status.Code(err) != tt.want {
|
||||||
|
t.Fatalf("write %v: error %v, want %v", write, err, tt.want)
|
||||||
|
}
|
||||||
|
if tt.want == codes.OK && capture.got.Org != "acme" {
|
||||||
|
t.Fatalf("write %v: org %q, want acme", write, capture.got.Org)
|
||||||
|
}
|
||||||
|
if tt.want != codes.OK && capture.got.Org != "" {
|
||||||
|
t.Fatalf("unauthorized access reached store: %+v", capture.got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type harnessRPC struct {
|
||||||
|
pb.StateStoreServiceClient
|
||||||
|
server *server
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h harnessRPC) GetHarnessSecret(ctx context.Context, r *pb.GetHarnessSecretRequest, _ ...grpc.CallOption) (*pb.GetHarnessSecretResponse, error) {
|
||||||
|
return h.server.GetHarnessSecret(ctx, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h harnessRPC) PutHarnessSecret(ctx context.Context, r *pb.PutHarnessSecretRequest, _ ...grpc.CallOption) (*pb.PutHarnessSecretResponse, error) {
|
||||||
|
return h.server.PutHarnessSecret(ctx, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHarnessSecretClientGrant(t *testing.T) {
|
||||||
|
grants := &TaskGrants{Store: credentials{}}
|
||||||
|
token := strings.Repeat("ab", 32)
|
||||||
|
if err := grants.register(t.Context(), 7, token, time.Hour); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
capture := &harnessCapture{}
|
||||||
|
c := &Client{client: harnessRPC{server: &server{deps: Deps{Grants: grants, Tasks: harnessTasks{}, HarnessSecrets: capture}}}}
|
||||||
|
ctx := harnesssecret.WithRunCredential(t.Context(), token)
|
||||||
|
for _, tt := range []struct {
|
||||||
|
name, org string
|
||||||
|
revoke bool
|
||||||
|
want codes.Code
|
||||||
|
}{
|
||||||
|
{name: "task org", org: "acme"},
|
||||||
|
{name: "another org", org: "victim", want: codes.PermissionDenied},
|
||||||
|
{name: "revoked grant", org: "acme", revoke: true, want: codes.Unauthenticated},
|
||||||
|
} {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if tt.revoke {
|
||||||
|
if err := grants.revoke(ctx, token); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
secret, err := c.GetHarnessSecret(ctx, tt.org, "provider")
|
||||||
|
if status.Code(err) != tt.want {
|
||||||
|
t.Fatalf("error %v, want %v", err, tt.want)
|
||||||
|
}
|
||||||
|
if tt.want == codes.OK {
|
||||||
|
if secret.Org != "acme" {
|
||||||
|
t.Fatalf("secret org %q, want acme", secret.Org)
|
||||||
|
}
|
||||||
|
if err := c.PutHarnessSecret(ctx, secret); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if capture.got.Org != "acme" {
|
||||||
|
t.Fatalf("stored org %q, want acme", capture.got.Org)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -377,28 +377,45 @@ func (s *server) ListSteps(ctx context.Context, r *pb.ListStepsRequest) (*pb.Lis
|
|||||||
return &pb.ListStepsResponse{Steps: mapValues(steps, stepExecutionProto)}, nil
|
return &pb.ListStepsResponse{Steps: mapValues(steps, stepExecutionProto)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// confineHarnessOrg refuses a harness secret request that names an org other
|
// harnessOrg derives scope from a live run credential or the authenticated caller.
|
||||||
// than the one the caller is confined to. Internal services act unscoped and
|
func (s *server) harnessOrg(ctx context.Context, credential string) (string, error) {
|
||||||
// name the task's org; the system org operates the whole instance.
|
if credential == "" {
|
||||||
func confineHarnessOrg(ctx context.Context, target string) error {
|
return string(org.OrgFromContext(ctx)), nil
|
||||||
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
|
if s.deps.Grants == nil || s.deps.Tasks == nil {
|
||||||
|
return "", status.Error(codes.Unavailable, "task grants unavailable")
|
||||||
|
}
|
||||||
|
id, err := s.deps.Grants.taskFor(ctx, credential)
|
||||||
|
if err != nil {
|
||||||
|
return "", status.Error(codes.Unavailable, "run credentials unavailable")
|
||||||
|
}
|
||||||
|
if id == 0 {
|
||||||
|
return "", status.Error(codes.Unauthenticated, "invalid task grant")
|
||||||
|
}
|
||||||
|
t, err := s.deps.Tasks.TaskByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return "", mapError(err)
|
||||||
|
}
|
||||||
|
target := t.OrgOrDefault()
|
||||||
|
if scope, ok := org.Scope(ctx); ok && string(scope) != target {
|
||||||
|
return "", status.Error(codes.PermissionDenied, "harness secret belongs to another org")
|
||||||
|
}
|
||||||
|
return target, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) GetHarnessSecret(ctx context.Context, r *pb.GetHarnessSecretRequest) (*pb.GetHarnessSecretResponse, error) {
|
func (s *server) GetHarnessSecret(ctx context.Context, r *pb.GetHarnessSecretRequest) (*pb.GetHarnessSecretResponse, error) {
|
||||||
if s.deps.HarnessSecrets == nil {
|
if s.deps.HarnessSecrets == nil {
|
||||||
return nil, status.Error(codes.Unavailable, "harness secret store unavailable")
|
return nil, status.Error(codes.Unavailable, "harness secret store unavailable")
|
||||||
}
|
}
|
||||||
if err := confineHarnessOrg(ctx, r.Org); err != nil {
|
orgID, err := s.harnessOrg(ctx, r.RunCredential)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
secret, err := s.deps.HarnessSecrets.GetHarnessSecret(ctx, r.Org, r.Service)
|
secret, err := s.deps.HarnessSecrets.GetHarnessSecret(ctx, orgID, r.Service)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, s.logErr("GetHarnessSecret", err)
|
return nil, s.logErr("GetHarnessSecret", err)
|
||||||
}
|
}
|
||||||
return &pb.GetHarnessSecretResponse{Secret: harnessSecretProto(secret)}, nil
|
return &pb.GetHarnessSecretResponse{Secret: harnessSecretProto(secret), Org: orgID}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) PutHarnessSecret(ctx context.Context, r *pb.PutHarnessSecretRequest) (*pb.PutHarnessSecretResponse, error) {
|
func (s *server) PutHarnessSecret(ctx context.Context, r *pb.PutHarnessSecretRequest) (*pb.PutHarnessSecretResponse, error) {
|
||||||
@@ -406,9 +423,11 @@ func (s *server) PutHarnessSecret(ctx context.Context, r *pb.PutHarnessSecretReq
|
|||||||
return nil, status.Error(codes.Unavailable, "harness secret store unavailable")
|
return nil, status.Error(codes.Unavailable, "harness secret store unavailable")
|
||||||
}
|
}
|
||||||
value := harnessSecretValue(r.Secret)
|
value := harnessSecretValue(r.Secret)
|
||||||
if err := confineHarnessOrg(ctx, value.Org); err != nil {
|
orgID, err := s.harnessOrg(ctx, r.RunCredential)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
value.Org = orgID
|
||||||
if err := s.deps.HarnessSecrets.PutHarnessSecret(ctx, value); err != nil {
|
if err := s.deps.HarnessSecrets.PutHarnessSecret(ctx, value); err != nil {
|
||||||
return nil, s.logErr("PutHarnessSecret", err)
|
return nil, s.logErr("PutHarnessSecret", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -364,7 +364,7 @@ func stepExecutionValue(s *pb.StepExecution) task.StepExecution {
|
|||||||
|
|
||||||
func harnessSecretProto(s harnesssecret.Secret) *pb.HarnessSecret {
|
func harnessSecretProto(s harnesssecret.Secret) *pb.HarnessSecret {
|
||||||
return &pb.HarnessSecret{
|
return &pb.HarnessSecret{
|
||||||
Org: s.Org, Service: s.Service, AccessToken: s.AccessToken,
|
Service: s.Service, AccessToken: s.AccessToken,
|
||||||
RefreshToken: s.RefreshToken, TokenType: s.TokenType,
|
RefreshToken: s.RefreshToken, TokenType: s.TokenType,
|
||||||
ExpiresAt: timestamp(s.ExpiresAt), UpdatedAt: timestamp(s.UpdatedAt),
|
ExpiresAt: timestamp(s.ExpiresAt), UpdatedAt: timestamp(s.UpdatedAt),
|
||||||
Scopes: s.Scopes,
|
Scopes: s.Scopes,
|
||||||
@@ -376,7 +376,7 @@ func harnessSecretValue(s *pb.HarnessSecret) harnesssecret.Secret {
|
|||||||
return harnesssecret.Secret{}
|
return harnesssecret.Secret{}
|
||||||
}
|
}
|
||||||
return harnesssecret.Secret{
|
return harnesssecret.Secret{
|
||||||
Org: s.Org, Service: s.Service, AccessToken: s.AccessToken,
|
Service: s.Service, AccessToken: s.AccessToken,
|
||||||
RefreshToken: s.RefreshToken, TokenType: s.TokenType,
|
RefreshToken: s.RefreshToken, TokenType: s.TokenType,
|
||||||
ExpiresAt: timeValue(s.ExpiresAt), UpdatedAt: timeValue(s.UpdatedAt),
|
ExpiresAt: timeValue(s.ExpiresAt), UpdatedAt: timeValue(s.UpdatedAt),
|
||||||
Scopes: slices.Clone(s.Scopes),
|
Scopes: slices.Clone(s.Scopes),
|
||||||
|
|||||||
@@ -846,7 +846,8 @@ message ListStepsResponse { repeated StepExecution steps = 1; }
|
|||||||
// captured OAuth token set for a credential binding's service
|
// captured OAuth token set for a credential binding's service
|
||||||
// (docs/prds/external-agent-harness.md, Credentials).
|
// (docs/prds/external-agent-harness.md, Credentials).
|
||||||
message HarnessSecret {
|
message HarnessSecret {
|
||||||
string org = 1;
|
reserved 1;
|
||||||
|
reserved "org";
|
||||||
string service = 2;
|
string service = 2;
|
||||||
string access_token = 3;
|
string access_token = 3;
|
||||||
string refresh_token = 4;
|
string refresh_token = 4;
|
||||||
@@ -857,12 +858,20 @@ message HarnessSecret {
|
|||||||
}
|
}
|
||||||
|
|
||||||
message GetHarnessSecretRequest {
|
message GetHarnessSecretRequest {
|
||||||
string org = 1;
|
reserved 1;
|
||||||
|
reserved "org";
|
||||||
string service = 2;
|
string service = 2;
|
||||||
|
string run_credential = 3;
|
||||||
|
}
|
||||||
|
message GetHarnessSecretResponse {
|
||||||
|
HarnessSecret secret = 1;
|
||||||
|
string org = 2;
|
||||||
}
|
}
|
||||||
message GetHarnessSecretResponse { HarnessSecret secret = 1; }
|
|
||||||
|
|
||||||
message PutHarnessSecretRequest { HarnessSecret secret = 1; }
|
message PutHarnessSecretRequest {
|
||||||
|
HarnessSecret secret = 1;
|
||||||
|
string run_credential = 2;
|
||||||
|
}
|
||||||
message PutHarnessSecretResponse {}
|
message PutHarnessSecretResponse {}
|
||||||
|
|
||||||
message UpdateRequest { Task task = 1; }
|
message UpdateRequest { Task task = 1; }
|
||||||
|
|||||||
Reference in new issue
Block a user