Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new header to capture customattributes #402

Merged
merged 5 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 9 additions & 8 deletions server/authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,15 @@ func TestImmediateAnalytics(t *testing.T) {

testAuthMan := &testAuthMan{}
ac := &auth.Context{
ClientID: "client id",
AccessToken: "token",
Application: "app",
APIProducts: []string{"product1"},
Expires: time.Now(),
DeveloperEmail: "email",
Scopes: []string{"scope"},
APIKey: "apikey",
ClientID: "client id",
AccessToken: "token",
Application: "app",
APIProducts: []string{"product1"},
Expires: time.Now(),
DeveloperEmail: "email",
Scopes: []string{"scope"},
APIKey: "apikey",
CustomAttributes: "{\"tier\":\"standard\"}",
}
testAuthMan.sendAuth(ac, auth.ErrBadAuth)

Expand Down
16 changes: 9 additions & 7 deletions server/header_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func makeMetadataHeaders(api string, ac *auth.Context, authorized bool) []*core.
header(headerEnvironment, ac.Environment()),
header(headerOrganization, ac.Organization()),
header(headerScope, strings.Join(ac.Scopes, " ")),
header(headerCustomAttributes, ac.CustomAttributes),
}

if authorized {
Expand Down Expand Up @@ -77,12 +78,13 @@ func (h *Handler) decodeMetadataHeaders(headers map[string]string) (string, *aut
}

return api, &auth.Context{
Context: rootContext,
AccessToken: headers[headerAccessToken],
APIProducts: strings.Split(headers[headerAPIProducts], ","),
Application: headers[headerApplication],
ClientID: headers[headerClientID],
DeveloperEmail: headers[headerDeveloperEmail],
Scopes: strings.Split(headers[headerScope], " "),
Context: rootContext,
AccessToken: headers[headerAccessToken],
APIProducts: strings.Split(headers[headerAPIProducts], ","),
Application: headers[headerApplication],
ClientID: headers[headerClientID],
DeveloperEmail: headers[headerDeveloperEmail],
Scopes: strings.Split(headers[headerScope], " "),
CustomAttributes: headers[headerCustomAttributes],
}
}
16 changes: 9 additions & 7 deletions server/header_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ func TestMetadataHeaders(t *testing.T) {
"env",
}
authContext := &auth.Context{
Context: h,
ClientID: "clientid",
AccessToken: "accesstoken",
Application: "application",
APIProducts: []string{"prod1", "prod2"},
DeveloperEmail: "dev@google.com",
Scopes: []string{"scope1", "scope2"},
Context: h,
ClientID: "clientid",
AccessToken: "accesstoken",
Application: "application",
APIProducts: []string{"prod1", "prod2"},
DeveloperEmail: "dev@google.com",
Scopes: []string{"scope1", "scope2"},
CustomAttributes: "{\"tier\":\"standard\"}",
}
api := "api"
opts = makeMetadataHeaders(api, authContext, true)
Expand All @@ -61,6 +62,7 @@ func TestMetadataHeaders(t *testing.T) {
equal(headerApplication, authContext.Application)
equal(headerClientID, authContext.ClientID)
equal(headerDeveloperEmail, authContext.DeveloperEmail)
equal(headerCustomAttributes, authContext.CustomAttributes)
equal(headerEnvironment, authContext.Environment())
equal(headerOrganization, authContext.Organization())
equal(headerScope, strings.Join(authContext.Scopes, " "))
Expand Down
55 changes: 29 additions & 26 deletions server/metadata_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ import (
const (
extAuthzFilterNamespace = "envoy.filters.http.ext_authz"

headerAuthorized = "x-apigee-authorized"
headerAccessToken = "x-apigee-accesstoken"
headerAPI = "x-apigee-api"
headerAPIProducts = "x-apigee-apiproducts"
headerApplication = "x-apigee-application"
headerClientID = "x-apigee-clientid"
headerDeveloperEmail = "x-apigee-developeremail"
headerEnvironment = "x-apigee-environment"
headerOrganization = "x-apigee-organization"
headerScope = "x-apigee-scope"
headerAuthorized = "x-apigee-authorized"
headerAccessToken = "x-apigee-accesstoken"
headerAPI = "x-apigee-api"
headerAPIProducts = "x-apigee-apiproducts"
headerApplication = "x-apigee-application"
headerClientID = "x-apigee-clientid"
headerDeveloperEmail = "x-apigee-developeremail"
headerEnvironment = "x-apigee-environment"
headerOrganization = "x-apigee-organization"
headerScope = "x-apigee-scope"
headerCustomAttributes = "x-apigee-customattributes"
)

// encodeExtAuthzMetadata encodes given api and auth context into
Expand All @@ -46,15 +47,16 @@ func encodeExtAuthzMetadata(api string, ac *auth.Context, authorized bool) *stru
}

fields := map[string]*structpb.Value{
headerAccessToken: stringValueFrom(ac.AccessToken),
headerAPI: stringValueFrom(api),
headerAPIProducts: stringValueFrom(strings.Join(ac.APIProducts, ",")),
headerApplication: stringValueFrom(ac.Application),
headerClientID: stringValueFrom(ac.ClientID),
headerDeveloperEmail: stringValueFrom(ac.DeveloperEmail),
headerEnvironment: stringValueFrom(ac.Environment()),
headerOrganization: stringValueFrom(ac.Organization()),
headerScope: stringValueFrom(strings.Join(ac.Scopes, " ")),
headerAccessToken: stringValueFrom(ac.AccessToken),
headerAPI: stringValueFrom(api),
headerAPIProducts: stringValueFrom(strings.Join(ac.APIProducts, ",")),
headerApplication: stringValueFrom(ac.Application),
headerClientID: stringValueFrom(ac.ClientID),
headerDeveloperEmail: stringValueFrom(ac.DeveloperEmail),
headerEnvironment: stringValueFrom(ac.Environment()),
headerOrganization: stringValueFrom(ac.Organization()),
headerScope: stringValueFrom(strings.Join(ac.Scopes, " ")),
headerCustomAttributes: stringValueFrom(ac.CustomAttributes),
mtalreja16 marked this conversation as resolved.
Show resolved Hide resolved
}
if authorized {
fields[headerAuthorized] = stringValueFrom("true")
Expand Down Expand Up @@ -119,12 +121,13 @@ func (h *Handler) decodeExtAuthzMetadata(fields map[string]*structpb.Value) (str
}

return api, &auth.Context{
Context: rootContext,
AccessToken: fields[headerAccessToken].GetStringValue(),
APIProducts: strings.Split(fields[headerAPIProducts].GetStringValue(), ","),
Application: fields[headerApplication].GetStringValue(),
ClientID: fields[headerClientID].GetStringValue(),
DeveloperEmail: fields[headerDeveloperEmail].GetStringValue(),
Scopes: strings.Split(fields[headerScope].GetStringValue(), " "),
Context: rootContext,
AccessToken: fields[headerAccessToken].GetStringValue(),
APIProducts: strings.Split(fields[headerAPIProducts].GetStringValue(), ","),
Application: fields[headerApplication].GetStringValue(),
ClientID: fields[headerClientID].GetStringValue(),
DeveloperEmail: fields[headerDeveloperEmail].GetStringValue(),
Scopes: strings.Split(fields[headerScope].GetStringValue(), " "),
CustomAttributes: fields[headerCustomAttributes].GetStringValue(),
}
}
31 changes: 17 additions & 14 deletions server/metadata_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ func TestEncodeMetadata(t *testing.T) {
"env",
}
authContext := &auth.Context{
Context: h,
ClientID: "clientid",
AccessToken: "accesstoken",
Application: "application",
APIProducts: []string{"prod1", "prod2"},
DeveloperEmail: "dev@google.com",
Scopes: []string{"scope1", "scope2"},
Context: h,
ClientID: "clientid",
AccessToken: "accesstoken",
Application: "application",
APIProducts: []string{"prod1", "prod2"},
DeveloperEmail: "dev@google.com",
Scopes: []string{"scope1", "scope2"},
CustomAttributes: "{\"tier\":\"standard\"}",
}
api := "api"
metadata := encodeExtAuthzMetadata(api, authContext, true)
Expand All @@ -63,6 +64,7 @@ func TestEncodeMetadata(t *testing.T) {
equal(headerEnvironment, authContext.Environment())
equal(headerOrganization, authContext.Organization())
equal(headerScope, strings.Join(authContext.Scopes, " "))
equal(headerCustomAttributes, authContext.CustomAttributes)

api2, ac2 := h.decodeExtAuthzMetadata(metadata.GetFields())
if api != api2 {
Expand All @@ -86,13 +88,14 @@ func TestEncodeMetadataAuthorizedField(t *testing.T) {
envName: "env",
}
authContext := &auth.Context{
Context: h,
ClientID: "clientid",
AccessToken: "accesstoken",
Application: "application",
APIProducts: []string{"prod1", "prod2"},
DeveloperEmail: "dev@google.com",
Scopes: []string{"scope1", "scope2"},
Context: h,
ClientID: "clientid",
AccessToken: "accesstoken",
Application: "application",
APIProducts: []string{"prod1", "prod2"},
DeveloperEmail: "dev@google.com",
Scopes: []string{"scope1", "scope2"},
CustomAttributes: "",
}

metadata := encodeExtAuthzMetadata("api", authContext, true)
Expand Down