Skip to content

Commit 3623f09

Browse files
committed
For analytics records, upload a singular product that was used for an authorized operation. This replaces the existing functionality that picked a random matching product from the credential
1 parent b6ade2f commit 3623f09

File tree

8 files changed

+27
-15
lines changed

8 files changed

+27
-15
lines changed

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.1.2
8+
github.com/apigee/apigee-remote-service-golib/v2 v2.1.3
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.1.2 h1:9+tdJlIrpWOhGXTvYNysgG2bL4Sta93L2exiW3ChKEc=
46-
github.com/apigee/apigee-remote-service-golib/v2 v2.1.2/go.mod h1:km/iROUzLa13srZgVP0R31sAcsdgYnCJlvZdCW7ud98=
45+
github.com/apigee/apigee-remote-service-golib/v2 v2.1.3 h1:Sh4CD+w4Hgtm8njaFVyM+MMXceFvQC29xIcUHkg73Wg=
46+
github.com/apigee/apigee-remote-service-golib/v2 v2.1.3/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.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ func (a *AuthorizationServer) Check(ctx gocontext.Context, req *envoy_auth.Check
144144
if len(authorizedOps) == 0 {
145145
return a.denied(req, tracker, authContext, api), nil
146146
}
147+
authContext.AnalyticsProduct = authorizedOps[0].APIProduct
147148

148149
// apply quotas to matched operations
149150
var quotaArgs = quota.Args{QuotaAmount: 1}

server/authorization_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,13 @@ func TestImmediateAnalytics(t *testing.T) {
365365
ClientID: "client id",
366366
AccessToken: "token",
367367
Application: "app",
368-
APIProducts: []string{"product1"},
368+
APIProducts: []string{"not-accepted-product", "accepted-product"},
369369
Expires: time.Now(),
370370
DeveloperEmail: "email",
371371
Scopes: []string{"scope"},
372372
APIKey: "apikey",
373373
CustomAttributes: "{\"tier\":\"standard\"}",
374+
AnalyticsProduct: "accepted-product",
374375
}
375376
testAuthMan.sendAuth(ac, auth.ErrBadAuth)
376377

@@ -430,7 +431,7 @@ func TestImmediateAnalytics(t *testing.T) {
430431
DeveloperApp: ac.Application,
431432
AccessToken: ac.AccessToken,
432433
ClientID: ac.ClientID,
433-
APIProduct: ac.APIProducts[0],
434+
APIProduct: ac.AnalyticsProduct,
434435
Organization: server.handler.orgName,
435436
Environment: server.handler.envName,
436437
GatewaySource: gatewaySource,

server/header_context.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func makeMetadataHeaders(api string, ac *auth.Context, authorized bool) []*core.
3838
header(headerEnvironment, ac.Environment()),
3939
header(headerOrganization, ac.Organization()),
4040
header(headerScope, strings.Join(ac.Scopes, " ")),
41+
header(headerAnalyticsProduct, ac.AnalyticsProduct),
4142
}
4243
if ac.CustomAttributes != "" {
4344
headers = append(headers, header(headerCustomAttributes, ac.CustomAttributes))
@@ -87,5 +88,6 @@ func (h *Handler) decodeMetadataHeaders(headers map[string]string) (string, *aut
8788
DeveloperEmail: headers[headerDeveloperEmail],
8889
Scopes: strings.Split(headers[headerScope], " "),
8990
CustomAttributes: headers[headerCustomAttributes],
91+
AnalyticsProduct: headers[headerAnalyticsProduct],
9092
}
9193
}

server/header_context_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func TestMetadataHeaders(t *testing.T) {
4242
DeveloperEmail: "[email protected]",
4343
Scopes: []string{"scope1", "scope2"},
4444
CustomAttributes: "{\"tier\":\"standard\"}",
45+
AnalyticsProduct: "prod1",
4546
}
4647
api := "api"
4748
opts = makeMetadataHeaders(api, authContext, true)
@@ -66,6 +67,7 @@ func TestMetadataHeaders(t *testing.T) {
6667
equal(headerEnvironment, authContext.Environment())
6768
equal(headerOrganization, authContext.Organization())
6869
equal(headerScope, strings.Join(authContext.Scopes, " "))
70+
equal(headerAnalyticsProduct, authContext.AnalyticsProduct)
6971

7072
api2, ac2 := h.decodeMetadataHeaders(headers)
7173
if api != api2 {

server/metadata_context.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const (
3737
headerOrganization = "x-apigee-organization"
3838
headerScope = "x-apigee-scope"
3939
headerCustomAttributes = "x-apigee-customattributes"
40+
headerAnalyticsProduct = "x-apigee-analytics-product"
4041
)
4142

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

4950
fields := map[string]*structpb.Value{
50-
headerAccessToken: stringValueFrom(ac.AccessToken),
51-
headerAPI: stringValueFrom(api),
52-
headerAPIProducts: stringValueFrom(strings.Join(ac.APIProducts, ",")),
53-
headerApplication: stringValueFrom(ac.Application),
54-
headerClientID: stringValueFrom(ac.ClientID),
55-
headerDeveloperEmail: stringValueFrom(ac.DeveloperEmail),
56-
headerEnvironment: stringValueFrom(ac.Environment()),
57-
headerOrganization: stringValueFrom(ac.Organization()),
58-
headerScope: stringValueFrom(strings.Join(ac.Scopes, " ")),
51+
headerAccessToken: stringValueFrom(ac.AccessToken),
52+
headerAPI: stringValueFrom(api),
53+
headerAPIProducts: stringValueFrom(strings.Join(ac.APIProducts, ",")),
54+
headerApplication: stringValueFrom(ac.Application),
55+
headerClientID: stringValueFrom(ac.ClientID),
56+
headerDeveloperEmail: stringValueFrom(ac.DeveloperEmail),
57+
headerEnvironment: stringValueFrom(ac.Environment()),
58+
headerOrganization: stringValueFrom(ac.Organization()),
59+
headerScope: stringValueFrom(strings.Join(ac.Scopes, " ")),
60+
headerAnalyticsProduct: stringValueFrom((ac.AnalyticsProduct)),
5961
}
6062

6163
if ac.CustomAttributes != "" {
@@ -133,5 +135,6 @@ func (h *Handler) decodeExtAuthzMetadata(fields map[string]*structpb.Value) (str
133135
DeveloperEmail: fields[headerDeveloperEmail].GetStringValue(),
134136
Scopes: strings.Split(fields[headerScope].GetStringValue(), " "),
135137
CustomAttributes: fields[headerCustomAttributes].GetStringValue(),
138+
AnalyticsProduct: fields[headerAnalyticsProduct].GetStringValue(),
136139
}
137140
}

server/metadata_context_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestEncodeMetadata(t *testing.T) {
4141
DeveloperEmail: "[email protected]",
4242
Scopes: []string{"scope1", "scope2"},
4343
CustomAttributes: "{\"tier\":\"standard\"}",
44+
AnalyticsProduct: "prod1",
4445
}
4546
api := "api"
4647
metadata := encodeExtAuthzMetadata(api, authContext, true)
@@ -65,6 +66,7 @@ func TestEncodeMetadata(t *testing.T) {
6566
equal(headerOrganization, authContext.Organization())
6667
equal(headerScope, strings.Join(authContext.Scopes, " "))
6768
equal(headerCustomAttributes, authContext.CustomAttributes)
69+
equal(headerAnalyticsProduct, authContext.AnalyticsProduct)
6870

6971
api2, ac2 := h.decodeExtAuthzMetadata(metadata.GetFields())
7072
if api != api2 {
@@ -96,6 +98,7 @@ func TestEncodeMetadataAuthorizedField(t *testing.T) {
9698
DeveloperEmail: "[email protected]",
9799
Scopes: []string{"scope1", "scope2"},
98100
CustomAttributes: "",
101+
AnalyticsProduct: "prod1",
99102
}
100103

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

0 commit comments

Comments
 (0)