Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit 7476140

Browse files
astefanovarajkiranrbala
authored andcommitted
Move authorization before searching db in Reporting (#579)
* Add recourse_id to resourceInstanceUsage params * Adapt doc
1 parent d2df603 commit 7476140

File tree

3 files changed

+64
-51
lines changed

3 files changed

+64
-51
lines changed

doc/api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2195,7 +2195,7 @@ The _resource instance usage summary report_ API can be used to retrieve aggrega
21952195
### Method: get
21962196
_HTTP request_:
21972197
```
2198-
GET /v1/metering/organizations/:organization_id/resource_instances/:resource_instance_id/consumers/:consumer_id/plans/:plan_id/metering_plans/:metering_plan_id/rating_plans/:rating_plan_id/pricing_plans/:pricing_plan_id/t/:t/aggregated/usage/:time
2198+
GET /v1/metering/organizations/:organization_id/spaces/:space_id/resource_id/:resource_id/resource_instances/:resource_instance_id/consumers/:consumer_id/plans/:plan_id/metering_plans/:metering_plan_id/rating_plans/:rating_plan_id/pricing_plans/:pricing_plan_id/t/:t/aggregated/usage/:time
21992199
```
22002200

22012201
_Description_: Retrieves a usage report document containing a summary of the aggregated Cloud resource usage incurred by the specified resource instance within an organization and the specific set of plans at the specified time.

lib/aggregation/reporting/src/index.js

+30-22
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,9 @@ const orgsUsage = function *(orgids, time, auth) {
542542

543543
// Return the usage for a resource instance for a particular plan in a given
544544
// organization, consumer, time period
545-
const resourceInstanceUsage = function *(orgid, spaceid, resid, conid, planid,
546-
mplanid, rplanid, pplanid, t, time, auth) {
545+
const resourceInstanceUsage = function *(orgid, spaceid, resourceInstanceId,
546+
consumerId, planid, meteringPlanId, ratingPlanId, pricingPlanId,
547+
timeBasedKeySegment, time, auth) {
547548
// Forward authorization header field to account to authorize
548549
const o = auth ? { headers: { authorization: auth } } : {};
549550

@@ -563,13 +564,18 @@ const resourceInstanceUsage = function *(orgid, spaceid, resid, conid, planid,
563564
throw res;
564565
}
565566

566-
const id = ['k', orgid, resid, conid, planid, mplanid, rplanid, pplanid, 't',
567-
t].join('/');
567+
const id = ['k', orgid, resourceInstanceId, consumerId, planid,
568+
meteringPlanId, ratingPlanId, pricingPlanId,
569+
't', timeBasedKeySegment].join('/');
570+
571+
572+
debug('Adiii %s', id);
568573

569574
const doc = yield accumulatordb.get(id);
570575

571576
if(!doc) {
572-
debug('No resource instance usage found for %s on %s', resid, time);
577+
debug('No resource instance usage found for %s on %s',
578+
resourceInstanceId, time);
573579

574580
// Return an empty usage report if no usage was found
575581
return {};
@@ -751,14 +757,14 @@ const runQuery = function *(query) {
751757
};
752758

753759
// Return OAuth system scopes needed to retrieve org usage
754-
const sysScopes = (doc) => secured() ? {
760+
const sysScopes = () => secured() ? {
755761
system: ['abacus.usage.read']
756762
} : undefined;
757763

758764
// Return OAuth resource or system scopes needed to retrieve resource instance
759765
// usage
760-
const scopes = (doc) => secured() ? {
761-
resource: [['abacus.usage', doc.resource_id, 'read'].join('.')],
766+
const scopes = (resourceId) => secured() ? {
767+
resource: [['abacus.usage', resourceId, 'read'].join('.')],
762768
system: ['abacus.usage.read']
763769
} : undefined;
764770

@@ -767,14 +773,14 @@ const retrieveUsage = function *(req) {
767773
debug('Retrieving rated usage for organization %s on %s',
768774
req.params.organization_id, req.params.time);
769775

776+
if (secured())
777+
oauth.authorize(req.headers && req.headers.authorization, sysScopes());
778+
770779
// Retrieve and return the rated usage for the given org and time
771780
const doc = yield orgUsage(req.params.organization_id,
772781
req.params.time ? parseInt(req.params.time) : undefined,
773782
req.headers && req.headers.authorization);
774783

775-
if (secured())
776-
oauth.authorize(req.headers && req.headers.authorization, sysScopes(doc));
777-
778784
return {
779785
body: omit(dbclient.undbify(doc),
780786
['last_rated_usage_id', 'aggregated_usage_id',
@@ -784,22 +790,23 @@ const retrieveUsage = function *(req) {
784790
};
785791

786792
// Retrieve a usage report summary for a resource instance given the
787-
// org, resource instance, consumer, plan, metering plan,
788-
// rating plan, pricing plan, time
793+
// org, space, resource instance, consumer, plan, metering plan,
794+
// rating plan, pricing plan, t, time
789795
const retrieveResourceInstanceUsage = function *(req) {
790796
debug('Retrieving rated usage for resource instance %s on %s',
791797
req.params.resource_instance_id, req.params.time);
792798

799+
if (secured())
800+
oauth.authorize(req.headers && req.headers.authorization,
801+
scopes(req.params.resource_id));
802+
793803
const doc = yield resourceInstanceUsage(req.params.organization_id,
794804
req.params.space_id, req.params.resource_instance_id,
795805
req.params.consumer_id, req.params.plan_id, req.params.metering_plan_id,
796806
req.params.rating_plan_id, req.params.pricing_plan_id, req.params.t,
797807
req.params.time ? parseInt(req.params.time) : undefined,
798808
req.headers && req.headers.authorization);
799809

800-
if (secured())
801-
oauth.authorize(req.headers && req.headers.authorization, scopes(doc));
802-
803810
return {
804811
body: omit(dbclient.undbify(doc),
805812
['last_rated_usage_id', 'aggregated_usage_id', 'accumulated_usage_id'])
@@ -820,9 +827,10 @@ routes.get(
820827

821828
routes.get(
822829
'/v1/metering/organizations/:organization_id/spaces/:space_id/' +
823-
'resource_instances/:resource_instance_id/consumers/:consumer_id/plans/' +
824-
':plan_id/metering_plans/:metering_plan_id/rating_plans/:rating_plan_id/' +
825-
'pricing_plans/:pricing_plan_id/t/:t/aggregated/usage/:time',
830+
'resource_id/:resource_id/resource_instances/:resource_instance_id/' +
831+
'consumers/:consumer_id/plans/:plan_id/metering_plans/:metering_plan_id/' +
832+
'rating_plans/:rating_plan_id/pricing_plans/:pricing_plan_id/' +
833+
't/:t/aggregated/usage/:time',
826834
throttle(retrieveResourceInstanceUsage));
827835

828836
// Retrieve a usage summary using a GraphQL query
@@ -831,6 +839,9 @@ routes.get(
831839
debug(
832840
'Retrieving rated usage using graphql query %s', req.params.query);
833841

842+
if (secured())
843+
oauth.authorize(req.headers && req.headers.authorization, sysScopes());
844+
834845
const q = req.headers && req.headers.authorization ?
835846
req.params.query.replace(/(.*)\((.*)/,
836847
'$1(authorization: "' + req.headers.authorization + '", $2') :
@@ -853,9 +864,6 @@ routes.get(
853864
});
854865
}
855866

856-
if (secured())
857-
oauth.authorize(req.headers && req.headers.authorization, sysScopes(doc));
858-
859867
return {
860868
body: omit(dbclient.undbify(doc.data),
861869
['last_rated_usage_id', 'aggregated_usage_id', 'accumulated_usage_id',

0 commit comments

Comments
 (0)