Skip to content

Commit 4604974

Browse files
authored
Added new header to capture customattributes (#402)
1 parent c70fcad commit 4604974

7 files changed

Lines changed: 103 additions & 58 deletions

File tree

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ module github.com/apigee/apigee-remote-service-envoy/v2
22

33
go 1.16
44

5-
// replace github.com/apigee/apigee-remote-service-golib/v2 => ../apigee-remote-service-golib
5+
//replace github.com/apigee/apigee-remote-service-golib/v2 => ../apigee-remote-service-golib
66

77
require (
8-
github.com/apigee/apigee-remote-service-golib/v2 v2.0.7-0.20230308165829-ba684bc16fda
8+
github.com/apigee/apigee-remote-service-golib/v2 v2.0.7-0.20230308201312-6db69e217dea
99
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad
1010
github.com/gogo/googleapis v1.4.1
1111
github.com/golang/protobuf v1.5.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
4242
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
4343
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
4444
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
45-
github.com/apigee/apigee-remote-service-golib/v2 v2.0.7-0.20230308165829-ba684bc16fda h1:T1VfDgp3mSIXqOiLo14VdwhnwmVjV+/wGESBzr5KOGg=
46-
github.com/apigee/apigee-remote-service-golib/v2 v2.0.7-0.20230308165829-ba684bc16fda/go.mod h1:km/iROUzLa13srZgVP0R31sAcsdgYnCJlvZdCW7ud98=
45+
github.com/apigee/apigee-remote-service-golib/v2 v2.0.7-0.20230308201312-6db69e217dea h1:iwqx15jtWqCTKDjq1hKh3CRjCyl9pI7+ujQijjqXRw0=
46+
github.com/apigee/apigee-remote-service-golib/v2 v2.0.7-0.20230308201312-6db69e217dea/go.mod h1:km/iROUzLa13srZgVP0R31sAcsdgYnCJlvZdCW7ud98=
4747
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
4848
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
4949
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=

server/authorization_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,15 @@ func TestImmediateAnalytics(t *testing.T) {
362362

363363
testAuthMan := &testAuthMan{}
364364
ac := &auth.Context{
365-
ClientID: "client id",
366-
AccessToken: "token",
367-
Application: "app",
368-
APIProducts: []string{"product1"},
369-
Expires: time.Now(),
370-
DeveloperEmail: "email",
371-
Scopes: []string{"scope"},
372-
APIKey: "apikey",
365+
ClientID: "client id",
366+
AccessToken: "token",
367+
Application: "app",
368+
APIProducts: []string{"product1"},
369+
Expires: time.Now(),
370+
DeveloperEmail: "email",
371+
Scopes: []string{"scope"},
372+
APIKey: "apikey",
373+
CustomAttributes: "{\"tier\":\"standard\"}",
373374
}
374375
testAuthMan.sendAuth(ac, auth.ErrBadAuth)
375376

server/header_context.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ func makeMetadataHeaders(api string, ac *auth.Context, authorized bool) []*core.
3939
header(headerOrganization, ac.Organization()),
4040
header(headerScope, strings.Join(ac.Scopes, " ")),
4141
}
42-
42+
if ac.CustomAttributes != "" {
43+
headers = append(headers, header(headerCustomAttributes, ac.CustomAttributes))
44+
}
4345
if authorized {
4446
headers = append(headers, header(headerAuthorized, "true"))
4547
}
@@ -77,12 +79,13 @@ func (h *Handler) decodeMetadataHeaders(headers map[string]string) (string, *aut
7779
}
7880

7981
return api, &auth.Context{
80-
Context: rootContext,
81-
AccessToken: headers[headerAccessToken],
82-
APIProducts: strings.Split(headers[headerAPIProducts], ","),
83-
Application: headers[headerApplication],
84-
ClientID: headers[headerClientID],
85-
DeveloperEmail: headers[headerDeveloperEmail],
86-
Scopes: strings.Split(headers[headerScope], " "),
82+
Context: rootContext,
83+
AccessToken: headers[headerAccessToken],
84+
APIProducts: strings.Split(headers[headerAPIProducts], ","),
85+
Application: headers[headerApplication],
86+
ClientID: headers[headerClientID],
87+
DeveloperEmail: headers[headerDeveloperEmail],
88+
Scopes: strings.Split(headers[headerScope], " "),
89+
CustomAttributes: headers[headerCustomAttributes],
8790
}
8891
}

server/header_context_test.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ func TestMetadataHeaders(t *testing.T) {
3434
"env",
3535
}
3636
authContext := &auth.Context{
37-
Context: h,
38-
ClientID: "clientid",
39-
AccessToken: "accesstoken",
40-
Application: "application",
41-
APIProducts: []string{"prod1", "prod2"},
42-
DeveloperEmail: "[email protected]",
43-
Scopes: []string{"scope1", "scope2"},
37+
Context: h,
38+
ClientID: "clientid",
39+
AccessToken: "accesstoken",
40+
Application: "application",
41+
APIProducts: []string{"prod1", "prod2"},
42+
DeveloperEmail: "[email protected]",
43+
Scopes: []string{"scope1", "scope2"},
44+
CustomAttributes: "{\"tier\":\"standard\"}",
4445
}
4546
api := "api"
4647
opts = makeMetadataHeaders(api, authContext, true)
@@ -61,6 +62,7 @@ func TestMetadataHeaders(t *testing.T) {
6162
equal(headerApplication, authContext.Application)
6263
equal(headerClientID, authContext.ClientID)
6364
equal(headerDeveloperEmail, authContext.DeveloperEmail)
65+
equal(headerCustomAttributes, authContext.CustomAttributes)
6466
equal(headerEnvironment, authContext.Environment())
6567
equal(headerOrganization, authContext.Organization())
6668
equal(headerScope, strings.Join(authContext.Scopes, " "))
@@ -75,6 +77,35 @@ func TestMetadataHeaders(t *testing.T) {
7577
}
7678
}
7779

80+
func TestCustomAttributeMetadata(t *testing.T) {
81+
h := &multitenantContext{
82+
&Handler{
83+
orgName: "org",
84+
envName: "*",
85+
isMultitenant: true,
86+
},
87+
"env",
88+
}
89+
ac := &auth.Context{
90+
Context: h,
91+
ClientID: "clientid",
92+
AccessToken: "accesstoken",
93+
Application: "application",
94+
APIProducts: []string{"prod1", "prod2"},
95+
DeveloperEmail: "[email protected]",
96+
Scopes: []string{"scope1", "scope2"},
97+
}
98+
99+
// Call the function with authorized set to true
100+
headers := makeMetadataHeaders("api", ac, true)
101+
102+
// Verify that the CustomAttributes header is not included in the headers
103+
for _, h := range headers {
104+
if h.Header.Key == headerCustomAttributes {
105+
t.Errorf("Expected CustomAttributes header to not be included, but found it with value %s", h.Header.Value)
106+
}
107+
}
108+
}
78109
func TestMetadataHeadersExceptions(t *testing.T) {
79110
opts := makeMetadataHeaders("api", nil, true)
80111
if opts != nil {

server/metadata_context.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ import (
2626
const (
2727
extAuthzFilterNamespace = "envoy.filters.http.ext_authz"
2828

29-
headerAuthorized = "x-apigee-authorized"
30-
headerAccessToken = "x-apigee-accesstoken"
31-
headerAPI = "x-apigee-api"
32-
headerAPIProducts = "x-apigee-apiproducts"
33-
headerApplication = "x-apigee-application"
34-
headerClientID = "x-apigee-clientid"
35-
headerDeveloperEmail = "x-apigee-developeremail"
36-
headerEnvironment = "x-apigee-environment"
37-
headerOrganization = "x-apigee-organization"
38-
headerScope = "x-apigee-scope"
29+
headerAuthorized = "x-apigee-authorized"
30+
headerAccessToken = "x-apigee-accesstoken"
31+
headerAPI = "x-apigee-api"
32+
headerAPIProducts = "x-apigee-apiproducts"
33+
headerApplication = "x-apigee-application"
34+
headerClientID = "x-apigee-clientid"
35+
headerDeveloperEmail = "x-apigee-developeremail"
36+
headerEnvironment = "x-apigee-environment"
37+
headerOrganization = "x-apigee-organization"
38+
headerScope = "x-apigee-scope"
39+
headerCustomAttributes = "x-apigee-customattributes"
3940
)
4041

4142
// encodeExtAuthzMetadata encodes given api and auth context into
@@ -56,6 +57,11 @@ func encodeExtAuthzMetadata(api string, ac *auth.Context, authorized bool) *stru
5657
headerOrganization: stringValueFrom(ac.Organization()),
5758
headerScope: stringValueFrom(strings.Join(ac.Scopes, " ")),
5859
}
60+
61+
if ac.CustomAttributes != "" {
62+
fields[headerCustomAttributes] = stringValueFrom(ac.CustomAttributes)
63+
}
64+
5965
if authorized {
6066
fields[headerAuthorized] = stringValueFrom("true")
6167
}
@@ -119,12 +125,13 @@ func (h *Handler) decodeExtAuthzMetadata(fields map[string]*structpb.Value) (str
119125
}
120126

121127
return api, &auth.Context{
122-
Context: rootContext,
123-
AccessToken: fields[headerAccessToken].GetStringValue(),
124-
APIProducts: strings.Split(fields[headerAPIProducts].GetStringValue(), ","),
125-
Application: fields[headerApplication].GetStringValue(),
126-
ClientID: fields[headerClientID].GetStringValue(),
127-
DeveloperEmail: fields[headerDeveloperEmail].GetStringValue(),
128-
Scopes: strings.Split(fields[headerScope].GetStringValue(), " "),
128+
Context: rootContext,
129+
AccessToken: fields[headerAccessToken].GetStringValue(),
130+
APIProducts: strings.Split(fields[headerAPIProducts].GetStringValue(), ","),
131+
Application: fields[headerApplication].GetStringValue(),
132+
ClientID: fields[headerClientID].GetStringValue(),
133+
DeveloperEmail: fields[headerDeveloperEmail].GetStringValue(),
134+
Scopes: strings.Split(fields[headerScope].GetStringValue(), " "),
135+
CustomAttributes: fields[headerCustomAttributes].GetStringValue(),
129136
}
130137
}

server/metadata_context_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ func TestEncodeMetadata(t *testing.T) {
3333
"env",
3434
}
3535
authContext := &auth.Context{
36-
Context: h,
37-
ClientID: "clientid",
38-
AccessToken: "accesstoken",
39-
Application: "application",
40-
APIProducts: []string{"prod1", "prod2"},
41-
DeveloperEmail: "[email protected]",
42-
Scopes: []string{"scope1", "scope2"},
36+
Context: h,
37+
ClientID: "clientid",
38+
AccessToken: "accesstoken",
39+
Application: "application",
40+
APIProducts: []string{"prod1", "prod2"},
41+
DeveloperEmail: "[email protected]",
42+
Scopes: []string{"scope1", "scope2"},
43+
CustomAttributes: "{\"tier\":\"standard\"}",
4344
}
4445
api := "api"
4546
metadata := encodeExtAuthzMetadata(api, authContext, true)
@@ -63,6 +64,7 @@ func TestEncodeMetadata(t *testing.T) {
6364
equal(headerEnvironment, authContext.Environment())
6465
equal(headerOrganization, authContext.Organization())
6566
equal(headerScope, strings.Join(authContext.Scopes, " "))
67+
equal(headerCustomAttributes, authContext.CustomAttributes)
6668

6769
api2, ac2 := h.decodeExtAuthzMetadata(metadata.GetFields())
6870
if api != api2 {
@@ -86,13 +88,14 @@ func TestEncodeMetadataAuthorizedField(t *testing.T) {
8688
envName: "env",
8789
}
8890
authContext := &auth.Context{
89-
Context: h,
90-
ClientID: "clientid",
91-
AccessToken: "accesstoken",
92-
Application: "application",
93-
APIProducts: []string{"prod1", "prod2"},
94-
DeveloperEmail: "[email protected]",
95-
Scopes: []string{"scope1", "scope2"},
91+
Context: h,
92+
ClientID: "clientid",
93+
AccessToken: "accesstoken",
94+
Application: "application",
95+
APIProducts: []string{"prod1", "prod2"},
96+
DeveloperEmail: "[email protected]",
97+
Scopes: []string{"scope1", "scope2"},
98+
CustomAttributes: "",
9699
}
97100

98101
metadata := encodeExtAuthzMetadata("api", authContext, true)

0 commit comments

Comments
 (0)