feat(kitrun): render the OAuth scopes and omit an uncaptured primary key
{{.Scopes}} now substitutes the captured set as an ARRAY in the target
encoding, so a one-element set stays a one-element array rather than
collapsing to a bare string. An empty set REFUSES by name with an
actionable "re-run the setup terminal" message: the spec declares an
omission case for {{.PrimaryApiKey}} but none for {{.Scopes}}, and an empty
array would silently change the CLI's capability decisions.
{{.PrimaryApiKey}} now implements the spec's omit case: its enclosing key is
dropped from the structure, never emptied. archie resolves a service key
only to fill the egress grant and the container holds egress.Sentinel, so
there is never a key to render.
The renderer's input becomes OAuthFacts{Scopes, ExpiresAt} -- still the only
thing it receives about a stored token set, so a real access or refresh
token cannot reach the file and an unsourceable placeholder still refuses by
name (docs/prds/external-agent-harness.md, Verification).
This commit is contained in:
6 files changed
+274
-74
No files matched your search
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||
@@ -12,12 +11,17 @@ import (
|
||||
|
||||
// Credential-file placeholders, the vocabulary the Kit spec's
|
||||
// credentialFile.structure declares. A leaf may reference the token
|
||||
// sentinels as a whole value or inside a larger string; ExpiresAt is a
|
||||
// number and so may only be the whole value.
|
||||
// sentinels as a whole value or inside a larger string; ExpiresAt (a number)
|
||||
// and Scopes (an array) may only be the whole value, and PrimaryApiKey's
|
||||
// enclosing key is dropped rather than substituted.
|
||||
const (
|
||||
accessPlaceholder = "{{.AccessToken}}"
|
||||
refreshPlaceholder = "{{.RefreshToken}}"
|
||||
expiresPlaceholder = "{{.ExpiresAt}}"
|
||||
scopesPlaceholder = "{{.Scopes}}"
|
||||
// primaryKeyPlaceholder is the spec's omit marker, not a value to render:
|
||||
// see substitutePlaceholders.
|
||||
primaryKeyPlaceholder = "{{.PrimaryApiKey}}"
|
||||
|
||||
// credentialFileMode is owner-only: the file holds sentinels, but the
|
||||
// path is still a credential the agent alone should read.
|
||||
@@ -25,25 +29,25 @@ const (
|
||||
)
|
||||
|
||||
// renderCredentialFile renders c's oauth.credentialFile: the declared
|
||||
// structure with the Kit's sentinels substituted for the token placeholders
|
||||
// and the stored token set's expiry for {{.ExpiresAt}}, encoded in the
|
||||
// declared format. expires is the only fact about the stored token set the
|
||||
// renderer receives, so a real token cannot reach the file
|
||||
// (docs/prds/external-agent-harness.md, "Verification: the credential file
|
||||
// holds only sentinels").
|
||||
func renderCredentialFile(c spec.CredentialCapability, expires time.Time) (string, error) {
|
||||
// structure with the Kit's sentinels substituted for the token placeholders,
|
||||
// the stored token set's scopes for {{.Scopes}} and its expiry for
|
||||
// {{.ExpiresAt}}, encoded in the declared format. facts is the only thing the
|
||||
// renderer receives about the stored token set, so a real token cannot reach
|
||||
// the file (docs/prds/external-agent-harness.md, "Verification: the credential
|
||||
// file holds only sentinels").
|
||||
func renderCredentialFile(c spec.CredentialCapability, facts OAuthFacts) (string, error) {
|
||||
cf := c.OAuth.CredentialFile
|
||||
var sentinels spec.Sentinels
|
||||
if c.OAuth.Sentinels != nil {
|
||||
sentinels = *c.OAuth.Sentinels
|
||||
}
|
||||
expiresAt := int64(0)
|
||||
if !expires.IsZero() {
|
||||
if !facts.ExpiresAt.IsZero() {
|
||||
// Both in-spec OAuth credential files (Claude Code's credentials.json
|
||||
// and OpenCode's auth.json) carry the epoch in milliseconds.
|
||||
expiresAt = expires.UnixMilli()
|
||||
expiresAt = facts.ExpiresAt.UnixMilli()
|
||||
}
|
||||
structure, err := substitutePlaceholders(cf.Structure, sentinels, expiresAt)
|
||||
structure, err := substitutePlaceholders(cf.Structure, sentinels, facts.Scopes, expiresAt)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("credential %q credential file: %w", c.Service, err)
|
||||
}
|
||||
@@ -57,12 +61,20 @@ func renderCredentialFile(c spec.CredentialCapability, expires time.Time) (strin
|
||||
// substitutePlaceholders replaces every leaf that references a placeholder
|
||||
// with the typed value the target encoding expects, and refuses a placeholder
|
||||
// archie does not render rather than leaving it standing in the file.
|
||||
func substitutePlaceholders(v any, sentinels spec.Sentinels, expiresAt int64) (any, error) {
|
||||
func substitutePlaceholders(v any, sentinels spec.Sentinels, scopes []string, expiresAt int64) (any, error) {
|
||||
switch t := v.(type) {
|
||||
case map[string]any:
|
||||
out := make(map[string]any, len(t))
|
||||
for k, e := range t {
|
||||
sub, err := substitutePlaceholders(e, sentinels, expiresAt)
|
||||
// {{.PrimaryApiKey}}'s spec case is that its ENCLOSING KEY is omitted
|
||||
// when no key is captured. archie resolves a service key only to fill
|
||||
// the egress grant and the container holds egress.Sentinel, so there
|
||||
// is never a key to render here: drop the key, never empty it (absent
|
||||
// and empty are different bugs). Do not "fix" this into a value.
|
||||
if s, ok := e.(string); ok && s == primaryKeyPlaceholder {
|
||||
continue
|
||||
}
|
||||
sub, err := substitutePlaceholders(e, sentinels, scopes, expiresAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", k, err)
|
||||
}
|
||||
@@ -72,7 +84,7 @@ func substitutePlaceholders(v any, sentinels spec.Sentinels, expiresAt int64) (a
|
||||
case []any:
|
||||
out := make([]any, len(t))
|
||||
for i, e := range t {
|
||||
sub, err := substitutePlaceholders(e, sentinels, expiresAt)
|
||||
sub, err := substitutePlaceholders(e, sentinels, scopes, expiresAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
@@ -80,40 +92,29 @@ func substitutePlaceholders(v any, sentinels spec.Sentinels, expiresAt int64) (a
|
||||
}
|
||||
return out, nil
|
||||
case string:
|
||||
return substituteString(t, sentinels, expiresAt)
|
||||
return substituteString(t, sentinels, scopes, expiresAt)
|
||||
default:
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
|
||||
func substituteString(s string, sentinels spec.Sentinels, expiresAt int64) (any, error) {
|
||||
func substituteString(s string, sentinels spec.Sentinels, scopes []string, expiresAt int64) (any, error) {
|
||||
switch s {
|
||||
case accessPlaceholder:
|
||||
if sentinels.AccessToken == "" {
|
||||
return nil, fmt.Errorf("no access-token sentinel is declared for this credential")
|
||||
}
|
||||
return sentinels.AccessToken, nil
|
||||
return tokenSentinel(sentinels.AccessToken, "access-token")
|
||||
case refreshPlaceholder:
|
||||
if sentinels.RefreshToken == "" {
|
||||
return nil, fmt.Errorf("no refresh-token sentinel is declared for this credential")
|
||||
}
|
||||
return sentinels.RefreshToken, nil
|
||||
return tokenSentinel(sentinels.RefreshToken, "refresh-token")
|
||||
case expiresPlaceholder:
|
||||
return expiresAt, nil
|
||||
case scopesPlaceholder:
|
||||
return grantedScopes(scopes)
|
||||
}
|
||||
// A token sentinel may ride inside a larger string, e.g. "Bearer ...".
|
||||
out := s
|
||||
if strings.Contains(out, accessPlaceholder) {
|
||||
if sentinels.AccessToken == "" {
|
||||
return nil, fmt.Errorf("no access-token sentinel is declared for this credential")
|
||||
}
|
||||
out = strings.ReplaceAll(out, accessPlaceholder, sentinels.AccessToken)
|
||||
out, err := spliceSentinels(s, sentinels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.Contains(out, refreshPlaceholder) {
|
||||
if sentinels.RefreshToken == "" {
|
||||
return nil, fmt.Errorf("no refresh-token sentinel is declared for this credential")
|
||||
}
|
||||
out = strings.ReplaceAll(out, refreshPlaceholder, sentinels.RefreshToken)
|
||||
if err := valuePlaceholderInString(out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if at := firstPlaceholder(out); at != "" {
|
||||
return nil, fmt.Errorf("placeholder %s is not rendered by archie", at)
|
||||
@@ -121,6 +122,61 @@ func substituteString(s string, sentinels spec.Sentinels, expiresAt int64) (any,
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// tokenSentinel returns the sentinel standing in for a token, or an error when
|
||||
// the Kit declared none: a token placeholder that cannot be hidden must not
|
||||
// reach the file.
|
||||
func tokenSentinel(sentinel, name string) (string, error) {
|
||||
if sentinel == "" {
|
||||
return "", fmt.Errorf("no %s sentinel is declared for this credential", name)
|
||||
}
|
||||
return sentinel, nil
|
||||
}
|
||||
|
||||
// grantedScopes renders {{.Scopes}} as the array the target encoding expects.
|
||||
// The spec declares no omission case for it the way it does for
|
||||
// {{.PrimaryApiKey}}, so an empty set refuses rather than writing an empty
|
||||
// array, which would silently change the CLI's capability decisions.
|
||||
func grantedScopes(scopes []string) ([]string, error) {
|
||||
if len(scopes) == 0 {
|
||||
return nil, fmt.Errorf("%s: no scopes were captured for this credential; re-run the setup terminal so the provider's granted scopes are recorded", scopesPlaceholder)
|
||||
}
|
||||
return scopes, nil
|
||||
}
|
||||
|
||||
// spliceSentinels replaces a token placeholder that rides inside a larger
|
||||
// string, e.g. "Bearer {{.AccessToken}}".
|
||||
func spliceSentinels(s string, sentinels spec.Sentinels) (string, error) {
|
||||
for _, tok := range []struct{ placeholder, sentinel, name string }{
|
||||
{accessPlaceholder, sentinels.AccessToken, "access-token"},
|
||||
{refreshPlaceholder, sentinels.RefreshToken, "refresh-token"},
|
||||
} {
|
||||
if !strings.Contains(s, tok.placeholder) {
|
||||
continue
|
||||
}
|
||||
sentinel, err := tokenSentinel(tok.sentinel, tok.name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
s = strings.ReplaceAll(s, tok.placeholder, sentinel)
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// valuePlaceholderInString rejects a value placeholder left inside a larger
|
||||
// string: a number or an array renders in the encoding's own type and cannot
|
||||
// ride inside a string, and the primary key is omitted rather than rendered.
|
||||
func valuePlaceholderInString(s string) error {
|
||||
for _, whole := range []string{expiresPlaceholder, scopesPlaceholder} {
|
||||
if strings.Contains(s, whole) {
|
||||
return fmt.Errorf("%s must be a field's whole value", whole)
|
||||
}
|
||||
}
|
||||
if strings.Contains(s, primaryKeyPlaceholder) {
|
||||
return fmt.Errorf("%s is omitted, never rendered, so it cannot appear inside a larger string", primaryKeyPlaceholder)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// firstPlaceholder reports the first {{...}} occurrence in s, or "".
|
||||
func firstPlaceholder(s string) string {
|
||||
start := strings.Index(s, "{{")
|
||||
|
||||
@@ -98,7 +98,7 @@ func TestAssembleRendersTheStoredExpiry(t *testing.T) {
|
||||
expires := time.UnixMilli(1700000000123)
|
||||
l, err := Assemble(credentialFilePlan(t), image, LaunchParams{
|
||||
Execution: "exec-42", ProxyToken: "tok", CAPath: "/etc/archie/ca.pem",
|
||||
Bound: []string{"claude-code"}, OAuthExpiries: map[string]time.Time{"claude-code": expires},
|
||||
Bound: []string{"claude-code"}, OAuth: map[string]OAuthFacts{"claude-code": {ExpiresAt: expires}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -170,7 +170,7 @@ func TestAssembleRendersATOMLCredentialFile(t *testing.T) {
|
||||
// not supply must refuse the launch rather than leave the literal standing in
|
||||
// the file, which the CLI would read as a credential.
|
||||
func TestAssembleRefusesAPlaceholderItCannotRender(t *testing.T) {
|
||||
const scopesKit = sessions + `
|
||||
const unknownKit = sessions + `
|
||||
- type: com.docker.sandbox/credential@1
|
||||
config:
|
||||
service: example
|
||||
@@ -181,14 +181,14 @@ func TestAssembleRefusesAPlaceholderItCannotRender(t *testing.T) {
|
||||
credentialFile:
|
||||
path: /home/agent/credentials.json
|
||||
structure:
|
||||
scopes: "{{.Scopes}}"`
|
||||
p, err := Admit(descriptor(t, spec.KindWorkload, scopesKit))
|
||||
mystery: "{{.NotAPlaceholder}}"`
|
||||
p, err := Admit(descriptor(t, spec.KindWorkload, unknownKit))
|
||||
if err != nil {
|
||||
t.Fatalf("admit: %v", err)
|
||||
}
|
||||
_, err = Assemble(p, image, LaunchParams{Execution: "exec-42", Bound: []string{"example"}})
|
||||
if err == nil || !strings.Contains(err.Error(), "{{.Scopes}}") {
|
||||
t.Fatalf("Assemble = %v, want a refusal naming {{.Scopes}}", err)
|
||||
if err == nil || !strings.Contains(err.Error(), "{{.NotAPlaceholder}}") {
|
||||
t.Fatalf("Assemble = %v, want a refusal naming {{.NotAPlaceholder}}", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
package kit
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||
)
|
||||
|
||||
// claudeCodeCredentialFileKit mirrors the published Claude Code Kit's
|
||||
// credentialFile: the OAuth fields nested, the granted scopes among them, and
|
||||
// a top-level primaryApiKey.
|
||||
const claudeCodeCredentialFileKit = sessions + `
|
||||
- type: com.docker.sandbox/credential@1
|
||||
config:
|
||||
service: claude-code
|
||||
phase: runtime
|
||||
oauth:
|
||||
tokenEndpoint: {host: platform.claude.com, path: /v1/oauth/token}
|
||||
sentinels:
|
||||
accessToken: sk-ant-oat01-proxy-managed
|
||||
refreshToken: sk-ant-ort01-proxy-managed
|
||||
credentialFile:
|
||||
path: /home/agent/.claude/.credentials.json
|
||||
structure:
|
||||
claudeAiOauth:
|
||||
accessToken: "{{.AccessToken}}"
|
||||
refreshToken: "{{.RefreshToken}}"
|
||||
expiresAt: "{{.ExpiresAt}}"
|
||||
scopes: "{{.Scopes}}"
|
||||
primaryApiKey: "{{.PrimaryApiKey}}"`
|
||||
|
||||
// renderClaudeCredentialFile assembles the Claude-shaped Kit with facts and
|
||||
// returns the credential file's decoded top level.
|
||||
func renderClaudeCredentialFile(t *testing.T, facts OAuthFacts) map[string]any {
|
||||
t.Helper()
|
||||
p, err := Admit(descriptor(t, spec.KindWorkload, claudeCodeCredentialFileKit))
|
||||
if err != nil {
|
||||
t.Fatalf("admit: %v", err)
|
||||
}
|
||||
l, err := Assemble(p, image, LaunchParams{
|
||||
Execution: "exec-42", Bound: []string{"claude-code"},
|
||||
OAuth: map[string]OAuthFacts{"claude-code": facts},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Assemble: %v", err)
|
||||
}
|
||||
f, ok := findFile(l.Files, "/home/agent/.claude/.credentials.json")
|
||||
if !ok {
|
||||
t.Fatalf("no credential file at the declared path; files = %v", l.Files)
|
||||
}
|
||||
var doc map[string]any
|
||||
if err := json.Unmarshal([]byte(f.Content), &doc); err != nil {
|
||||
t.Fatalf("credential file is not JSON: %v\n%s", err, f.Content)
|
||||
}
|
||||
return doc
|
||||
}
|
||||
|
||||
// TestAssembleRendersTheGrantedScopes: {{.Scopes}} is an array in the target
|
||||
// encoding, so a one-element set must still be a one-element ARRAY and never
|
||||
// collapse to a bare string -- the single and multi cases differ exactly
|
||||
// there, and nowhere else.
|
||||
func TestAssembleRendersTheGrantedScopes(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
scopes []string
|
||||
}{
|
||||
{name: "a single granted scope is a one-element array", scopes: []string{"user:inference"}},
|
||||
{name: "several granted scopes are a multi-element array", scopes: []string{"user:inference", "user:profile"}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
doc := renderClaudeCredentialFile(t, OAuthFacts{Scopes: tt.scopes})
|
||||
oauth, _ := doc["claudeAiOauth"].(map[string]any)
|
||||
if oauth == nil {
|
||||
t.Fatalf("claudeAiOauth = %v, want an object", doc["claudeAiOauth"])
|
||||
}
|
||||
raw, ok := oauth["scopes"].([]any)
|
||||
if !ok {
|
||||
t.Fatalf("scopes = %#v (%T), want a JSON array", oauth["scopes"], oauth["scopes"])
|
||||
}
|
||||
if len(raw) != len(tt.scopes) {
|
||||
t.Fatalf("scopes = %v, want %v", raw, tt.scopes)
|
||||
}
|
||||
for i, want := range tt.scopes {
|
||||
if raw[i] != want {
|
||||
t.Fatalf("scopes[%d] = %v, want %q", i, raw[i], want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAssembleRefusesWhenNoScopesWereCaptured: the spec declares no omission
|
||||
// case for {{.Scopes}}, so a token set with no scopes refuses the launch with
|
||||
// something the operator can act on rather than writing an empty array, which
|
||||
// would silently change the CLI's capability decisions.
|
||||
func TestAssembleRefusesWhenNoScopesWereCaptured(t *testing.T) {
|
||||
p, err := Admit(descriptor(t, spec.KindWorkload, claudeCodeCredentialFileKit))
|
||||
if err != nil {
|
||||
t.Fatalf("admit: %v", err)
|
||||
}
|
||||
_, err = Assemble(p, image, LaunchParams{
|
||||
Execution: "exec-42", Bound: []string{"claude-code"},
|
||||
OAuth: map[string]OAuthFacts{"claude-code": {}},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("Assemble with no captured scopes = nil error, want a refusal")
|
||||
}
|
||||
for _, want := range []string{"{{.Scopes}}", "setup terminal"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Fatalf("Assemble error = %q, want it to name %q", err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestAssembleOmitsAnUncapturedPrimaryApiKey: the spec's case for
|
||||
// {{.PrimaryApiKey}} is that its enclosing key is omitted when no key is
|
||||
// captured. archie never hands the container a key -- it resolves one only to
|
||||
// fill the egress grant -- so the key must be ABSENT; absent and empty are
|
||||
// different bugs.
|
||||
func TestAssembleOmitsAnUncapturedPrimaryApiKey(t *testing.T) {
|
||||
doc := renderClaudeCredentialFile(t, OAuthFacts{Scopes: []string{"user:inference"}})
|
||||
if v, present := doc["primaryApiKey"]; present {
|
||||
t.Fatalf("primaryApiKey = %#v, want the key omitted entirely", v)
|
||||
}
|
||||
if _, ok := doc["claudeAiOauth"]; !ok {
|
||||
t.Fatalf("claudeAiOauth missing alongside primaryApiKey: %v", doc)
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,15 @@ type ImageConfig struct {
|
||||
User string
|
||||
}
|
||||
|
||||
// OAuthFacts is the non-secret part of a bound OAuth credential's stored
|
||||
// token set that a credential file renders: the scopes the provider granted
|
||||
// and when the access token expires. The tokens themselves never cross into
|
||||
// kit, so a real token cannot be written to the file.
|
||||
type OAuthFacts struct {
|
||||
Scopes []string
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
// LaunchParams are the run-specific inputs to a Kit container.
|
||||
type LaunchParams struct {
|
||||
// Execution keys the Kit's volumes, so they survive retries of one
|
||||
@@ -52,11 +61,11 @@ type LaunchParams struct {
|
||||
CAPath string
|
||||
// Bound lists the credential services the run credential carries.
|
||||
Bound []string
|
||||
// OAuthExpiries is the stored token set's expiry for each bound OAuth
|
||||
// credential whose Kit renders a credentialFile, keyed by service. It is
|
||||
// the only fact about a token set the renderer receives: the tokens
|
||||
// themselves never cross into kit, so a real token cannot be written.
|
||||
OAuthExpiries map[string]time.Time
|
||||
// OAuth is the non-secret facts of each bound OAuth credential's stored
|
||||
// token set, keyed by service, that a Kit's credentialFile renders: the
|
||||
// granted scopes and the expiry. The tokens themselves never cross into
|
||||
// kit, so a real token cannot be written.
|
||||
OAuth map[string]OAuthFacts
|
||||
}
|
||||
|
||||
// Hook is one lifecycle hook, ready to exec in the container.
|
||||
@@ -134,7 +143,7 @@ func Assemble(p *Plan, img ImageConfig, params LaunchParams) (Launch, error) {
|
||||
}
|
||||
l.Files = lifecycle.Files
|
||||
}
|
||||
credential, err := credentialFiles(creds, params.Bound, params.OAuthExpiries)
|
||||
credential, err := credentialFiles(creds, params.Bound, params.OAuth)
|
||||
if err != nil {
|
||||
return Launch{}, err
|
||||
}
|
||||
@@ -150,15 +159,15 @@ func Assemble(p *Plan, img ImageConfig, params LaunchParams) (Launch, error) {
|
||||
|
||||
// credentialFiles renders the credential file of every bound OAuth credential
|
||||
// whose Kit declares one: the path and structure the Kit names, with the
|
||||
// stored token set's expiry. A service the run credential does not carry
|
||||
// produces no file.
|
||||
func credentialFiles(creds []spec.CredentialCapability, bound []string, expiries map[string]time.Time) ([]spec.File, error) {
|
||||
// stored token set's scopes and expiry. A service the run credential does not
|
||||
// carry produces no file.
|
||||
func credentialFiles(creds []spec.CredentialCapability, bound []string, oauth map[string]OAuthFacts) ([]spec.File, error) {
|
||||
var files []spec.File
|
||||
for _, c := range creds {
|
||||
if c.OAuth == nil || c.OAuth.CredentialFile == nil || !slices.Contains(bound, c.Service) {
|
||||
continue
|
||||
}
|
||||
content, err := renderCredentialFile(c, expiries[c.Service])
|
||||
content, err := renderCredentialFile(c, oauth[c.Service])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package kitrun
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -77,24 +78,28 @@ func planCredentials(t *testing.T, p *kit.Plan) []spec.CredentialCapability {
|
||||
// real values must not appear anywhere in the rendered file.
|
||||
func TestCredentialFileNeverCarriesARealToken(t *testing.T) {
|
||||
expires := time.UnixMilli(1700000000123)
|
||||
ancestors := []string{"user:inference", "user:profile"}
|
||||
l := &Launcher{OAuth: fakeOAuthSecrets{secret: harnesssecret.Secret{
|
||||
Org: "acme", Service: "claude-code",
|
||||
AccessToken: "real-access-token", RefreshToken: "real-refresh-token",
|
||||
ExpiresAt: expires,
|
||||
ExpiresAt: expires, Scopes: ancestors,
|
||||
}}}
|
||||
p := credentialFilePlan(t)
|
||||
bound := []string{"claude-code"}
|
||||
|
||||
expiries, err := l.oauthExpiries(context.Background(), "acme", planCredentials(t, p), bound)
|
||||
facts, err := l.oauthFacts(context.Background(), "acme", planCredentials(t, p), bound)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := expiries["claude-code"]; got != expires {
|
||||
t.Fatalf("oauthExpiries[claude-code] = %v, want the stored expiry %v", got, expires)
|
||||
if got := facts["claude-code"].ExpiresAt; got != expires {
|
||||
t.Fatalf("oauthFacts[claude-code].ExpiresAt = %v, want the stored expiry %v", got, expires)
|
||||
}
|
||||
if got := facts["claude-code"].Scopes; !slices.Equal(got, ancestors) {
|
||||
t.Fatalf("oauthFacts[claude-code].Scopes = %v, want the stored scopes %v", got, ancestors)
|
||||
}
|
||||
|
||||
launch, err := kit.Assemble(p, kit.ImageConfig{User: "agent"}, kit.LaunchParams{
|
||||
Execution: "exec-1", Bound: bound, OAuthExpiries: expiries,
|
||||
Execution: "exec-1", Bound: bound, OAuth: facts,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -124,11 +129,11 @@ func TestOAuthExpiriesFailsClosed(t *testing.T) {
|
||||
bound := []string{"claude-code"}
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := (&Launcher{}).oauthExpiries(ctx, "acme", creds, bound); err == nil {
|
||||
t.Fatal("oauthExpiries with no store = nil error, want a refusal")
|
||||
if _, err := (&Launcher{}).oauthFacts(ctx, "acme", creds, bound); err == nil {
|
||||
t.Fatal("oauthFacts with no store = nil error, want a refusal")
|
||||
}
|
||||
l := &Launcher{OAuth: fakeOAuthSecrets{err: errors.New("no captured token")}}
|
||||
if _, err := l.oauthExpiries(ctx, "acme", creds, bound); err == nil {
|
||||
t.Fatal("oauthExpiries with no stored token = nil error, want a refusal")
|
||||
if _, err := l.oauthFacts(ctx, "acme", creds, bound); err == nil {
|
||||
t.Fatal("oauthFacts with no stored token = nil error, want a refusal")
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"maps"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/docker/sandbox-kit-spec/v3/fetch"
|
||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||
@@ -141,7 +140,7 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
|
||||
return nil, err
|
||||
}
|
||||
granted, bound := l.resolveCredentials(req, k.creds)
|
||||
expiries, err := l.oauthExpiries(ctx, req.Org, k.creds, bound)
|
||||
facts, err := l.oauthFacts(ctx, req.Org, k.creds, bound)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -156,7 +155,7 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
|
||||
return nil, err
|
||||
}
|
||||
run := &Run{network: "archie-kit-" + req.Execution, token: session.Token(), execution: req.Execution}
|
||||
launch, err := kit.Assemble(k.plan, k.img, kit.LaunchParams{Execution: req.Execution, ProxyToken: session.Token(), CAPath: caPath, Bound: bound, OAuthExpiries: expiries})
|
||||
launch, err := kit.Assemble(k.plan, k.img, kit.LaunchParams{Execution: req.Execution, ProxyToken: session.Token(), CAPath: caPath, Bound: bound, OAuth: facts})
|
||||
if err != nil {
|
||||
l.release(run)
|
||||
return nil, err
|
||||
@@ -234,12 +233,12 @@ func (l *Launcher) resolveCredentials(req Request, creds []spec.CredentialCapabi
|
||||
return granted, bound
|
||||
}
|
||||
|
||||
// oauthExpiries reads the stored token set's expiry for each bound OAuth
|
||||
// credential whose Kit renders a credential file. The store is read only for a
|
||||
// service the run credential carries, and only the expiry leaves here: the
|
||||
// tokens stay behind, so the renderer has no way to write one.
|
||||
func (l *Launcher) oauthExpiries(ctx context.Context, org string, creds []spec.CredentialCapability, bound []string) (map[string]time.Time, error) {
|
||||
expiries := map[string]time.Time{}
|
||||
// oauthFacts reads the stored token set's scopes and expiry for each bound
|
||||
// OAuth credential whose Kit renders a credential file. The store is read only
|
||||
// for a service the run credential carries, and only those facts leave here:
|
||||
// the tokens stay behind, so the renderer has no way to write one.
|
||||
func (l *Launcher) oauthFacts(ctx context.Context, org string, creds []spec.CredentialCapability, bound []string) (map[string]kit.OAuthFacts, error) {
|
||||
facts := map[string]kit.OAuthFacts{}
|
||||
for _, c := range creds {
|
||||
if c.OAuth == nil || c.OAuth.CredentialFile == nil || !egress.IsOAuthManaged(c) || !slices.Contains(bound, c.Service) {
|
||||
continue
|
||||
@@ -251,9 +250,9 @@ func (l *Launcher) oauthExpiries(ctx context.Context, org string, creds []spec.C
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("credential %q renders a credential file and has no captured OAuth token (run the setup terminal first): %w", c.Service, err)
|
||||
}
|
||||
expiries[c.Service] = secret.ExpiresAt
|
||||
facts[c.Service] = kit.OAuthFacts{Scopes: secret.Scopes, ExpiresAt: secret.ExpiresAt}
|
||||
}
|
||||
return expiries, nil
|
||||
return facts, nil
|
||||
}
|
||||
|
||||
// release undoes what Launch already did for run: the egress grant, then the
|
||||
|
||||
Reference in new issue
Block a user