Skip to content

Commit eea94e3

Browse files
authored
chore: update proton and remove deprecated billing account org_id usage (#1336)
1 parent 3a716fd commit eea94e3

8 files changed

Lines changed: 6141 additions & 6076 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ TAG := $(shell git rev-list --tags --max-count=1)
44
VERSION := $(shell git describe --tags ${TAG})
55
.PHONY: build check fmt lint test test-race vet test-cover-html help install proto ui compose-up-dev
66
.DEFAULT_GOAL := build
7-
PROTON_COMMIT := "4144445eb0f9cbd1a801a3d0aa5cfce4cc0ea551"
7+
PROTON_COMMIT := "b1687af73f994fa9612a023c850aa97c35735af8"
88

99
ui:
1010
@echo " > generating ui build"

‎billing/customer/service.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ func (s *Service) Update(ctx context.Context, customer Customer) (Customer, erro
167167
return Customer{}, err
168168
}
169169

170+
// Always infer org_id from existing customer (ignore from request for security)
171+
customer.OrgID = existingCustomer.OrgID
172+
170173
// update a customer in stripe
171174
stripeCustomer, err := s.stripeClient.Customers.Update(existingCustomer.ProviderID, &stripe.CustomerParams{
172175
Params: stripe.Params{
@@ -184,7 +187,7 @@ func (s *Service) Update(ctx context.Context, customer Customer) (Customer, erro
184187
Name: &customer.Name,
185188
Phone: &customer.Phone,
186189
Metadata: map[string]string{
187-
"org_id": customer.OrgID,
190+
"org_id": existingCustomer.OrgID,
188191
"managed_by": "frontier",
189192
},
190193
})

‎internal/api/v1beta1connect/billing_customer.go‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ func (h *ConnectHandler) UpdateBillingAccount(ctx context.Context, request *conn
105105
}
106106
}
107107

108+
// Ignore org_id from request - it will be inferred from billing account ID
108109
updatedCustomer, err := h.customerService.Update(ctx, customer.Customer{
109110
ID: request.Msg.GetId(),
110-
OrgID: request.Msg.GetOrgId(),
111111
Name: request.Msg.GetBody().GetName(),
112112
Email: request.Msg.GetBody().GetEmail(),
113113
Phone: request.Msg.GetBody().GetPhone(),
@@ -119,7 +119,6 @@ func (h *ConnectHandler) UpdateBillingAccount(ctx context.Context, request *conn
119119
if err != nil {
120120
errorLogger.LogServiceError(ctx, request, "UpdateBillingAccount.Update", err,
121121
zap.String("customer_id", request.Msg.GetId()),
122-
zap.String("org_id", request.Msg.GetOrgId()),
123122
zap.String("customer_name", request.Msg.GetBody().GetName()),
124123
zap.String("customer_email", request.Msg.GetBody().GetEmail()),
125124
zap.String("currency", request.Msg.GetBody().GetCurrency()))
@@ -447,14 +446,20 @@ func (h *ConnectHandler) UpdateBillingAccountDetails(ctx context.Context, reques
447446
return nil, connect.NewError(connect.CodeInternal, ErrInternalServerError)
448447
}
449448

450-
// Add audit log
451-
audit.GetAuditor(ctx, request.Msg.GetOrgId()).LogWithAttrs(audit.BillingAccountDetailsUpdatedEvent, audit.Target{
452-
ID: request.Msg.GetId(),
453-
Type: "billing_account",
454-
}, map[string]string{
455-
"credit_min": fmt.Sprintf("%d", details.CreditMin),
456-
"due_in_days": fmt.Sprintf("%d", details.DueInDays),
457-
})
449+
// Add audit log - infer org_id from billing account
450+
customerOb, err := h.customerService.GetByID(ctx, request.Msg.GetId())
451+
if err == nil {
452+
audit.GetAuditor(ctx, customerOb.OrgID).LogWithAttrs(audit.BillingAccountDetailsUpdatedEvent, audit.Target{
453+
ID: request.Msg.GetId(),
454+
Type: "billing_account",
455+
}, map[string]string{
456+
"credit_min": fmt.Sprintf("%d", details.CreditMin),
457+
"due_in_days": fmt.Sprintf("%d", details.DueInDays),
458+
})
459+
} else {
460+
errorLogger.LogServiceError(ctx, request, "UpdateBillingAccountDetails.GetByID", err,
461+
zap.String("customer_id", request.Msg.GetId()))
462+
}
458463

459464
return connect.NewResponse(&frontierv1beta1.UpdateBillingAccountDetailsResponse{}), nil
460465
}

‎internal/api/v1beta1connect/billing_customer_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ func TestHandler_UpdateBillingAccountDetails(t *testing.T) {
6767
if tt.request.Msg.GetDueInDays() >= 0 {
6868
mockCustomerService.EXPECT().UpdateDetails(mock.Anything, tt.request.Msg.GetId(), mock.Anything).
6969
Return(tt.mockUpdateDetails, tt.mockUpdateError)
70+
// Mock GetByID call used for fetching org_id for audit log (only called if UpdateDetails succeeds)
71+
if tt.mockUpdateError == nil {
72+
mockCustomerService.EXPECT().GetByID(mock.Anything, tt.request.Msg.GetId()).
73+
Return(customer.Customer{
74+
ID: tt.request.Msg.GetId(),
75+
OrgID: "test-org-id",
76+
}, nil)
77+
}
7078
}
7179

7280
handler := &ConnectHandler{

‎internal/api/v1beta1connect/v1beta1connect.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ func (h *ConnectHandler) GetOrgIDFromCheckoutID(ctx context.Context, checkoutID
147147
return customer.OrgID, nil
148148
}
149149

150+
// GetOrgIDFromBillingAccountID returns the organization ID for a given billing account
151+
func (h *ConnectHandler) GetOrgIDFromBillingAccountID(ctx context.Context, billingAccountID string) (string, error) {
152+
customer, err := h.customerService.GetByID(ctx, billingAccountID)
153+
if err != nil {
154+
return "", err
155+
}
156+
return customer.OrgID, nil
157+
}
158+
150159
func ExtractLogger(ctx context.Context) *zap.Logger {
151160
if logger, ok := ctx.Value(loggerContextKey).(*zap.Logger); ok {
152161
return logger

‎pkg/server/connect_interceptors/authorization.go‎

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -762,60 +762,71 @@ var authorizationValidationMap = map[string]func(ctx context.Context, handler *v
762762
},
763763
"/raystack.frontier.v1beta1.FrontierService/GetBillingAccount": func(ctx context.Context, handler *v1beta1connect.ConnectHandler, req connect.AnyRequest) error {
764764
pbreq := req.(*connect.Request[frontierv1beta1.GetBillingAccountRequest])
765-
if err := ensureBillingAccountBelongToOrg(ctx, handler, pbreq.Msg.GetOrgId(), pbreq.Msg.GetId()); err != nil {
765+
orgID, err := getOrgForBillingAccount(ctx, handler, pbreq.Msg.GetId())
766+
if err != nil {
766767
return err
767768
}
768-
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: pbreq.Msg.GetOrgId()}, schema.GetPermission, req)
769+
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: orgID}, schema.GetPermission, req)
769770
},
770771
"/raystack.frontier.v1beta1.FrontierService/GetBillingBalance": func(ctx context.Context, handler *v1beta1connect.ConnectHandler, req connect.AnyRequest) error {
771772
pbreq := req.(*connect.Request[frontierv1beta1.GetBillingBalanceRequest])
772-
if err := ensureBillingAccountBelongToOrg(ctx, handler, pbreq.Msg.GetOrgId(), pbreq.Msg.GetId()); err != nil {
773+
orgID, err := getOrgForBillingAccount(ctx, handler, pbreq.Msg.GetId())
774+
if err != nil {
773775
return err
774776
}
775-
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: pbreq.Msg.GetOrgId()}, schema.GetPermission, req)
777+
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: orgID}, schema.GetPermission, req)
776778
},
777779
"/raystack.frontier.v1beta1.FrontierService/CheckCreditEntitlement": func(ctx context.Context, handler *v1beta1connect.ConnectHandler, req connect.AnyRequest) error {
778780
pbreq := req.(*connect.Request[frontierv1beta1.CheckCreditEntitlementRequest])
779781
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: pbreq.Msg.GetOrgId()}, schema.GetPermission, req)
780782
},
781783
"/raystack.frontier.v1beta1.FrontierService/UpdateBillingAccount": func(ctx context.Context, handler *v1beta1connect.ConnectHandler, req connect.AnyRequest) error {
782784
pbreq := req.(*connect.Request[frontierv1beta1.UpdateBillingAccountRequest])
783-
if err := ensureBillingAccountBelongToOrg(ctx, handler, pbreq.Msg.GetOrgId(), pbreq.Msg.GetId()); err != nil {
785+
orgID, err := getOrgForBillingAccount(ctx, handler, pbreq.Msg.GetId())
786+
if err != nil {
784787
return err
785788
}
786-
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: pbreq.Msg.GetOrgId()}, schema.DeletePermission, req)
789+
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: orgID}, schema.DeletePermission, req)
787790
},
788791
"/raystack.frontier.v1beta1.FrontierService/DeleteBillingAccount": func(ctx context.Context, handler *v1beta1connect.ConnectHandler, req connect.AnyRequest) error {
789792
pbreq := req.(*connect.Request[frontierv1beta1.DeleteBillingAccountRequest])
790-
if err := ensureBillingAccountBelongToOrg(ctx, handler, pbreq.Msg.GetOrgId(), pbreq.Msg.GetId()); err != nil {
793+
orgID, err := getOrgForBillingAccount(ctx, handler, pbreq.Msg.GetId())
794+
if err != nil {
791795
return err
792796
}
793-
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: pbreq.Msg.GetOrgId()}, schema.DeletePermission, req)
797+
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: orgID}, schema.DeletePermission, req)
794798
},
795799
"/raystack.frontier.v1beta1.FrontierService/EnableBillingAccount": func(ctx context.Context, handler *v1beta1connect.ConnectHandler, req connect.AnyRequest) error {
796800
pbreq := req.(*connect.Request[frontierv1beta1.EnableBillingAccountRequest])
797-
if err := ensureBillingAccountBelongToOrg(ctx, handler, pbreq.Msg.GetOrgId(), pbreq.Msg.GetId()); err != nil {
801+
orgID, err := getOrgForBillingAccount(ctx, handler, pbreq.Msg.GetId())
802+
if err != nil {
798803
return err
799804
}
800-
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: pbreq.Msg.GetOrgId()}, schema.DeletePermission, req)
805+
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: orgID}, schema.DeletePermission, req)
801806
},
802807
"/raystack.frontier.v1beta1.FrontierService/DisableBillingAccount": func(ctx context.Context, handler *v1beta1connect.ConnectHandler, req connect.AnyRequest) error {
803808
pbreq := req.(*connect.Request[frontierv1beta1.DisableBillingAccountRequest])
804-
if err := ensureBillingAccountBelongToOrg(ctx, handler, pbreq.Msg.GetOrgId(), pbreq.Msg.GetId()); err != nil {
809+
orgID, err := getOrgForBillingAccount(ctx, handler, pbreq.Msg.GetId())
810+
if err != nil {
805811
return err
806812
}
807-
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: pbreq.Msg.GetOrgId()}, schema.DeletePermission, req)
813+
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: orgID}, schema.DeletePermission, req)
808814
},
809815
"/raystack.frontier.v1beta1.FrontierService/HasTrialed": func(ctx context.Context, handler *v1beta1connect.ConnectHandler, req connect.AnyRequest) error {
810816
pbreq := req.(*connect.Request[frontierv1beta1.HasTrialedRequest])
811-
if err := ensureBillingAccountBelongToOrg(ctx, handler, pbreq.Msg.GetOrgId(), pbreq.Msg.GetId()); err != nil {
817+
orgID, err := getOrgForBillingAccount(ctx, handler, pbreq.Msg.GetId())
818+
if err != nil {
812819
return err
813820
}
814-
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: pbreq.Msg.GetOrgId()}, schema.GetPermission, req)
821+
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: orgID}, schema.GetPermission, req)
815822
},
816823
"/raystack.frontier.v1beta1.FrontierService/RegisterBillingAccount": func(ctx context.Context, handler *v1beta1connect.ConnectHandler, req connect.AnyRequest) error {
817824
pbreq := req.(*connect.Request[frontierv1beta1.RegisterBillingAccountRequest])
818-
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: pbreq.Msg.GetOrgId()}, schema.DeletePermission, req)
825+
orgID, err := getOrgForBillingAccount(ctx, handler, pbreq.Msg.GetId())
826+
if err != nil {
827+
return err
828+
}
829+
return handler.IsAuthorized(ctx, relation.Object{Namespace: schema.OrganizationNamespace, ID: orgID}, schema.DeletePermission, req)
819830
},
820831

821832
// subscriptions - org_id and billing_id are now inferred from subscription_id
@@ -1052,14 +1063,16 @@ var authorizationValidationMap = map[string]func(ctx context.Context, handler *v
10521063
},
10531064
"/raystack.frontier.v1beta1.AdminService/GetBillingAccountDetails": func(ctx context.Context, handler *v1beta1connect.ConnectHandler, req connect.AnyRequest) error {
10541065
pbReq := req.(*connect.Request[frontierv1beta1.GetBillingAccountDetailsRequest])
1055-
if err := ensureBillingAccountBelongToOrg(ctx, handler, pbReq.Msg.GetOrgId(), pbReq.Msg.GetId()); err != nil {
1066+
_, err := getOrgForBillingAccount(ctx, handler, pbReq.Msg.GetId())
1067+
if err != nil {
10561068
return err
10571069
}
10581070
return handler.IsSuperUser(ctx, req)
10591071
},
10601072
"/raystack.frontier.v1beta1.AdminService/UpdateBillingAccountDetails": func(ctx context.Context, handler *v1beta1connect.ConnectHandler, req connect.AnyRequest) error {
10611073
pbReq := req.(*connect.Request[frontierv1beta1.UpdateBillingAccountDetailsRequest])
1062-
if err := ensureBillingAccountBelongToOrg(ctx, handler, pbReq.Msg.GetOrgId(), pbReq.Msg.GetId()); err != nil {
1074+
_, err := getOrgForBillingAccount(ctx, handler, pbReq.Msg.GetId())
1075+
if err != nil {
10631076
return err
10641077
}
10651078
return handler.IsSuperUser(ctx, req)
@@ -1122,18 +1135,14 @@ func ensureRoleBelongToOrg(ctx context.Context, handler *v1beta1connect.ConnectH
11221135
return nil
11231136
}
11241137

1125-
func ensureBillingAccountBelongToOrg(ctx context.Context, handler *v1beta1connect.ConnectHandler, orgID, billingID string) error {
1126-
acc, err := handler.GetBillingAccount(ctx, connect.NewRequest(&frontierv1beta1.GetBillingAccountRequest{
1127-
OrgId: orgID,
1128-
Id: billingID,
1129-
}))
1138+
func getOrgForBillingAccount(ctx context.Context, handler *v1beta1connect.ConnectHandler, billingID string) (string, error) {
1139+
// Infer org_id from billing_id (don't trust org_id from request for security)
1140+
orgID, err := handler.GetOrgIDFromBillingAccountID(ctx, billingID)
11301141
if err != nil {
1131-
return err
1132-
}
1133-
if acc.Msg.GetBillingAccount().GetOrgId() != orgID {
1134-
return ErrDeniedInvalidArgs
1142+
return "", err
11351143
}
1136-
return nil
1144+
// Return the inferred org_id for authorization check
1145+
return orgID, nil
11371146
}
11381147

11391148
func ensureSubscriptionBelongToOrg(ctx context.Context, handler *v1beta1connect.ConnectHandler, subID string) (string, error) {

‎proto/apidocs.swagger.yaml‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5547,6 +5547,7 @@ paths:
55475547
$ref: '#/definitions/googlerpcStatus'
55485548
parameters:
55495549
- name: org_id
5550+
description: 'DEPRECATED: org_id will be inferred from billing account id'
55505551
in: path
55515552
required: true
55525553
type: string
@@ -5607,6 +5608,7 @@ paths:
56075608
$ref: '#/definitions/googlerpcStatus'
56085609
parameters:
56095610
- name: org_id
5611+
description: 'DEPRECATED: org_id will be inferred from billing account id'
56105612
in: path
56115613
required: true
56125614
type: string
@@ -5652,6 +5654,7 @@ paths:
56525654
$ref: '#/definitions/googlerpcStatus'
56535655
parameters:
56545656
- name: org_id
5657+
description: 'DEPRECATED: org_id will be inferred from billing account id'
56555658
in: path
56565659
required: true
56575660
type: string
@@ -5707,6 +5710,7 @@ paths:
57075710
$ref: '#/definitions/googlerpcStatus'
57085711
parameters:
57095712
- name: org_id
5713+
description: 'DEPRECATED: org_id will be inferred from billing account id'
57105714
in: path
57115715
required: true
57125716
type: string
@@ -5753,6 +5757,7 @@ paths:
57535757
$ref: '#/definitions/googlerpcStatus'
57545758
parameters:
57555759
- name: org_id
5760+
description: 'DEPRECATED: org_id will be inferred from billing account id'
57565761
in: path
57575762
required: true
57585763
type: string
@@ -5804,6 +5809,7 @@ paths:
58045809
$ref: '#/definitions/googlerpcStatus'
58055810
parameters:
58065811
- name: org_id
5812+
description: 'DEPRECATED: org_id will be inferred from billing account id'
58075813
in: path
58085814
required: true
58095815
type: string
@@ -5855,11 +5861,15 @@ paths:
58555861
$ref: '#/definitions/googlerpcStatus'
58565862
parameters:
58575863
- name: org_id
5864+
description: 'DEPRECATED: org_id will be inferred from plan_id'
58585865
in: path
58595866
required: true
58605867
type: string
58615868
- name: id
5862-
description: ID of the billing account to check
5869+
description: |-
5870+
ID of the billing account to check
5871+
5872+
DEPRECATED: billing_id will be inferred from plan_id
58635873
in: path
58645874
required: true
58655875
type: string
@@ -5906,6 +5916,7 @@ paths:
59065916
$ref: '#/definitions/googlerpcStatus'
59075917
parameters:
59085918
- name: org_id
5919+
description: 'DEPRECATED: org_id will be inferred from billing account id'
59095920
in: path
59105921
required: true
59115922
type: string

0 commit comments

Comments
 (0)