@@ -16,6 +16,7 @@ package egress
1616
1717import (
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.
6277func (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+ }
0 commit comments