feat(webui): verify a provider credential on a request path
The verifier was a library nothing called. A request now carries a credential or is refused: webui.Server.Authenticate resolves a presented bearer token to the identity that may act, and requireIdentity attaches it to the request context. With a provider configured that check replaces the shared token, so every request resolves to a named identity or is refused; with none, the shared-token gate stays for a loopback instance. Nothing about the caller is read from the request. The subject comes from the credential the provider signed and the identity is the record that subject is bound to, which is what makes attribution possible at all: a caller cannot assert who it is, it can only present what the provider gave it. A test sends an X-Archie-Identity header and an actor field in the body alongside a valid token and asserts the resolved identity is still the credential's. Refusals follow HTTP's own distinction: absent or unverifiable credentials are 401 and name the scheme, while a credential that verified for an identity that may not act is 403. Each is covered, because a refusal path with no test is a path nobody has watched refuse. The two state-store RPCs the dashboard needs to resolve a subject exist: ResolveIdentitySubject and BindIdentitySubject, with the client implementing identity.SubjectBinding (a narrower contract than Repository, so the administrative facade keeps its shape) and the server answering Unavailable rather than pretending when a process has no binding store. chat.proto carries the actor for the next step: actor_id, actor_kind and principal_id on ApplyOperatorTaskActionRequest, regenerated here. The transport can now carry an attribution and nothing fills it yet, which is why the dashboard still records task_approved rather than a human attribution. Not wired in production yet, and worth stating plainly: dep.Authenticate is a seam nothing sets, because the dashboard is served only by the UI process and that process is documented to take endpoint references only -- there is deliberately no config.Config in it. OIDC settings must therefore arrive as flags there, which is an existing design boundary I did not want to cross unilaterally. config.Oidc exists so the daemon-side wiring has somewhere to read from. Verified: task check exit 0 after this commit; focused webui run records the resolution and all four refusals.
This commit is contained in:
1 parent
faf32d44b5
commit
369ae695d4
14 files changed
+1391
-672
No files matched your search
@@ -322,6 +322,13 @@ func (b *boot) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps {
|
||||
if identities, ok := b.st.(identity.Repository); ok {
|
||||
deps.Identities = identities
|
||||
}
|
||||
// Subject bindings are asserted separately from the repository facade: only
|
||||
// the authenticating path resolves a verified subject, and a store that
|
||||
// cannot is one where the dashboard cannot name its caller rather than one
|
||||
// that fails to boot.
|
||||
if bindings, ok := b.st.(identity.SubjectBinding); ok {
|
||||
deps.SubjectBindings = bindings
|
||||
}
|
||||
// Task logs live in the state directory, which this process owns, and the
|
||||
// dashboard process owns no such directory -- so this is where a task-log
|
||||
// read is served from (docs/prds/ui-service-boundary.md). The reader is
|
||||
|
||||
@@ -27,6 +27,9 @@ type deps struct {
|
||||
Health *health.Registry
|
||||
ControlPlane controlpb.ControlPlaneServiceClient
|
||||
Identities identity.Repository
|
||||
// Authenticate resolves a presented credential to the identity that may
|
||||
// act. Nil when no provider is configured, which keeps the shared token.
|
||||
Authenticate func(context.Context, string) (identity.Identity, error)
|
||||
}
|
||||
|
||||
// compose builds the dashboard server for the UI process. It sets exactly the
|
||||
@@ -67,6 +70,7 @@ func compose(d deps) *webui.Server {
|
||||
Store: d.Store,
|
||||
Log: d.Log,
|
||||
Token: d.Options.Token,
|
||||
Authenticate: d.Authenticate,
|
||||
TrustForwardedHeaders: d.Options.trustForwardedHeaders(),
|
||||
Health: d.Health,
|
||||
ControlPlane: d.ControlPlane,
|
||||
|
||||
@@ -514,6 +514,7 @@ type Config struct {
|
||||
|
||||
Budgets Budgets `toml:"budgets" yaml:"budgets"`
|
||||
Web Web `toml:"web" yaml:"web"`
|
||||
Oidc Oidc `toml:"oidc" yaml:"oidc"`
|
||||
Health Health `toml:"health" yaml:"health"`
|
||||
Log Log `toml:"log" yaml:"log"`
|
||||
Notify Notify `toml:"notify" yaml:"notify"`
|
||||
@@ -1064,6 +1065,19 @@ type Notify struct {
|
||||
Webhook string `toml:"webhook" json:"webhook" yaml:"webhook"`
|
||||
}
|
||||
|
||||
// Oidc configures verification of identity-provider tokens. An empty issuer
|
||||
// disables it, and the dashboard keeps its shared-token gate -- which attributes
|
||||
// a request to a credential, not to a person.
|
||||
type Oidc struct {
|
||||
// Issuer is the provider's issuer URL. Discovery and the signing keys are
|
||||
// read from it, so archie holds no key material of its own.
|
||||
Issuer string `toml:"issuer" yaml:"issuer"`
|
||||
// Audience is the resource identifier archie accepts tokens for. Required
|
||||
// when an issuer is set: without it a token the provider minted for another
|
||||
// service would authenticate here.
|
||||
Audience string `toml:"audience" yaml:"audience"`
|
||||
}
|
||||
|
||||
// Web configures the observability dashboard.
|
||||
// Health is archied's own liveness surface, always on. It is separate from
|
||||
// the dashboard's /healthz because the process whose restart is being
|
||||
|
||||
@@ -1646,9 +1646,20 @@ func (x *ApplyTaskActionResponse) GetMessage() string {
|
||||
// carries no identity to scope against -- the distinction the daemon's task
|
||||
// action service draws with a nil identity (internal/domain/taskactions).
|
||||
type ApplyOperatorTaskActionRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
TaskId int64 `protobuf:"varint,1,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"`
|
||||
Action string `protobuf:"bytes,2,opt,name=action,proto3" json:"action,omitempty"`
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
TaskId int64 `protobuf:"varint,1,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"`
|
||||
Action string `protobuf:"bytes,2,opt,name=action,proto3" json:"action,omitempty"`
|
||||
// actor_id is the identity that performed the action, as asserted by the
|
||||
// caller that verified the credential the request presented. actor_kind
|
||||
// travels beside it so the record can say an agent acted without a reader
|
||||
// resolving the identity. principal_id is the identity whose authority the
|
||||
// action used; empty means the action is recorded unattributed rather than
|
||||
// credited to the actor's own authority. All three are empty when the caller
|
||||
// has no verified identity, which is recorded as an unattributed action and
|
||||
// never as a human's.
|
||||
ActorId string `protobuf:"bytes,3,opt,name=actor_id,json=actorId,proto3" json:"actor_id,omitempty"`
|
||||
ActorKind string `protobuf:"bytes,4,opt,name=actor_kind,json=actorKind,proto3" json:"actor_kind,omitempty"`
|
||||
PrincipalId string `protobuf:"bytes,5,opt,name=principal_id,json=principalId,proto3" json:"principal_id,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@@ -1697,6 +1708,27 @@ func (x *ApplyOperatorTaskActionRequest) GetAction() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *ApplyOperatorTaskActionRequest) GetActorId() string {
|
||||
if x != nil {
|
||||
return x.ActorId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *ApplyOperatorTaskActionRequest) GetActorKind() string {
|
||||
if x != nil {
|
||||
return x.ActorKind
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *ApplyOperatorTaskActionRequest) GetPrincipalId() string {
|
||||
if x != nil {
|
||||
return x.PrincipalId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type ApplyOperatorTaskActionResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
TaskId int64 `protobuf:"varint,1,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"`
|
||||
@@ -2924,10 +2956,14 @@ const file_gateway_v1_chat_proto_rawDesc = "" +
|
||||
"\x17ApplyTaskActionResponse\x12\x17\n" +
|
||||
"\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x16\n" +
|
||||
"\x06action\x18\x02 \x01(\tR\x06action\x12\x18\n" +
|
||||
"\amessage\x18\x03 \x01(\tR\amessage\"Q\n" +
|
||||
"\amessage\x18\x03 \x01(\tR\amessage\"\xae\x01\n" +
|
||||
"\x1eApplyOperatorTaskActionRequest\x12\x17\n" +
|
||||
"\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x16\n" +
|
||||
"\x06action\x18\x02 \x01(\tR\x06action\"l\n" +
|
||||
"\x06action\x18\x02 \x01(\tR\x06action\x12\x19\n" +
|
||||
"\bactor_id\x18\x03 \x01(\tR\aactorId\x12\x1d\n" +
|
||||
"\n" +
|
||||
"actor_kind\x18\x04 \x01(\tR\tactorKind\x12!\n" +
|
||||
"\fprincipal_id\x18\x05 \x01(\tR\vprincipalId\"l\n" +
|
||||
"\x1fApplyOperatorTaskActionResponse\x12\x17\n" +
|
||||
"\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x16\n" +
|
||||
"\x06action\x18\x02 \x01(\tR\x06action\x12\x18\n" +
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -28,6 +28,8 @@ const (
|
||||
StateStoreService_SuspendIdentity_FullMethodName = "/state.v1.StateStoreService/SuspendIdentity"
|
||||
StateStoreService_ReactivateIdentity_FullMethodName = "/state.v1.StateStoreService/ReactivateIdentity"
|
||||
StateStoreService_RetireIdentity_FullMethodName = "/state.v1.StateStoreService/RetireIdentity"
|
||||
StateStoreService_ResolveIdentitySubject_FullMethodName = "/state.v1.StateStoreService/ResolveIdentitySubject"
|
||||
StateStoreService_BindIdentitySubject_FullMethodName = "/state.v1.StateStoreService/BindIdentitySubject"
|
||||
StateStoreService_EnqueueIssue_FullMethodName = "/state.v1.StateStoreService/EnqueueIssue"
|
||||
StateStoreService_EnqueueChatTask_FullMethodName = "/state.v1.StateStoreService/EnqueueChatTask"
|
||||
StateStoreService_ClaimNext_FullMethodName = "/state.v1.StateStoreService/ClaimNext"
|
||||
@@ -105,6 +107,11 @@ type StateStoreServiceClient interface {
|
||||
SuspendIdentity(ctx context.Context, in *SuspendIdentityRequest, opts ...grpc.CallOption) (*SuspendIdentityResponse, error)
|
||||
ReactivateIdentity(ctx context.Context, in *ReactivateIdentityRequest, opts ...grpc.CallOption) (*ReactivateIdentityResponse, error)
|
||||
RetireIdentity(ctx context.Context, in *RetireIdentityRequest, opts ...grpc.CallOption) (*RetireIdentityResponse, error)
|
||||
// Subject bindings. A verified provider subject is resolved to the identity it
|
||||
// is bound to; the authenticating process calls these rather than owning the
|
||||
// identity store.
|
||||
ResolveIdentitySubject(ctx context.Context, in *ResolveIdentitySubjectRequest, opts ...grpc.CallOption) (*ResolveIdentitySubjectResponse, error)
|
||||
BindIdentitySubject(ctx context.Context, in *BindIdentitySubjectRequest, opts ...grpc.CallOption) (*BindIdentitySubjectResponse, error)
|
||||
// Lifecycle
|
||||
EnqueueIssue(ctx context.Context, in *EnqueueIssueRequest, opts ...grpc.CallOption) (*EnqueueIssueResponse, error)
|
||||
EnqueueChatTask(ctx context.Context, in *EnqueueChatTaskRequest, opts ...grpc.CallOption) (*EnqueueChatTaskResponse, error)
|
||||
@@ -305,6 +312,26 @@ func (c *stateStoreServiceClient) RetireIdentity(ctx context.Context, in *Retire
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) ResolveIdentitySubject(ctx context.Context, in *ResolveIdentitySubjectRequest, opts ...grpc.CallOption) (*ResolveIdentitySubjectResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(ResolveIdentitySubjectResponse)
|
||||
err := c.cc.Invoke(ctx, StateStoreService_ResolveIdentitySubject_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) BindIdentitySubject(ctx context.Context, in *BindIdentitySubjectRequest, opts ...grpc.CallOption) (*BindIdentitySubjectResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(BindIdentitySubjectResponse)
|
||||
err := c.cc.Invoke(ctx, StateStoreService_BindIdentitySubject_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) EnqueueIssue(ctx context.Context, in *EnqueueIssueRequest, opts ...grpc.CallOption) (*EnqueueIssueResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(EnqueueIssueResponse)
|
||||
@@ -877,6 +904,11 @@ type StateStoreServiceServer interface {
|
||||
SuspendIdentity(context.Context, *SuspendIdentityRequest) (*SuspendIdentityResponse, error)
|
||||
ReactivateIdentity(context.Context, *ReactivateIdentityRequest) (*ReactivateIdentityResponse, error)
|
||||
RetireIdentity(context.Context, *RetireIdentityRequest) (*RetireIdentityResponse, error)
|
||||
// Subject bindings. A verified provider subject is resolved to the identity it
|
||||
// is bound to; the authenticating process calls these rather than owning the
|
||||
// identity store.
|
||||
ResolveIdentitySubject(context.Context, *ResolveIdentitySubjectRequest) (*ResolveIdentitySubjectResponse, error)
|
||||
BindIdentitySubject(context.Context, *BindIdentitySubjectRequest) (*BindIdentitySubjectResponse, error)
|
||||
// Lifecycle
|
||||
EnqueueIssue(context.Context, *EnqueueIssueRequest) (*EnqueueIssueResponse, error)
|
||||
EnqueueChatTask(context.Context, *EnqueueChatTaskRequest) (*EnqueueChatTaskResponse, error)
|
||||
@@ -1014,6 +1046,12 @@ func (UnimplementedStateStoreServiceServer) ReactivateIdentity(context.Context,
|
||||
func (UnimplementedStateStoreServiceServer) RetireIdentity(context.Context, *RetireIdentityRequest) (*RetireIdentityResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method RetireIdentity not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) ResolveIdentitySubject(context.Context, *ResolveIdentitySubjectRequest) (*ResolveIdentitySubjectResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method ResolveIdentitySubject not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) BindIdentitySubject(context.Context, *BindIdentitySubjectRequest) (*BindIdentitySubjectResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method BindIdentitySubject not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) EnqueueIssue(context.Context, *EnqueueIssueRequest) (*EnqueueIssueResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method EnqueueIssue not implemented")
|
||||
}
|
||||
@@ -1353,6 +1391,42 @@ func _StateStoreService_RetireIdentity_Handler(srv interface{}, ctx context.Cont
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_ResolveIdentitySubject_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(ResolveIdentitySubjectRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StateStoreServiceServer).ResolveIdentitySubject(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: StateStoreService_ResolveIdentitySubject_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StateStoreServiceServer).ResolveIdentitySubject(ctx, req.(*ResolveIdentitySubjectRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_BindIdentitySubject_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(BindIdentitySubjectRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StateStoreServiceServer).BindIdentitySubject(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: StateStoreService_BindIdentitySubject_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StateStoreServiceServer).BindIdentitySubject(ctx, req.(*BindIdentitySubjectRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_EnqueueIssue_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(EnqueueIssueRequest)
|
||||
if err := dec(in); err != nil {
|
||||
@@ -2311,6 +2385,14 @@ var StateStoreService_ServiceDesc = grpc.ServiceDesc{
|
||||
MethodName: "RetireIdentity",
|
||||
Handler: _StateStoreService_RetireIdentity_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "ResolveIdentitySubject",
|
||||
Handler: _StateStoreService_ResolveIdentitySubject_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "BindIdentitySubject",
|
||||
Handler: _StateStoreService_BindIdentitySubject_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "EnqueueIssue",
|
||||
Handler: _StateStoreService_EnqueueIssue_Handler,
|
||||
|
||||
@@ -39,6 +39,15 @@ type Verifier interface {
|
||||
Verify(ctx context.Context, rawToken string) (Credential, error)
|
||||
}
|
||||
|
||||
// SubjectBinding resolves a verified provider subject and binds one to an
|
||||
// identity. It is the pair a store implements to support authentication, kept
|
||||
// separate from Repository so the administrative facade keeps its shape and a
|
||||
// consumer that only authenticates takes a narrower dependency.
|
||||
type SubjectBinding interface {
|
||||
SubjectResolver
|
||||
BindSubject(context.Context, IdentityID, Subject, Audit) error
|
||||
}
|
||||
|
||||
// SubjectResolver resolves a verified provider subject to the identity bound to
|
||||
// it. It is deliberately narrower than Repository: authenticating a request
|
||||
// needs to resolve a subject, not to administer identities, so a consumer that
|
||||
|
||||
@@ -69,6 +69,46 @@ func (s *server) RetireIdentity(ctx context.Context, request *pb.RetireIdentityR
|
||||
return &pb.RetireIdentityResponse{Identity: identityProto(value)}, err
|
||||
}
|
||||
|
||||
func (s *server) ResolveIdentitySubject(ctx context.Context, request *pb.ResolveIdentitySubjectRequest) (*pb.ResolveIdentitySubjectResponse, error) {
|
||||
if s.deps.SubjectBindings == nil {
|
||||
return nil, status.Error(codes.Unavailable, "identity subject bindings unavailable")
|
||||
}
|
||||
value, err := s.deps.SubjectBindings.ResolveSubject(ctx, identity.Subject{Issuer: request.Issuer, Subject: request.Subject})
|
||||
if err != nil {
|
||||
return nil, identityStatus(err)
|
||||
}
|
||||
return &pb.ResolveIdentitySubjectResponse{Identity: identityProto(value)}, nil
|
||||
}
|
||||
|
||||
func (s *server) BindIdentitySubject(ctx context.Context, request *pb.BindIdentitySubjectRequest) (*pb.BindIdentitySubjectResponse, error) {
|
||||
if s.deps.SubjectBindings == nil {
|
||||
return nil, status.Error(codes.Unavailable, "identity subject bindings unavailable")
|
||||
}
|
||||
subject := identity.Subject{Issuer: request.Issuer, Subject: request.Subject}
|
||||
if err := s.deps.SubjectBindings.BindSubject(ctx, identity.IdentityID(request.Id), subject, identityAudit(request.Audit)); err != nil {
|
||||
return nil, identityStatus(err)
|
||||
}
|
||||
value, err := s.deps.Identities.Get(ctx, identity.IdentityID(request.Id))
|
||||
if err != nil {
|
||||
return nil, identityStatus(err)
|
||||
}
|
||||
return &pb.BindIdentitySubjectResponse{Identity: identityProto(value)}, nil
|
||||
}
|
||||
|
||||
func (c *Client) ResolveSubject(ctx context.Context, subject identity.Subject) (identity.Identity, error) {
|
||||
response, err := c.client.ResolveIdentitySubject(ctx, &pb.ResolveIdentitySubjectRequest{Issuer: subject.Issuer, Subject: subject.Subject})
|
||||
if err != nil {
|
||||
return identity.Identity{}, identityClientError(err)
|
||||
}
|
||||
return identityValue(response.Identity), nil
|
||||
}
|
||||
|
||||
func (c *Client) BindSubject(ctx context.Context, id identity.IdentityID, subject identity.Subject, audit identity.Audit) error {
|
||||
_, err := c.client.BindIdentitySubject(ctx, &pb.BindIdentitySubjectRequest{Id: string(id), Issuer: subject.Issuer, Subject: subject.Subject, Audit: auditProto(audit)})
|
||||
return identityClientError(err)
|
||||
}
|
||||
|
||||
// applyIdentityCommand applies one lifecycle command through the store facade.
|
||||
func (s *server) applyIdentityCommand(ctx context.Context, id string, version int64, audit *pb.IdentityAudit, command identity.Command) (identity.Identity, error) {
|
||||
value, err := s.deps.Identities.Apply(ctx, identity.IdentityID(id), version, command, identityAudit(audit))
|
||||
if err != nil {
|
||||
|
||||
@@ -60,8 +60,12 @@ const taskLogChunkBytes = 256 << 10
|
||||
// codes.Unavailable) exactly as their daemon-side consumers already treat a
|
||||
// nil Mappings/Bindings/BindingDispatcher/BindingTaskCreator as "disabled".
|
||||
type Deps struct {
|
||||
ControlPlane controlpb.ControlPlaneServiceServer
|
||||
Identities identity.Repository
|
||||
ControlPlane controlpb.ControlPlaneServiceServer
|
||||
Identities identity.Repository
|
||||
// SubjectBindings fronts the subject-binding surface, which only the
|
||||
// authenticating path needs. Unset means this process cannot resolve a
|
||||
// verified subject, and the RPCs answer Unavailable rather than pretending.
|
||||
SubjectBindings identity.SubjectBinding
|
||||
Grants *TaskGrants
|
||||
Tasks storecontract.TaskStore
|
||||
Captures storecontract.CaptureStore
|
||||
|
||||
+90
-2
@@ -1,9 +1,11 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
@@ -12,6 +14,8 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
)
|
||||
|
||||
// tokenCookie carries the dashboard token once it has been exchanged from the
|
||||
@@ -89,8 +93,12 @@ func LoadOrCreateToken(path string) (string, error) {
|
||||
return tok, nil
|
||||
}
|
||||
|
||||
// requireToken wraps h with token authentication. A zero token disables the
|
||||
// check entirely, which is how loopback binds stay frictionless.
|
||||
// requireToken wraps h with the credential check this process is configured for.
|
||||
//
|
||||
// With an identity provider configured, the check is a provider-issued bearer
|
||||
// token that resolves to a named identity, and a request that cannot produce one
|
||||
// is refused. With no provider, the shared token is the gate, which is the
|
||||
// frictionless loopback behaviour a single-operator instance wants.
|
||||
//
|
||||
// The token may arrive as ?t=... once; it is then moved into a
|
||||
// SameSite=Strict, HttpOnly cookie and the caller redirected to the clean URL.
|
||||
@@ -98,6 +106,9 @@ func LoadOrCreateToken(path string) (string, error) {
|
||||
// The token is read per request rather than captured when the handler is
|
||||
// built, so a Server whose Token is set after Handler() is still protected.
|
||||
func (s *Server) requireToken(h http.Handler) http.Handler {
|
||||
if s.Authenticate != nil {
|
||||
return s.requireIdentity(h)
|
||||
}
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Token == "" {
|
||||
h.ServeHTTP(w, r)
|
||||
@@ -149,6 +160,83 @@ func (s *Server) requireToken(h http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
// requireIdentity authenticates a request from a provider-issued bearer token and
|
||||
// attaches the identity that token resolved to.
|
||||
//
|
||||
// Nothing about the caller is read from the request body, a header or a query
|
||||
// parameter: the subject comes from the credential the provider signed, and the
|
||||
// identity is the record that subject is bound to. That is why an agent's action
|
||||
// can be attributed at all -- a caller cannot assert who it is, it can only
|
||||
// present what the provider gave it.
|
||||
func (s *Server) requireIdentity(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
credential, ok := bearerCredential(r)
|
||||
if !ok {
|
||||
s.refuseUnidentified(w, r, identity.ErrNoCredential)
|
||||
return
|
||||
}
|
||||
value, err := s.Authenticate(r.Context(), credential)
|
||||
if err != nil {
|
||||
s.refuseUnidentified(w, r, err)
|
||||
return
|
||||
}
|
||||
h.ServeHTTP(w, r.WithContext(WithActingIdentity(r.Context(), value)))
|
||||
})
|
||||
}
|
||||
|
||||
// bearerCredential reads the credential a request presented. An empty token is
|
||||
// reported as absent rather than as a rejected one: there is nothing to verify.
|
||||
func bearerCredential(r *http.Request) (string, bool) {
|
||||
const prefix = "Bearer "
|
||||
header := r.Header.Get("Authorization")
|
||||
if len(header) <= len(prefix) || !strings.EqualFold(header[:len(prefix)], prefix) {
|
||||
return "", false
|
||||
}
|
||||
token := strings.TrimSpace(header[len(prefix):])
|
||||
return token, token != ""
|
||||
}
|
||||
|
||||
// refuseUnidentified answers a request archie could not attribute to an identity.
|
||||
//
|
||||
// The status is HTTP's own distinction: absent or unverifiable credentials are
|
||||
// 401 and name the scheme, while a credential that verified for an identity that
|
||||
// may not act is 403 -- the caller proved who it is and is still not allowed.
|
||||
func (s *Server) refuseUnidentified(w http.ResponseWriter, r *http.Request, err error) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
allowed := errors.Is(err, identity.ErrIdentityInactive) || errors.Is(err, identity.ErrSubjectUnbound)
|
||||
status := http.StatusUnauthorized
|
||||
reason := "That credential was not accepted."
|
||||
switch {
|
||||
case errors.Is(err, identity.ErrIdentityInactive):
|
||||
status, reason = http.StatusForbidden, "That identity may not act."
|
||||
case errors.Is(err, identity.ErrSubjectUnbound):
|
||||
status, reason = http.StatusForbidden, "That credential is not bound to an identity here."
|
||||
default:
|
||||
w.Header().Set("WWW-Authenticate", `Bearer realm="archie"`)
|
||||
}
|
||||
if !allowed && wantsDocument(r) {
|
||||
s.authPage(w, reason)
|
||||
return
|
||||
}
|
||||
http.Error(w, reason, status)
|
||||
}
|
||||
|
||||
type actingIdentityContextKey struct{}
|
||||
|
||||
// WithActingIdentity returns a context carrying the identity a request resolved
|
||||
// to, so a handler can attribute what it does without re-verifying anything.
|
||||
func WithActingIdentity(ctx context.Context, value identity.Identity) context.Context {
|
||||
return context.WithValue(ctx, actingIdentityContextKey{}, value)
|
||||
}
|
||||
|
||||
// ActingIdentity returns the identity a request resolved to, if it resolved to
|
||||
// one. A false result means the request was not authenticated, which is a
|
||||
// different fact from an authenticated request whose actor is unrecorded.
|
||||
func ActingIdentity(ctx context.Context) (identity.Identity, bool) {
|
||||
value, ok := ctx.Value(actingIdentityContextKey{}).(identity.Identity)
|
||||
return value, ok
|
||||
}
|
||||
|
||||
// wantsDocument reports whether a request is a browser navigation rather than an
|
||||
// API or stream call. A navigation sends text/html in Accept; fetch() sends */*
|
||||
// or a JSON type, and EventSource sends text/event-stream.
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
)
|
||||
|
||||
var testActingIdentity = identity.Identity{
|
||||
ID: "40000000-0000-5000-8000-000000000001",
|
||||
Kind: identity.KindUser,
|
||||
DisplayName: "sam",
|
||||
Lifecycle: identity.LifecycleActive,
|
||||
Version: 1,
|
||||
}
|
||||
|
||||
// withIdentityCheck builds the middleware over a stub provider, which is the
|
||||
// seam the dashboard sees: verification and binding are proved in
|
||||
// internal/infrastructure/oidc and internal/domain/identity, and what is proved
|
||||
// here is that a request either resolves or is refused.
|
||||
func withIdentityCheck(t *testing.T, authenticate func(context.Context, string) (identity.Identity, error)) http.Handler {
|
||||
t.Helper()
|
||||
s := &Server{Authenticate: authenticate}
|
||||
return s.requireToken(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
value, ok := ActingIdentity(r.Context())
|
||||
if !ok {
|
||||
t.Error("handler ran without a resolved identity")
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(string(value.ID)))
|
||||
}))
|
||||
}
|
||||
|
||||
func TestIdentityRequestResolvesToTheCredentialSubject(t *testing.T) {
|
||||
seen := ""
|
||||
handler := withIdentityCheck(t, func(_ context.Context, raw string) (identity.Identity, error) {
|
||||
seen = raw
|
||||
return testActingIdentity, nil
|
||||
})
|
||||
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/tasks/7/action", strings.NewReader(`{"actor":"someone-else"}`))
|
||||
request.Header.Set("Authorization", "Bearer "+"a-provider-token")
|
||||
// A caller-supplied identity must not be consulted: the only source of the
|
||||
// acting identity is the credential the provider signed.
|
||||
request.Header.Set("X-Archie-Identity", "attacker-chosen")
|
||||
request.Header.Set("X-Archie-Actor", "attacker-chosen")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, request)
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusOK)
|
||||
}
|
||||
if seen != "a-provider-token" {
|
||||
t.Fatalf("verifier saw %q, want the presented credential", seen)
|
||||
}
|
||||
if got := recorder.Body.String(); got != string(testActingIdentity.ID) {
|
||||
t.Fatalf("resolved identity = %q, want %q", got, testActingIdentity.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdentityRequestRefusesEveryCallerItCannotAttribute(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
credential string
|
||||
authErr error
|
||||
wantStatus int
|
||||
}{
|
||||
{
|
||||
name: "no credential presented",
|
||||
credential: "",
|
||||
wantStatus: http.StatusUnauthorized,
|
||||
},
|
||||
{
|
||||
name: "credential rejected",
|
||||
credential: "Bearer expired-or-foreign",
|
||||
authErr: identity.ErrCredentialRejected,
|
||||
wantStatus: http.StatusUnauthorized,
|
||||
},
|
||||
{
|
||||
name: "subject not bound to an identity",
|
||||
credential: "Bearer valid-but-unknown",
|
||||
authErr: identity.ErrSubjectUnbound,
|
||||
wantStatus: http.StatusForbidden,
|
||||
},
|
||||
{
|
||||
name: "identity may not act",
|
||||
credential: "Bearer valid-but-suspended",
|
||||
authErr: identity.ErrIdentityInactive,
|
||||
wantStatus: http.StatusForbidden,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
handler := withIdentityCheck(t, func(_ context.Context, _ string) (identity.Identity, error) {
|
||||
if tc.authErr == nil {
|
||||
t.Fatal("the verifier was called with no credential to verify")
|
||||
}
|
||||
return identity.Identity{}, tc.authErr
|
||||
})
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/tasks", nil)
|
||||
if tc.credential != "" {
|
||||
request.Header.Set("Authorization", tc.credential)
|
||||
}
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, request)
|
||||
|
||||
if recorder.Code != tc.wantStatus {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, tc.wantStatus)
|
||||
}
|
||||
if tc.wantStatus == http.StatusUnauthorized &&
|
||||
recorder.Header().Get("WWW-Authenticate") == "" {
|
||||
t.Fatal("a 401 must name the scheme it accepts")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestIdentityCheckReplacesTheSharedToken: with a provider configured the shared
|
||||
// token is no longer a way in, because a credential that names nobody cannot
|
||||
// attribute what it does.
|
||||
func TestIdentityCheckReplacesTheSharedToken(t *testing.T) {
|
||||
handler := withIdentityCheck(t, func(_ context.Context, raw string) (identity.Identity, error) {
|
||||
if raw == "" {
|
||||
return identity.Identity{}, identity.ErrNoCredential
|
||||
}
|
||||
return testActingIdentity, nil
|
||||
})
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
request.AddCookie(&http.Cookie{Name: tokenCookie, Value: "the-shared-token"})
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, request)
|
||||
|
||||
if recorder.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSharedTokenStillGatesAnInstanceWithNoProvider: with no provider configured
|
||||
// the shared token remains the gate, which is the loopback-friendly behaviour a
|
||||
// single-operator instance relies on.
|
||||
func TestSharedTokenStillGatesAnInstanceWithNoProvider(t *testing.T) {
|
||||
s := &Server{Token: ""}
|
||||
reached := false
|
||||
handler := s.requireToken(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached = true }))
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, request)
|
||||
|
||||
if !reached {
|
||||
t.Fatal("a token-less loopback server refused a request")
|
||||
}
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusOK)
|
||||
}
|
||||
}
|
||||
@@ -100,6 +100,13 @@ type Server struct {
|
||||
// stay frictionless -- see IsLoopback.
|
||||
Token string
|
||||
|
||||
// Authenticate resolves a credential a request presented to the identity
|
||||
// that may act, and is the whole of the provider integration: verification
|
||||
// and binding both live behind it. Non-nil replaces the shared token, so
|
||||
// every request resolves to a named identity or is refused; nil keeps the
|
||||
// shared-token gate for an instance with no provider configured.
|
||||
Authenticate func(context.Context, string) (identity.Identity, error)
|
||||
|
||||
// Events publishes operator actions so they reach the task timeline and
|
||||
// the live activity stream. Optional: nil means the action is recorded
|
||||
// in the store but invisible to anyone watching.
|
||||
|
||||
@@ -141,7 +141,21 @@ message ApplyTaskActionResponse { int64 task_id = 1; string action = 2; string m
|
||||
// An authenticated dashboard operator acts across identities, so this request
|
||||
// carries no identity to scope against -- the distinction the daemon's task
|
||||
// action service draws with a nil identity (internal/domain/taskactions).
|
||||
message ApplyOperatorTaskActionRequest { int64 task_id = 1; string action = 2; }
|
||||
message ApplyOperatorTaskActionRequest {
|
||||
int64 task_id = 1;
|
||||
string action = 2;
|
||||
// actor_id is the identity that performed the action, as asserted by the
|
||||
// caller that verified the credential the request presented. actor_kind
|
||||
// travels beside it so the record can say an agent acted without a reader
|
||||
// resolving the identity. principal_id is the identity whose authority the
|
||||
// action used; empty means the action is recorded unattributed rather than
|
||||
// credited to the actor's own authority. All three are empty when the caller
|
||||
// has no verified identity, which is recorded as an unattributed action and
|
||||
// never as a human's.
|
||||
string actor_id = 3;
|
||||
string actor_kind = 4;
|
||||
string principal_id = 5;
|
||||
}
|
||||
message ApplyOperatorTaskActionResponse { int64 task_id = 1; string action = 2; string message = 3; }
|
||||
message SaveSessionResponse {}
|
||||
message SaveSessionRequest { Session session = 1; }
|
||||
|
||||
@@ -23,6 +23,11 @@ service StateStoreService {
|
||||
rpc SuspendIdentity(SuspendIdentityRequest) returns (SuspendIdentityResponse);
|
||||
rpc ReactivateIdentity(ReactivateIdentityRequest) returns (ReactivateIdentityResponse);
|
||||
rpc RetireIdentity(RetireIdentityRequest) returns (RetireIdentityResponse);
|
||||
// Subject bindings. A verified provider subject is resolved to the identity it
|
||||
// is bound to; the authenticating process calls these rather than owning the
|
||||
// identity store.
|
||||
rpc ResolveIdentitySubject(ResolveIdentitySubjectRequest) returns (ResolveIdentitySubjectResponse);
|
||||
rpc BindIdentitySubject(BindIdentitySubjectRequest) returns (BindIdentitySubjectResponse);
|
||||
// Lifecycle
|
||||
rpc EnqueueIssue(EnqueueIssueRequest) returns (EnqueueIssueResponse);
|
||||
rpc EnqueueChatTask(EnqueueChatTaskRequest) returns (EnqueueChatTaskResponse);
|
||||
@@ -189,6 +194,18 @@ message RenameIdentityResponse { Identity identity = 1; }
|
||||
message SuspendIdentityResponse { Identity identity = 1; }
|
||||
message ReactivateIdentityResponse { Identity identity = 1; }
|
||||
message RetireIdentityResponse { Identity identity = 1; }
|
||||
message ResolveIdentitySubjectRequest {
|
||||
string issuer = 1;
|
||||
string subject = 2;
|
||||
}
|
||||
message ResolveIdentitySubjectResponse { Identity identity = 1; }
|
||||
message BindIdentitySubjectRequest {
|
||||
string id = 1;
|
||||
string issuer = 2;
|
||||
string subject = 3;
|
||||
IdentityAudit audit = 4;
|
||||
}
|
||||
message BindIdentitySubjectResponse { Identity identity = 1; }
|
||||
|
||||
// Task mirrors internal/domain/workflow.Task.
|
||||
message Task {
|
||||
|
||||
Reference in new issue
Block a user