Skip to content

Commit

Permalink
For analytics records, upload a singular product that was used for an…
Browse files Browse the repository at this point in the history
… authorized operation. This replaces the existing functionality that picked a random matching product from the credential
  • Loading branch information
KyleWiese committed Sep 26, 2024
1 parent 0673c38 commit 7699aa1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
6 changes: 1 addition & 5 deletions analytics/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,9 @@ func (r Record) EnsureFields(authContext *auth.Context) Record {
r.ClientID = authContext.ClientID
r.Organization = authContext.Organization()
r.Environment = authContext.Environment()

r.GatewayFlowID = uuid.New().String()
r.APIProduct = authContext.AnalyticsProduct

// select arbitrary APIProduct
if len(authContext.APIProducts) > 0 {
r.APIProduct = authContext.APIProducts[0]
}
return r
}

Expand Down
21 changes: 16 additions & 5 deletions analytics/saas_analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ func TestPushAnalytics(t *testing.T) {
{
ClientReceivedStartTimestamp: ts * 1000,
ClientReceivedEndTimestamp: ts * 1000,
APIProduct: "product",
},
},
dir: uploadDir,
Expand Down Expand Up @@ -257,6 +256,7 @@ func TestPushAnalytics(t *testing.T) {
ClientReceivedStartTimestamp: ts * 1000,
ClientReceivedEndTimestamp: ts * 1000,
APIProxy: "proxy",
APIProduct: "product",
},
{
RecordType: "APIAnalytics",
Expand All @@ -280,6 +280,7 @@ func TestPushAnalytics(t *testing.T) {
ClientReceivedStartTimestamp: ts * 1000,
ClientReceivedEndTimestamp: ts * 1000,
RequestURI: "request URI",
APIProduct: "product",
},
},
dir: uploadDir,
Expand All @@ -293,7 +294,10 @@ func TestPushAnalytics(t *testing.T) {
tc := authtest.NewContext(fs.URL())
tc.SetOrganization("hi")
tc.SetEnvironment("test")
authContext := &auth.Context{Context: tc}
authContext := &auth.Context{
Context: tc,
AnalyticsProduct: "product",
}

if err := m.SendRecords(authContext, sendRecords[t1][0].records); err != nil {
t.Errorf("Error on SendRecords(): %s", err)
Expand All @@ -308,7 +312,10 @@ func TestPushAnalytics(t *testing.T) {
tc = authtest.NewContext(fs.URL())
tc.SetOrganization("otherorg")
tc.SetEnvironment("test")
authContext = &auth.Context{Context: tc}
authContext = &auth.Context{
Context: tc,
AnalyticsProduct: "product",
}
if err := m.SendRecords(authContext, sendRecords[t2][0].records); err != nil {
t.Errorf("Error on SendRecords(): %s", err)
}
Expand Down Expand Up @@ -395,7 +402,6 @@ func TestPushAnalyticsMultipleRecords(t *testing.T) {
{
ClientReceivedStartTimestamp: ts * 1000,
ClientReceivedEndTimestamp: ts * 1000,
APIProduct: "product",
},
},
dir: uploadDir,
Expand Down Expand Up @@ -424,6 +430,7 @@ func TestPushAnalyticsMultipleRecords(t *testing.T) {
ClientReceivedStartTimestamp: ts * 1000,
ClientReceivedEndTimestamp: ts * 1000,
APIProxy: "proxy",
APIProduct: "product",
},
{
RecordType: "APIAnalytics",
Expand All @@ -440,6 +447,7 @@ func TestPushAnalyticsMultipleRecords(t *testing.T) {
ClientReceivedStartTimestamp: ts * 1000,
ClientReceivedEndTimestamp: ts * 1000,
RequestURI: "request URI",
APIProduct: "product",
},
},
dir: uploadDir,
Expand All @@ -451,7 +459,10 @@ func TestPushAnalyticsMultipleRecords(t *testing.T) {
tc := authtest.NewContext(fs.URL())
tc.SetOrganization("hi")
tc.SetEnvironment("test")
authContext := &auth.Context{Context: tc}
authContext := &auth.Context{
Context: tc,
AnalyticsProduct: "product",
}

if err := m.SendRecords(authContext, sendRecords[t1][0].records); err != nil {
t.Errorf("Error on SendRecords(): %s", err)
Expand Down
1 change: 1 addition & 0 deletions auth/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Context struct {
Scopes []string
APIKey string
CustomAttributes string
AnalyticsProduct string // A single product to attatch to analytics records based on matched operations.
}

// if claims can't be processed, returns error and sets no fields
Expand Down
1 change: 1 addition & 0 deletions product/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type AuthorizedOperation struct {
QuotaLimit int64
QuotaInterval int64
QuotaTimeUnit string
APIProduct string
}

// Authorize a request against API Products and its Operations
Expand Down
2 changes: 2 additions & 0 deletions product/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func (p *APIProduct) authorize(authContext *auth.Context, api, path, method stri
QuotaLimit: p.QuotaLimitInt,
QuotaInterval: p.QuotaIntervalInt,
QuotaTimeUnit: p.QuotaTimeUnit,
APIProduct: p.Name,
}
// OperationConfig quota is an override
if oc.Quota != nil && oc.Quota.LimitInt > 0 {
Expand Down Expand Up @@ -353,6 +354,7 @@ func (p *APIProduct) authorize(authContext *auth.Context, api, path, method stri
QuotaLimit: p.QuotaLimitInt,
QuotaInterval: p.QuotaIntervalInt,
QuotaTimeUnit: p.QuotaTimeUnit,
APIProduct: p.Name,
})
hint = " authorized\n"

Expand Down
2 changes: 2 additions & 0 deletions product/product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func TestAuthorize(t *testing.T) {
wantAuthOp: &AuthorizedOperation{
ID: "Name [email protected]",
QuotaLimit: productsMap["Name 2"].QuotaLimitInt,
APIProduct: "Name 2",
},
wantHints: `Authorizing request:
environment: prod
Expand Down Expand Up @@ -255,6 +256,7 @@ func TestAuthorize(t *testing.T) {
wantAuthOp: &AuthorizedOperation{
ID: "Name [email protected]",
QuotaLimit: productsMap["Name 1"].QuotaLimitInt,
APIProduct: "Name 1",
},
wantHints: `Authorizing request:
environment: prod
Expand Down

0 comments on commit 7699aa1

Please sign in to comment.