Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,10 @@ linters:
- github.com/stacklok/toolhive/pkg/authserver/storage$
- github.com/stacklok/toolhive/pkg/oauthproto$
- github.com/stacklok/mecatl/internal/identitybroker/adapter/inprocesshttp$
# AS-1's actor-token validator implements ToolHive's interface by
# calling the broker's CredentialVerifier.
- github.com/stacklok/toolhive/pkg/authserver/server/tokenexchange$
- github.com/stacklok/mecatl/internal/identitybroker$
# The real-AS-1 test fixture shared by the broker, authserverclient,
# app, and mecak8s tests. It reaches AS-1 only through as1server's
# exported API and never ToolHive directly.
Expand Down
3 changes: 3 additions & 0 deletions internal/adapter/mcpbroker/toolhive_boundary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func TestToolHiveImportsStayBehindApprovedAdapterLeaves(t *testing.T) {
"internal/identitybroker/adapter/as1server/as1server.go": true,
// The identity broker's delegate client at AS-1 (#2607).
"internal/identitybroker/adapter/as1server/delegate.go": true,
// AS-1's actor-token validator, which implements ToolHive's
// interface with the broker's CredentialVerifier (#2654).
"internal/identitybroker/adapter/as1server/actorvalidator.go": true,
}
const toolHiveModule = "github.com/stacklok/toolhive"
if err := filepath.WalkDir(root, func(sourcePath string, entry fs.DirEntry, walkErr error) error {
Expand Down
103 changes: 103 additions & 0 deletions internal/identitybroker/adapter/as1server/actorvalidator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package as1server

import (
"context"
"errors"
"strings"
"sync/atomic"

"github.com/stacklok/toolhive/pkg/authserver/server/tokenexchange"

"github.com/stacklok/mecatl/internal/identitybroker"
)

// actorValidatorName is the name AS-1 registers the identity broker's
// actor-token validator under, which the delegate client's actor profile
// selects. ToolHive reserves names starting with "toolhive.".
const actorValidatorName = "mecatl"

// The plain errors the validator returns, which AS-1 answers with a
// retryable server_error. ToolHive logs their text, so it is fixed: never the
// broker's error text, a user identifier, or a token.
var (
errVerifierNotBound = errors.New("as1server: the identity broker is not ready to check actor credentials")
errGrantSubjectID = errors.New("as1server: the grant has no iss_sub sub_id")
errActorCheckFailed = errors.New("as1server: the identity broker could not check the actor credential")
)

// actorValidator is AS-1's actor-token check of the delegate client's ID-JAG
// requests (design step (7), check 6): it asks the identity broker's
// CredentialVerifier whether the actor token is a valid credential of an
// agent in an open session of the grant's owner (check 8). It never falls
// back to another check.
type actorValidator struct {
// clientID is the delegate client's ID, the only client it accepts.
clientID string
// actIssuer is act.iss, the trust domain every agent's act.sub is in.
actIssuer string
// verifier is nil until Server.UseCredentialVerifier binds it.
verifier atomic.Pointer[identitybroker.CredentialVerifier]
}

var _ tokenexchange.ActorTokenValidator = (*actorValidator)(nil)

// ValidateActor implements tokenexchange.ActorTokenValidator. An expired
// credential is actor_token_expired, the broker's denied refusal, or a request
// from another client, is denied, and everything else, including an unbound
// verifier, a grant without an iss_sub sub_id, and any error outside
// CredentialVerifier's contract, is a plain error.
func (a *actorValidator) ValidateActor(ctx context.Context, req tokenexchange.ActorRequest) (tokenexchange.ActorResult, error) {
if req.ClientID != a.clientID {
return tokenexchange.ActorResult{}, &tokenexchange.ActorDeniedError{Reason: tokenexchange.ActorDeniedReasonDenied}
}
v := a.verifier.Load()
if v == nil {
return tokenexchange.ActorResult{}, errVerifierNotBound
}
// A grant without one is a configuration fault: a denial would be final
// for the client and hide it from operators.
owner, err := identitybroker.OwnerFromSubjectIdentifier([]byte(req.Grant.SubjectIdentifier))
if err != nil {
return tokenexchange.ActorResult{}, errGrantSubjectID
}
agent, err := (*v).VerifyCredentialForOwner(ctx, identitybroker.VerifyCredentialForOwnerRequest{
Owner: owner, Credential: identitybroker.NewToken(req.ActorToken),
})
var e *identitybroker.Error
switch {
case err == nil && (agent.AgentID == "" || !strings.HasPrefix(agent.ActorID, a.actIssuer+"/")):
// AS-1 never names no agent, or one outside its act.iss: act.iss
// and the broker's trust domain disagree, a configuration fault.
return tokenexchange.ActorResult{}, errActorCheckFailed
case err == nil:
return tokenexchange.ActorResult{
AgentID: agent.ActorID,
ExpiresAt: agent.ExpiresAt,
CredentialSubject: agent.Agent.Subject,
}, nil
case errors.As(err, &e) && e.Code == identitybroker.CodeUnauthenticated && e.Reason == identitybroker.ReasonSVIDExpired:
return tokenexchange.ActorResult{}, &tokenexchange.ActorDeniedError{Reason: tokenexchange.ActorDeniedReasonActorTokenExpired}
case errors.As(err, &e) && e.Code == identitybroker.CodePermissionDenied && e.Reason == identitybroker.ReasonDenied:
return tokenexchange.ActorResult{}, &tokenexchange.ActorDeniedError{Reason: tokenexchange.ActorDeniedReasonDenied}
default:
return tokenexchange.ActorResult{}, errActorCheckFailed
}
}

// UseCredentialVerifier binds v, the identity broker's, as AS-1's check of
// the actor token of every ID-JAG request. AS-1 is built before the broker,
// so the verifier comes later; until then AS-1 answers those requests with a
// retryable server_error. It fails when the delegate client has no IDJAG
// issuance, v is nil, or a verifier is already bound. It is safe to call while
// AS-1 serves requests.
func (s *Server) UseCredentialVerifier(v identitybroker.CredentialVerifier) error {
switch {
case s.actors == nil:
return errors.New("as1server: no delegate client issues ID-JAGs")
case v == nil:
return errors.New("as1server: the credential verifier is nil")
case !s.actors.verifier.CompareAndSwap(nil, &v):
return errors.New("as1server: a credential verifier is already bound")
}
return nil
}
Loading
Loading