Skip to content

Commit d966c8e

Browse files
authored
Egress policies can inject actor JWTs (agent-substrate#2141)
Part of agent-substrate#1660. - `actor_jwt` entries in `replace_headers` are now injected on `http` and `https` rules alike, like `credential_uri` entries since agent-substrate#2158, when the request carries the header, instead of being denied with 501. The gateway mints a token for the actor behind the tunnel, with the policy's audiences and lifetime, and writes the prefix plus the token into the header. - Actor JWTs need no credential provider, so a gateway without one still injects them. A `credential_uri` entry still denies the request when no provider is configured. - A failed mint denies the request: 403 if the actor was deleted, 503 if ateapi is unavailable or the mint timed out, and 500 otherwise. - The `credential_uri` path moved into its own function. Each injection failure is wrapped with its header, and the credential URI for provider entries, so the router's log line says which injection denied the request. With `--experimental-enable-authz` set on ateapi, the gateway can only mint once agent-substrate#2026 lands. Testing: unit tests cover injection with no provider, the skip when the header is absent, and each denial on both legs. The e2e check is in agent-substrate#2256, stacked on this PR. - [x] Tests pass - [ ] Appropriate changes to documentation are included in the PR (the user guide lands later in the agent-substrate#1660 series)
1 parent fd00c6c commit d966c8e

4 files changed

Lines changed: 226 additions & 99 deletions

File tree

‎cmd/atenet/internal/router/egress/credentials.go‎

Lines changed: 89 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package egress
1616

1717
import (
1818
"context"
19+
"fmt"
1920
"log/slog"
2021
"strings"
2122

@@ -49,16 +50,30 @@ func mapCredentialProviderError(err error) error {
4950
}
5051
}
5152

53+
// mapActorJWTError converts an actor JWT mint failure into a client-facing
54+
// ext_proc denial. An actor that was deleted (NotFound) denies as 403, a
55+
// transient control-plane failure as a retryable 503, and anything else, a
56+
// gateway or policy misconfiguration that retrying cannot fix, as 500.
57+
func mapActorJWTError(err error) error {
58+
switch status.Code(err) {
59+
case codes.NotFound:
60+
return extproc.WrapReqError(envoy_type.StatusCode_Forbidden, err, deniedBody)
61+
case codes.Unavailable, codes.DeadlineExceeded:
62+
return extproc.WrapReqError(envoy_type.StatusCode_ServiceUnavailable, err, deniedBody)
63+
default:
64+
return extproc.WrapReqError(envoy_type.StatusCode_InternalServerError, err, deniedBody)
65+
}
66+
}
67+
5268
// applyEffects resolves a matched rule's credential injections and returns the
5369
// header mutations to add to the request, or an error that denies it. Only a
5470
// header the request carries is replaced (headers is keyed by lowercased
5571
// name); a request without it goes out unchanged, and its credential is not
5672
// fetched.
5773
//
5874
// Any failure to produce a credential the request needs denies it, including
59-
// having no provider configured.
60-
//
61-
// This gateway cannot mint actor JWTs yet, so a request that needs one is denied.
75+
// having no provider configured for a credential_uri entry. Actor JWTs need no
76+
// provider.
6277
func (h *Handler) applyEffects(ctx context.Context, ref resources.ActorRef, dest egresspolicy.Destination, headers map[string]string, effects *ateapipb.HttpRuleEffects) ([]*corev3.HeaderValueOption, error) {
6378
var injections []*ateapipb.CredentialHeader
6479
for _, inj := range effects.GetReplaceHeaders() {
@@ -70,24 +85,6 @@ func (h *Handler) applyEffects(ctx context.Context, ref resources.ActorRef, dest
7085
return nil, nil
7186
}
7287

73-
// TODO(identity): mint actor JWTs through Control.MintActorJWT.
74-
for _, inj := range injections {
75-
if inj.GetActorJwt() != nil {
76-
slog.ErrorContext(ctx, "egress denied: this gateway cannot inject actor JWTs yet",
77-
slog.Any("actor", ref), slog.String("host", dest.Hostname), slog.String("header", inj.GetHeader()))
78-
return nil, extproc.NewReqError(envoy_type.StatusCode_NotImplemented, deniedBody)
79-
}
80-
}
81-
if h.provider == nil {
82-
slog.ErrorContext(ctx, "egress denied: policy requires credential injection but no credential provider is configured",
83-
slog.Any("actor", ref), slog.String("host", dest.Hostname))
84-
return nil, extproc.NewReqError(envoy_type.StatusCode_InternalServerError, deniedBody)
85-
}
86-
87-
// Atunnel connected to us with an ateom-for-actor SPIFFE ID; translate it
88-
// to a pure actor SPIFFE ID for plugins to make decisions on.
89-
actorSpiffeID := resources.ActorSPIFFEID(ref).String()
90-
9188
setHeaders := make([]*corev3.HeaderValueOption, 0, len(injections))
9289
for _, inj := range injections {
9390
if err := validateInjectHeader(inj.GetHeader()); err != nil {
@@ -96,43 +93,20 @@ func (h *Handler) applyEffects(ctx context.Context, ref resources.ActorRef, dest
9693
return nil, extproc.WrapReqError(envoy_type.StatusCode_InternalServerError, err, deniedBody)
9794
}
9895

99-
// Confirm the credential URI names the provider this gateway serves
100-
// before dialing: the configured connection fronts one provider, so a URI
101-
// naming another cannot be resolved here and must fail closed rather than
102-
// be sent to the wrong provider.
103-
if h.providerName != "" {
104-
name, err := providerNameFromURI(inj.GetCredentialUri())
96+
var secret []byte
97+
var err error
98+
if src := inj.GetActorJwt(); src != nil {
99+
secret, err = h.actorJWT(ctx, ref, dest, src)
105100
if err != nil {
106-
slog.ErrorContext(ctx, "egress denied: policy names an unparseable credential URI",
107-
slog.Any("actor", ref), slog.String("host", dest.Hostname), slog.String("uri", inj.GetCredentialUri()), slog.Any("err", err))
108-
return nil, extproc.WrapReqError(envoy_type.StatusCode_InternalServerError, err, deniedBody)
101+
return nil, fmt.Errorf("minting an actor JWT for header %s: %w", inj.GetHeader(), err)
109102
}
110-
if name != h.providerName {
111-
slog.ErrorContext(ctx, "egress denied: credential URI names a provider this gateway does not serve",
112-
slog.Any("actor", ref), slog.String("host", dest.Hostname), slog.String("uri", inj.GetCredentialUri()),
113-
slog.String("provider", name), slog.String("serves", h.providerName))
114-
return nil, extproc.NewReqError(envoy_type.StatusCode_InternalServerError, deniedBody)
103+
} else {
104+
secret, err = h.providerCredential(ctx, ref, dest, inj.GetCredentialUri())
105+
if err != nil {
106+
return nil, fmt.Errorf("fetching credential %s for header %s: %w", inj.GetCredentialUri(), inj.GetHeader(), err)
115107
}
116108
}
117109

118-
resp, err := h.provider.FetchSecret(ctx, &credproviderpb.FetchSecretRequest{
119-
Uri: inj.GetCredentialUri(),
120-
ActorSpiffeId: actorSpiffeID,
121-
})
122-
if err != nil {
123-
// Fail closed: a credential the policy required but we could not fetch
124-
// must not let the request out without it.
125-
slog.ErrorContext(ctx, "egress denied: credential fetch failed",
126-
slog.Any("actor", ref), slog.String("host", dest.Hostname), slog.String("uri", inj.GetCredentialUri()), slog.Any("err", err))
127-
return nil, mapCredentialProviderError(err)
128-
}
129-
secret, err := sanitizeSecret(resp.GetOpaqueBytes())
130-
if err != nil {
131-
slog.ErrorContext(ctx, "egress denied: unusable credential",
132-
slog.Any("actor", ref), slog.String("host", dest.Hostname), slog.String("uri", inj.GetCredentialUri()), slog.Any("err", err))
133-
return nil, extproc.WrapReqError(envoy_type.StatusCode_ServiceUnavailable, err, deniedBody)
134-
}
135-
136110
// Overwrite any header the actor set itself, so a client cannot pre-seed a
137111
// value that survives injection.
138112
setHeaders = append(setHeaders, &corev3.HeaderValueOption{
@@ -142,3 +116,65 @@ func (h *Handler) applyEffects(ctx context.Context, ref resources.ActorRef, dest
142116
}
143117
return setHeaders, nil
144118
}
119+
120+
// actorJWT mints the JWT src asks for. Every error it returns is already a
121+
// client-facing denial.
122+
func (h *Handler) actorJWT(ctx context.Context, ref resources.ActorRef, dest egresspolicy.Destination, src *ateapipb.ActorJWTSource) ([]byte, error) {
123+
jwt, err := h.actorJWTs.Token(ctx, ref, src)
124+
if err != nil {
125+
slog.ErrorContext(ctx, "egress denied: actor JWT mint failed",
126+
slog.Any("actor", ref), slog.String("host", dest.Hostname), slog.Any("audiences", src.GetAudiences()), slog.Any("err", err))
127+
return nil, mapActorJWTError(err)
128+
}
129+
return []byte(jwt), nil
130+
}
131+
132+
// providerCredential fetches the secret uri names from the credential
133+
// provider. Every error it returns is already a client-facing denial.
134+
func (h *Handler) providerCredential(ctx context.Context, ref resources.ActorRef, dest egresspolicy.Destination, uri string) ([]byte, error) {
135+
if h.provider == nil {
136+
slog.ErrorContext(ctx, "egress denied: policy requires credential injection but no credential provider is configured",
137+
slog.Any("actor", ref), slog.String("host", dest.Hostname))
138+
return nil, extproc.NewReqError(envoy_type.StatusCode_InternalServerError, deniedBody)
139+
}
140+
141+
// Confirm the credential URI names the provider this gateway serves
142+
// before dialing: the configured connection fronts one provider, so a URI
143+
// naming another cannot be resolved here and must fail closed rather than
144+
// be sent to the wrong provider.
145+
if h.providerName != "" {
146+
name, err := providerNameFromURI(uri)
147+
if err != nil {
148+
slog.ErrorContext(ctx, "egress denied: policy names an unparseable credential URI",
149+
slog.Any("actor", ref), slog.String("host", dest.Hostname), slog.String("uri", uri), slog.Any("err", err))
150+
return nil, extproc.WrapReqError(envoy_type.StatusCode_InternalServerError, err, deniedBody)
151+
}
152+
if name != h.providerName {
153+
slog.ErrorContext(ctx, "egress denied: credential URI names a provider this gateway does not serve",
154+
slog.Any("actor", ref), slog.String("host", dest.Hostname), slog.String("uri", uri),
155+
slog.String("provider", name), slog.String("serves", h.providerName))
156+
return nil, extproc.NewReqError(envoy_type.StatusCode_InternalServerError, deniedBody)
157+
}
158+
}
159+
160+
// Atunnel connected to us with an ateom-for-actor SPIFFE ID; translate it
161+
// to a pure actor SPIFFE ID for plugins to make decisions on.
162+
resp, err := h.provider.FetchSecret(ctx, &credproviderpb.FetchSecretRequest{
163+
Uri: uri,
164+
ActorSpiffeId: resources.ActorSPIFFEID(ref).String(),
165+
})
166+
if err != nil {
167+
// Fail closed: a credential the policy required but we could not fetch
168+
// must not let the request out without it.
169+
slog.ErrorContext(ctx, "egress denied: credential fetch failed",
170+
slog.Any("actor", ref), slog.String("host", dest.Hostname), slog.String("uri", uri), slog.Any("err", err))
171+
return nil, mapCredentialProviderError(err)
172+
}
173+
secret, err := sanitizeSecret(resp.GetOpaqueBytes())
174+
if err != nil {
175+
slog.ErrorContext(ctx, "egress denied: unusable credential",
176+
slog.Any("actor", ref), slog.String("host", dest.Hostname), slog.String("uri", uri), slog.Any("err", err))
177+
return nil, extproc.WrapReqError(envoy_type.StatusCode_ServiceUnavailable, err, deniedBody)
178+
}
179+
return secret, nil
180+
}

‎cmd/atenet/internal/router/egress/credentials_test.go‎

Lines changed: 111 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package egress
1616

1717
import (
18+
"cmp"
1819
"context"
1920
"errors"
2021
"testing"
@@ -24,6 +25,7 @@ import (
2425
"google.golang.org/grpc"
2526
"google.golang.org/grpc/codes"
2627
"google.golang.org/grpc/status"
28+
"google.golang.org/protobuf/proto"
2729

2830
"github.com/agent-substrate/substrate/cmd/atenet/internal/router/extproc"
2931
"github.com/agent-substrate/substrate/pkg/proto/ateapipb"
@@ -252,47 +254,118 @@ func TestInjectionDenials(t *testing.T) {
252254
}
253255
}
254256

255-
func TestActorJWTInjectionNotImplemented(t *testing.T) {
256-
effects := &ateapipb.HttpRuleEffects{ReplaceHeaders: []*ateapipb.CredentialHeader{{
257-
Header: "authorization",
257+
// actorJWTHeader replaces header with an actor JWT bound to api.example.com.
258+
func actorJWTHeader(header string) *ateapipb.CredentialHeader {
259+
return &ateapipb.CredentialHeader{
260+
Header: header,
258261
Prefix: "Bearer ",
259262
ActorJwt: &ateapipb.ActorJWTSource{Audiences: []string{"https://api.example.com"}, ExpirationSeconds: 900},
260-
}}}
261-
httpsPolicy := &ateapipb.EgressPolicy{Rules: []*ateapipb.EgressRule{{Https: &ateapipb.HTTPSRule{Hostnames: []string{"api.example.com"}, Effects: effects}}}}
262-
httpPolicy := &ateapipb.EgressPolicy{Rules: []*ateapipb.EgressRule{{Http: &ateapipb.HTTPRule{Hostnames: []string{"api.example.com"}, Effects: effects}}}}
263-
tests := []struct {
264-
name string
265-
leg string
266-
policy *ateapipb.EgressPolicy
267-
provider *fakeProvider // nil means no provider configured
268-
}{{
269-
name: "https rule",
270-
leg: extproc.EgressTLSMITMFilterChainName,
271-
policy: httpsPolicy,
272-
provider: &fakeProvider{resp: bearerTokenResponse("s3cr3t")},
273-
}, {
274-
name: "https rule without a provider",
275-
leg: extproc.EgressTLSMITMFilterChainName,
276-
policy: httpsPolicy,
277-
}, {
278-
name: "http rule",
279-
leg: extproc.EgressCleartextFilterChainName,
280-
policy: httpPolicy,
281-
provider: &fakeProvider{resp: bearerTokenResponse("s3cr3t")},
282-
}}
283-
for _, tc := range tests {
284-
t.Run(tc.name, func(t *testing.T) {
285-
var h *Handler
286-
if tc.provider == nil {
287-
h = injectionHandlerFor(tc.policy, nil, injectionProviderName)
288-
} else {
289-
h = injectionHandlerFor(tc.policy, tc.provider, injectionProviderName)
263+
}
264+
}
265+
266+
// replaceHeadersPolicy is a rule for api.example.com on leg's protocol (https
267+
// for the MITM leg, http for the cleartext one) that replaces headers with
268+
// entries.
269+
func replaceHeadersPolicy(leg string, entries ...*ateapipb.CredentialHeader) *ateapipb.EgressPolicy {
270+
hosts := []string{"api.example.com"}
271+
effects := &ateapipb.HttpRuleEffects{ReplaceHeaders: entries}
272+
if leg == extproc.EgressCleartextFilterChainName {
273+
return &ateapipb.EgressPolicy{Rules: []*ateapipb.EgressRule{{Http: &ateapipb.HTTPRule{Hostnames: hosts, Effects: effects}}}}
274+
}
275+
return &ateapipb.EgressPolicy{Rules: []*ateapipb.EgressRule{{Https: &ateapipb.HTTPSRule{Hostnames: hosts, Effects: effects}}}}
276+
}
277+
278+
var requestLegs = []string{extproc.EgressTLSMITMFilterChainName, extproc.EgressCleartextFilterChainName}
279+
280+
// An actor JWT is injected with no credential provider configured. The
281+
// rule's credential_uri entry is for a header the request does not carry, so
282+
// the missing provider does not deny it.
283+
func TestActorJWTInjection(t *testing.T) {
284+
for _, leg := range requestLegs {
285+
t.Run(leg, func(t *testing.T) {
286+
client := &egressMockClient{actor: runningActor(), policy: replaceHeadersPolicy(leg,
287+
actorJWTHeader("authorization"),
288+
&ateapipb.CredentialHeader{Header: "x-api-key", CredentialUri: "ate-secret://k8s/default/token"},
289+
)}
290+
h := New(client, nil, 0, nil, "")
291+
292+
res, err := h.HandleRequestHeaders(context.Background(),
293+
withAuthorization(innerMetadata(leg, "GET", "api.example.com", nil)))
294+
if err != nil {
295+
t.Fatalf("HandleRequestHeaders: %v", err)
290296
}
291-
_, err := h.HandleRequestHeaders(context.Background(),
292-
withAuthorization(innerMetadata(tc.leg, "GET", "api.example.com", nil)))
293-
wantStatus(t, err, envoy_type.StatusCode_NotImplemented)
294-
if tc.provider != nil && tc.provider.got != nil {
295-
t.Errorf("provider was asked for %v, want no fetch", tc.provider.got)
297+
setHeaders := res.Response.GetResponse().GetHeaderMutation().GetSetHeaders()
298+
if len(setHeaders) != 1 {
299+
t.Fatalf("got %d header mutations, want only the actor JWT", len(setHeaders))
300+
}
301+
h0 := setHeaders[0]
302+
if got := h0.GetHeader().GetKey(); got != "authorization" {
303+
t.Errorf("header key = %q, want authorization", got)
304+
}
305+
if got := string(h0.GetHeader().GetRawValue()); got != "Bearer jwt-1" {
306+
t.Errorf("header value = %q, want %q", got, "Bearer jwt-1")
307+
}
308+
if h0.GetAppendAction() != corev3.HeaderValueOption_OVERWRITE_IF_EXISTS_OR_ADD {
309+
t.Errorf("append action = %v, want OVERWRITE_IF_EXISTS_OR_ADD", h0.GetAppendAction())
310+
}
311+
want := &ateapipb.MintActorJWTRequest{
312+
Actor: &ateapipb.ObjectRef{Atespace: testEgressAtespace, Name: testEgressActor},
313+
Audience: []string{"https://api.example.com"},
314+
ExpirationSeconds: 900,
315+
}
316+
if got := client.lastMint.Load(); !proto.Equal(got, want) {
317+
t.Errorf("MintActorJWT request = %v, want %v", got, want)
318+
}
319+
})
320+
}
321+
}
322+
323+
func TestActorJWTInjectionSkippedWithoutHeader(t *testing.T) {
324+
client := &egressMockClient{actor: runningActor(), policy: replaceHeadersPolicy(extproc.EgressTLSMITMFilterChainName, actorJWTHeader("authorization"))}
325+
h := New(client, nil, 0, nil, "")
326+
res, err := h.HandleRequestHeaders(context.Background(),
327+
innerMetadata(extproc.EgressTLSMITMFilterChainName, "GET", "api.example.com", nil))
328+
if err != nil {
329+
t.Fatalf("HandleRequestHeaders: %v", err)
330+
}
331+
if got := res.Response.GetResponse().GetHeaderMutation().GetSetHeaders(); len(got) != 0 {
332+
t.Errorf("got %d injected headers, want 0", len(got))
333+
}
334+
if calls := client.mintCalls.Load(); calls != 0 {
335+
t.Errorf("MintActorJWT calls = %d, want 0", calls)
336+
}
337+
}
338+
339+
func TestActorJWTInjectionDenials(t *testing.T) {
340+
tests := []struct {
341+
name string
342+
header string // authorization unless set
343+
mintErr error
344+
want envoy_type.StatusCode
345+
}{
346+
{name: "actor deleted", mintErr: status.Error(codes.NotFound, "actor not found"), want: envoy_type.StatusCode_Forbidden},
347+
{name: "ateapi unavailable", mintErr: status.Error(codes.Unavailable, "ateapi is down"), want: envoy_type.StatusCode_ServiceUnavailable},
348+
{name: "mint timed out", mintErr: status.Error(codes.DeadlineExceeded, "deadline exceeded"), want: envoy_type.StatusCode_ServiceUnavailable},
349+
{name: "gateway may not mint", mintErr: status.Error(codes.PermissionDenied, "denied"), want: envoy_type.StatusCode_InternalServerError},
350+
{name: "mint request rejected", mintErr: status.Error(codes.InvalidArgument, "bad lifetime"), want: envoy_type.StatusCode_InternalServerError},
351+
{name: "system header", header: ":path", want: envoy_type.StatusCode_InternalServerError},
352+
}
353+
for _, leg := range requestLegs {
354+
t.Run(leg, func(t *testing.T) {
355+
for _, tc := range tests {
356+
t.Run(tc.name, func(t *testing.T) {
357+
header := cmp.Or(tc.header, "authorization")
358+
mock := &egressMockClient{
359+
actor: runningActor(),
360+
policy: replaceHeadersPolicy(leg, actorJWTHeader(header)),
361+
mintErr: tc.mintErr,
362+
}
363+
h := New(mock, nil, 0, nil, "")
364+
md := innerMetadata(leg, "GET", "api.example.com", nil)
365+
md.Headers[header] = "placeholder"
366+
_, err := h.HandleRequestHeaders(context.Background(), md)
367+
wantStatus(t, err, tc.want)
368+
})
296369
}
297370
})
298371
}

‎cmd/atenet/internal/router/egress/egress.go‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ type Handler struct {
8686
policies *policyCache
8787
// actorJWTs mints the actor JWTs that credential injections ask for.
8888
actorJWTs *actorjwt.Minter
89-
// provider resolves an egress policy's credential injections. Nil means
90-
// credential injection is not configured, and a request that needs one is
91-
// denied.
89+
// provider resolves an egress policy's credential_uri injections. Nil
90+
// means none are configured, and a request that needs one is denied.
9291
provider credproviderpb.CredentialProviderClient
9392
// providerName, when set, is the provider this gateway serves (the host of
9493
// its ate-secret:// credential URIs); a credential URI naming another
@@ -100,11 +99,11 @@ type Handler struct {
10099
// trusted_ca; see verifyActorCertificate for why it is checked again here.
101100
// policyCacheTTL of 0 fetches the policy on every callout.
102101
//
103-
// provider resolves an allowed rule's credential injections on the
104-
// TLS-terminated MITM leg; nil leaves credential injection off, so a request
105-
// matching a rule that requires an injection is denied. providerName, when
106-
// set, is the provider this gateway serves; a credential URI naming another
107-
// provider is refused.
102+
// provider resolves an allowed rule's credential_uri injections on the
103+
// TLS-terminated MITM leg; nil leaves them off, so a request that needs one is
104+
// denied. Actor JWT injections need no provider. providerName, when set, is
105+
// the provider this gateway serves; a credential URI naming another provider
106+
// is refused.
108107
func New(apiClient ateapipb.ControlClient, actorIdentityRoots *x509.CertPool, policyCacheTTL time.Duration, provider credproviderpb.CredentialProviderClient, providerName string) *Handler {
109108
return &Handler{
110109
apiClient: apiClient,

0 commit comments

Comments
 (0)