Skip to content
Open
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
1,666 changes: 885 additions & 781 deletions api/ateapi/v1/ateapi.pb.go

Large diffs are not rendered by default.

45 changes: 42 additions & 3 deletions api/ateapi/v1/ateapi.proto
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ message EgressPolicy {
// +k8s:optional
// +k8s:maxItems=256
// +k8s:listType=atomic
// +k8s:customValidation # no two rules tie on a name and port
// +k8s:customValidation # no two rules tie on a name and port, and an https rule allows each token_url
repeated EgressRule rules = 2;
}

Expand All @@ -636,7 +636,7 @@ message EgressPolicyTemplate {
// +k8s:required
// +k8s:maxItems=256
// +k8s:listType=atomic
// +k8s:customValidation # no two rules tie on a name and port
// +k8s:customValidation # no two rules tie on a name and port, and an https rule allows each token_url
repeated EgressRule rules = 1;
}

Expand Down Expand Up @@ -839,7 +839,7 @@ message CredentialHeader {
// Source-agnostic reference interpreted by a registered credential provider:
// `ate-secret://<provider-class>/<provider-name>/<provider-specific-tail>`
//
// Exactly one of credential_uri and actor_jwt must be set.
// Exactly one of credential_uri, actor_jwt, and token_exchange must be set.
//
// +k8s:optional
// +k8s:unionMember
Expand All @@ -852,6 +852,45 @@ message CredentialHeader {
// +k8s:optional
// +k8s:unionMember
ActorJwtSource actor_jwt = 4;

// An access token obtained by exchanging an actor JWT at an OAuth 2.0
// token exchange endpoint (RFC 8693). A policy can have at most 16
// token_exchange entries.
//
// +k8s:optional
// +k8s:unionMember
TokenExchangeSource token_exchange = 5;
}

// TokenExchangeSource has the gateway mint a JWT for the Actor that sent the
// request, bound to audience, and exchange it at token_url (RFC 8693). The
// returned access token becomes the header value.
message TokenExchangeSource {
// HTTPS URL of the token endpoint, with a DNS hostname and without userinfo,
// query, or fragment. An https rule in the same policy must allow its host
// and port.
//
// +k8s:required
// +k8s:maxLength=2048
// +k8s:customValidation # format
string token_url = 1;

// Becomes the actor JWT's only aud and the request's audience parameter.
//
// +k8s:required
// +k8s:minLength=1
// +k8s:maxLength=512
string audience = 2;

// The request's scope parameter, space-joined.
//
// +k8s:optional
// +k8s:maxItems=16
// +k8s:listType=set
// +k8s:eachVal=+k8s:minLength=1
// +k8s:eachVal=+k8s:maxLength=256
// +k8s:customValidation # RFC 6749 scope-token characters
repeated string scopes = 3;
}

// ActorJwtSource has the gateway mint a JWT for the Actor that sent the
Expand Down
10 changes: 10 additions & 0 deletions cmd/ateapi/internal/apivalidation/actor_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ func TestValidateCreateActorTemplateRequest(t *testing.T) {
}}
})},
field.ErrorList{field.Invalid(field.NewPath("actor_template", "golden_egress_policy", "rules").Index(1).Child("tls_passthrough", "hostnames").Index(0), nil, "")},
}, {
"golden egress token exchange at an endpoint no rule allows",
&ateapi.CreateActorTemplateRequest{ActorTemplate: validActorTemplate(func(tmpl *ateapi.ActorTemplate) {
tmpl.GoldenEgressPolicy = &ateapi.EgressPolicyTemplate{Rules: []*ateapi.EgressRule{{Https: &ateapi.HttpsRule{
Hostnames: []string{"api.example.com"},
Ports: &ateapi.Ports{Numbers: []int32{443}},
Effects: &ateapi.HttpRuleEffects{ReplaceHeaders: []*ateapi.CredentialHeader{tokenExchangeHeader("https://sts.example.com/token")}},
}}}}
})},
field.ErrorList{field.Invalid(field.NewPath("actor_template", "golden_egress_policy", "rules").Index(0).Child("https", "effects", "replace_headers").Index(0).Child("token_exchange", "token_url"), nil, "")},
}, {
"too many golden egress rules",
&ateapi.CreateActorTemplateRequest{ActorTemplate: validActorTemplate(func(tmpl *ateapi.ActorTemplate) {
Expand Down
76 changes: 75 additions & 1 deletion cmd/ateapi/internal/apivalidation/egress_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func ValidateCustom_DeleteActorEgressPolicyRequest_Name(_ context.Context, _ ope
// ValidateCustom_EgressPolicy_Rules rejects two rules that tie on a pattern
// and a port, whatever their protocols: the gateway would have no way to pick
// one. Defaults are applied before validation, so an http rule left on 80
// and an https rule left on 443 never tie.
// and an https rule left on 443 never tie. It also rejects a token exchange
// whose token_url no https rule allows.
func ValidateCustom_EgressPolicy_Rules(_ context.Context, _ operation.Operation, p *field.Path, rules, _ []*ateapi.EgressRule) field.ErrorList {
type key struct {
pattern string
Expand Down Expand Up @@ -123,6 +124,49 @@ func ValidateCustom_EgressPolicy_Rules(_ context.Context, _ operation.Operation,
}
}
}
return append(errs, validateTokenURLsAllowed(p, rules)...)
}

// maxTokenExchanges bounds the token_exchange entries in one policy, since
// each costs a pass over every rule here.
const maxTokenExchanges = 16

func validateTokenURLsAllowed(p *field.Path, rules []*ateapi.EgressRule) field.ErrorList {
type tokenURL struct {
path *field.Path
raw string
}
var urls []tokenURL
for i, rule := range rules {
member, effects := "http", rule.GetHttp().GetEffects()
if rule.GetHttps() != nil {
member, effects = "https", rule.GetHttps().GetEffects()
}
for j, inj := range effects.GetReplaceHeaders() {
if src := inj.GetTokenExchange(); src != nil {
path := p.Index(i).Child(member, "effects", "replace_headers").Index(j).Child("token_exchange", "token_url")
urls = append(urls, tokenURL{path, src.GetTokenUrl()})
}
}
}
if len(urls) > maxTokenExchanges {
return field.ErrorList{field.Invalid(p, len(urls), fmt.Sprintf("at most %d replace_headers entries may set token_exchange", maxTokenExchanges))}
}
if len(urls) == 0 {
return nil
}
// Invalid patterns are reported by the hostname checks.
policy, _ := egresspolicy.Compile(&ateapi.EgressPolicy{Rules: rules})
var errs field.ErrorList
for _, u := range urls {
dest, ok := egresspolicy.TokenURLDestination(u.raw)
if !ok {
continue // reported by the token_url format check
}
if !policy.EvaluateRequest(dest, true).Allowed {
errs = append(errs, field.Invalid(u.path, u.raw, "host and port must be allowed by an https rule in this policy"))
}
}
return errs
}

Expand Down Expand Up @@ -254,6 +298,36 @@ func ValidateCustom_CredentialHeader_CredentialUri(_ context.Context, _ operatio
return nil
}

func ValidateCustom_TokenExchangeSource_TokenUrl(_ context.Context, _ operation.Operation, p *field.Path, raw, _ *string) field.ErrorList {
if _, ok := egresspolicy.TokenURLDestination(*raw); !ok {
return field.ErrorList{
field.Invalid(p, *raw, "must be an https URL with a DNS hostname and no userinfo, query, or fragment"),
}
}
return nil
}

func ValidateCustom_TokenExchangeSource_Scopes(_ context.Context, _ operation.Operation, p *field.Path, scopes, _ []string) field.ErrorList {
var errs field.ErrorList
for i, scope := range scopes {
if !validScopeToken(scope) {
errs = append(errs, field.Invalid(p.Index(i), scope, `must be printable ASCII without spaces, '"', or '\'`))
}
}
return errs
}

// validScopeToken reports whether scope holds only RFC 6749 scope-token
// characters, which exclude the space that joins scopes in a request.
func validScopeToken(scope string) bool {
for _, c := range []byte(scope) {
if c < 0x21 || c > 0x7e || c == '"' || c == '\\' {
return false
}
}
return true
}

func validCredentialURI(raw string) bool {
u, err := url.Parse(raw)
if err != nil || u.Scheme != "ate-secret" || u.Host == "" || u.Host != u.Hostname() || u.User != nil || u.RawQuery != "" || u.Fragment != "" || len(validation.IsDNS1123Subdomain(u.Host)) != 0 {
Expand Down
Loading
Loading