This commit is contained in:
@@ -37,3 +37,26 @@ func TestControlPlaneCredentialUsesDedicatedSecretRoot(t *testing.T) {
|
||||
t.Fatalf("control-plane credential resolution failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCandidateControlPlaneAuthenticationRejectsMissingOrWrongCredential(t *testing.T) {
|
||||
credential := func() (string, error) { return "service-token", nil }
|
||||
for _, authorization := range []string{"", "Bearer other-token", "Basic service-token"} {
|
||||
request := httptest.NewRequest(http.MethodPost, "/v1/internal/capability-bindings/candidate/resolve", nil)
|
||||
request.Header.Set("Authorization", authorization)
|
||||
if candidateControlPlaneAuthenticated(request, credential) {
|
||||
t.Fatalf("authorization %q was accepted", authorization)
|
||||
}
|
||||
}
|
||||
request := httptest.NewRequest(http.MethodPost, "/v1/internal/capability-bindings/candidate/resolve", nil)
|
||||
request.Header.Set("Authorization", "Bearer service-token")
|
||||
if !candidateControlPlaneAuthenticated(request, credential) {
|
||||
t.Fatal("expected service credential to be accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCandidateReleaseContextRequiresCompleteRuntimeMetadata(t *testing.T) {
|
||||
t.Setenv("CANDIDATE_RELEASE_SET_ID", "assistant-slot-b")
|
||||
if _, err := candidateReleaseContextFromRuntime(); err == nil {
|
||||
t.Fatal("partial candidate runtime metadata was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/subtle"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -49,6 +50,60 @@ const (
|
||||
controlPlaneServiceTokenFile = "control_plane_service_token"
|
||||
)
|
||||
|
||||
func candidateAuthorityResource(context capability.CandidateReleaseContext, origin string) authority.CandidateCapabilityBindingResource {
|
||||
digest := context.TargetDigest
|
||||
if origin != "" {
|
||||
calculated, err := capabilitydomain.CandidateTargetDigest(capability.CandidateBinding{Context: context, TargetOrigin: origin, Protocol: capability.ProtocolIdempotentReceiptV1})
|
||||
if err != nil {
|
||||
digest = ""
|
||||
} else {
|
||||
digest = calculated
|
||||
}
|
||||
}
|
||||
return authority.CandidateCapabilityBindingResource{ReleaseSetID: context.ReleaseSetID, Slot: context.Slot, CapabilityID: string(context.CapabilityID), ResourceScope: context.ResourceScope, Mode: string(context.Mode), TargetKind: context.TargetKind, TargetRef: context.TargetRef, TargetDigest: digest}
|
||||
}
|
||||
|
||||
// candidateReleaseContextFromRuntime accepts only deployment-owned metadata.
|
||||
// Candidate HTTP callers cannot choose a release identity for this runtime.
|
||||
func candidateReleaseContextFromRuntime() (*capability.CandidateReleaseContext, error) {
|
||||
const prefix = "CANDIDATE_"
|
||||
release := os.Getenv(prefix + "RELEASE_SET_ID")
|
||||
if release == "" {
|
||||
return nil, nil
|
||||
}
|
||||
context := &capability.CandidateReleaseContext{
|
||||
ReleaseSetID: release,
|
||||
Slot: os.Getenv(prefix + "SLOT"),
|
||||
TaskID: os.Getenv(prefix + "TASK_ID"),
|
||||
PrincipalID: identity.PrincipalID(os.Getenv(prefix + "PRINCIPAL_ID")),
|
||||
CapabilityID: capability.CapabilityID(os.Getenv(prefix + "CAPABILITY_ID")),
|
||||
ResourceScope: os.Getenv(prefix + "RESOURCE_SCOPE"),
|
||||
Mode: capability.CandidateBindingMode(os.Getenv(prefix + "BINDING_MODE")),
|
||||
TargetKind: os.Getenv(prefix + "TARGET_KIND"),
|
||||
TargetRef: os.Getenv(prefix + "TARGET_REF"),
|
||||
TargetDigest: os.Getenv(prefix + "TARGET_DIGEST"),
|
||||
AuthorityGrantID: os.Getenv(prefix + "AUTHORITY_GRANT_ID"),
|
||||
}
|
||||
revision, err := strconv.ParseInt(os.Getenv(prefix+"TASK_REVISION"), 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("candidate task revision: %w", err)
|
||||
}
|
||||
context.TaskRevision = revision
|
||||
if context.Slot == "" || context.TaskID == "" || context.TaskRevision < 1 || context.PrincipalID == "" || context.CapabilityID == "" || context.ResourceScope == "" || context.Mode == "" || context.TargetKind == "" || context.TargetRef == "" || context.TargetDigest == "" || context.AuthorityGrantID == "" {
|
||||
return nil, errors.New("incomplete candidate release metadata")
|
||||
}
|
||||
return context, nil
|
||||
}
|
||||
|
||||
func candidateControlPlaneAuthenticated(r *http.Request, credential func() (string, error)) bool {
|
||||
if credential == nil {
|
||||
return false
|
||||
}
|
||||
expected, err := credential()
|
||||
presented := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
||||
return err == nil && expected != "" && presented != "" && subtle.ConstantTimeCompare([]byte(expected), []byte(presented)) == 1
|
||||
}
|
||||
|
||||
const maxCreatorCredentialsBytes = 4 << 10
|
||||
|
||||
func main() {
|
||||
@@ -147,6 +202,10 @@ func main() {
|
||||
} else {
|
||||
log.Printf("delegated control-plane authority is not configured; existing human control remains available")
|
||||
}
|
||||
candidateContext, err := candidateReleaseContextFromRuntime()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
reminderClient, err := newContinuityReminderClient(envOr("CONTINUITY_RUNTIME_URL", "http://continuity-runtime:8081"), &http.Client{Timeout: 5 * time.Second})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -1499,6 +1558,61 @@ func main() {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(binding)
|
||||
})
|
||||
mux.HandleFunc("POST /v1/internal/capability-bindings/candidate", func(w http.ResponseWriter, r *http.Request) {
|
||||
if delegatedAuthority == nil || candidateContext == nil {
|
||||
http.Error(w, "delegated authority unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if !candidateControlPlaneAuthenticated(r, delegatedAuthority.credential) {
|
||||
http.Error(w, "service principal denied", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
var binding capability.CandidateBinding
|
||||
decoder := json.NewDecoder(io.LimitReader(r.Body, 16*1024+1))
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&binding); err != nil || binding.Context != *candidateContext || capabilitydomain.ValidateCandidateBinding(binding) != nil {
|
||||
http.Error(w, "invalid candidate binding", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
resource := candidateAuthorityResource(binding.Context, binding.TargetOrigin)
|
||||
request := authority.DelegatedControlPlaneRequest{GrantID: authority.GrantID(binding.Context.AuthorityGrantID), TaskID: binding.Context.TaskID, TaskRevision: binding.Context.TaskRevision, Operation: authority.DelegatedOperationCreateCandidateCapabilityBinding, CandidateResource: resource}
|
||||
if err := delegatedAuthority.authorize(r.Context(), request); err != nil || capabilityRepository.SaveCandidateBinding(r.Context(), binding) != nil {
|
||||
delegatedAuthority.completed(r.Context(), request, false)
|
||||
http.Error(w, "candidate binding denied", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
delegatedAuthority.completed(r.Context(), request, true)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
})
|
||||
mux.HandleFunc("POST /v1/internal/capability-bindings/candidate/resolve", func(w http.ResponseWriter, r *http.Request) {
|
||||
if delegatedAuthority == nil || candidateContext == nil {
|
||||
http.Error(w, "delegated authority unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if !candidateControlPlaneAuthenticated(r, delegatedAuthority.credential) {
|
||||
http.Error(w, "service principal denied", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
var context capability.CandidateReleaseContext
|
||||
if err := json.NewDecoder(io.LimitReader(r.Body, 8*1024+1)).Decode(&context); err != nil || context != *candidateContext {
|
||||
http.Error(w, "invalid candidate context", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
resource := candidateAuthorityResource(context, "")
|
||||
request := authority.DelegatedControlPlaneRequest{GrantID: authority.GrantID(context.AuthorityGrantID), TaskID: context.TaskID, TaskRevision: context.TaskRevision, Operation: authority.DelegatedOperationUseCandidateCapabilityBinding, CandidateResource: resource}
|
||||
if err := delegatedAuthority.authorize(r.Context(), request); err != nil {
|
||||
http.Error(w, "candidate binding denied", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
resolved, err := capabilityRepository.ResolveCandidateBinding(r.Context(), context, time.Now().UnixMilli())
|
||||
delegatedAuthority.completed(r.Context(), request, err == nil)
|
||||
if err != nil {
|
||||
http.Error(w, "candidate binding required", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(resolved)
|
||||
})
|
||||
mux.HandleFunc("GET /v1/cognitive-bindings/{id}/revisions/{revision}/snapshot", func(w http.ResponseWriter, r *http.Request) {
|
||||
revision, err := strconv.ParseInt(r.PathValue("revision"), 10, 64)
|
||||
if err != nil || revision < 1 {
|
||||
|
||||
Reference in New Issue
Block a user