Skip to content
This repository was archived by the owner on Dec 6, 2025. It is now read-only.

Commit 2fcd785

Browse files
authored
Let GetUserID and GetOrganizationID panic (#878)
1 parent baffb92 commit 2fcd785

File tree

23 files changed

+116
-280
lines changed

23 files changed

+116
-280
lines changed

libs/common/auth/auth.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,22 +238,44 @@ func SessionValidUntil(ctx context.Context) (time.Time, error) {
238238
}
239239
}
240240

241-
func GetUserID(ctx context.Context) (uuid.UUID, error) {
241+
// MaybeGetUserID can be used instead of MustGetUserID
242+
func MaybeGetUserID(ctx context.Context) *uuid.UUID {
242243
res, ok := ctx.Value(userIDKey{}).(uuid.UUID)
243244
if !ok {
244-
return uuid.UUID{}, status.Error(codes.Internal, "userID not in context, set up auth")
245-
} else {
246-
return res, nil
245+
return nil
246+
}
247+
return &res
248+
}
249+
250+
// MustGetUserID panics, if context does not contain the userIDKey,
251+
// which should have been set in the auth middleware
252+
// Also see MaybeGetUserID, if you can not ensure that
253+
func MustGetUserID(ctx context.Context) uuid.UUID {
254+
res := MaybeGetUserID(ctx)
255+
if res == nil {
256+
panic("MustGetUserID called, but userID not in context, set up auth for this handler!")
247257
}
258+
return *res
248259
}
249260

250-
func GetOrganizationID(ctx context.Context) (uuid.UUID, error) {
261+
// MaybeGetOrganizationID can be used instead of MustGetOrganizationID
262+
func MaybeGetOrganizationID(ctx context.Context) *uuid.UUID {
251263
res, ok := ctx.Value(organizationIDKey{}).(uuid.UUID)
252264
if !ok {
253-
return uuid.UUID{}, status.Error(codes.Internal, "organizationID not in context, set up auth")
254-
} else {
255-
return res, nil
265+
return nil
266+
}
267+
return &res
268+
}
269+
270+
// MustGetOrganizationID panics, if context does not contain the organizationIDKey,
271+
// which should have been set in the auth middleware
272+
// Also see MaybeGetOrganizationID, if you can not ensure that
273+
func MustGetOrganizationID(ctx context.Context) uuid.UUID {
274+
res := MaybeGetOrganizationID(ctx)
275+
if res == nil {
276+
panic("MustGetOrganizationID called, but organizationID not in context, set up auth for this handler!")
256277
}
278+
return *res
257279
}
258280

259281
// SetupAuth sets up auth, such that GetIDTokenVerifier and GetOAuthConfig work

libs/common/hwgrpc/auth_interceptor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func authInterceptor(ctx context.Context) (context.Context, error) {
9292
return nil, status.Errorf(codes.Internal, "invalid userID")
9393
}
9494

95-
// attach userID to the context, so we can get it in a handler using GetUserID()
95+
// attach userID to the context, so we can get it in a handler using MustGetUserID()
9696
ctx = auth.ContextWithUserID(ctx, userID)
9797

9898
// attach userID to the current span (should be the auth interceptor span)

libs/common/hwgrpc/organization_interceptor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func organizationInterceptor(ctx context.Context) (context.Context, error) {
7070
return nil, status.Errorf(codes.Internal, "invalid organizationID")
7171
}
7272

73-
// attach organizationID to the context, so we can get it in a handler using GetOrganizationID()
73+
// attach organizationID to the context, so we can get it in a handler using MustGetOrganizationID()
7474
ctx = auth.ContextWithOrganizationID(ctx, organizationID)
7575

7676
// attach organizationID to the current span

libs/hwes/event.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -260,29 +260,29 @@ func (e *Event) GetJsonData(data interface{}) error {
260260
return json.Unmarshal(e.Data, data)
261261
}
262262

263-
// SetCommitterFromCtx injects the UserID from the passed context via common.GetUserID().
263+
// SetCommitterFromCtx injects the UserID from the passed context via auth.MustGetUserID().
264264
func (e *Event) SetCommitterFromCtx(ctx context.Context) error {
265-
userID, err := auth.GetUserID(ctx)
266-
if err != nil {
265+
userID := auth.MaybeGetUserID(ctx)
266+
if userID == nil {
267267
// don't set a user, if no user is available
268-
return nil //nolint:nilerr
268+
return nil
269269
}
270270

271-
e.CommitterUserID = &userID
271+
e.CommitterUserID = userID
272272

273273
telemetry.SetSpanStr(ctx, "committerUserID", e.CommitterUserID.String())
274274
return nil
275275
}
276276

277-
// SetOrganizationFromCtx injects the OrganizationID from the passed context via common.GetOrganizationID().
277+
// SetOrganizationFromCtx injects the OrganizationID from the passed context via common.MustGetOrganizationID().
278278
func (e *Event) SetOrganizationFromCtx(ctx context.Context) error {
279-
organizationID, err := auth.GetOrganizationID(ctx)
280-
if err != nil {
279+
organizationID := auth.MaybeGetOrganizationID(ctx)
280+
if organizationID == nil {
281281
// don't set an org, if no org is available
282-
return nil //nolint:nilerr
282+
return nil
283283
}
284284

285-
e.OrganizationID = &organizationID
285+
e.OrganizationID = organizationID
286286

287287
if _, err := uuid.Parse(e.OrganizationID.String()); err != nil {
288288
return fmt.Errorf("SetOrganizationFromCtx: cant parse organization uid: %w", err)

libs/telemetry/setup.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package telemetry
33
import (
44
"context"
55
"errors"
6-
"github.com/prometheus/client_golang/prometheus/promauto"
76
"hwutil"
87
"net/http"
98
"os"
109
"time"
1110

11+
"github.com/prometheus/client_golang/prometheus/promauto"
12+
1213
"github.com/prometheus/client_golang/prometheus"
1314
"github.com/prometheus/client_golang/prometheus/promhttp"
1415
"github.com/rs/zerolog"

services/property-svc/internal/property/commands/v1/create_property.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,8 @@ func NewCreatePropertyCommandHandler(as hwes.AggregateStore, authz hwauthz.AuthZ
3535
setID *string,
3636
fieldTypeData *models.FieldTypeData,
3737
) (version common.ConsistencyToken, err error) {
38-
user, err := perm.UserFromCtx(ctx)
39-
if err != nil {
40-
return 0, err
41-
}
42-
43-
organization, err := perm.OrganizationFromCtx(ctx)
44-
if err != nil {
45-
return 0, err
46-
}
38+
user := perm.UserFromCtx(ctx)
39+
organization := perm.OrganizationFromCtx(ctx)
4740

4841
check := hwauthz.NewPermissionCheck(user, perm.OrganizationCanUserCreateProperty, organization)
4942
if err = authz.Must(ctx, check); err != nil {

services/property-svc/internal/property/commands/v1/update_property.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,10 @@ func NewUpdatePropertyCommandHandler(as hwes.AggregateStore, authz hwauthz.AuthZ
4040
removeOptions []string,
4141
isArchived *bool,
4242
) (common.ConsistencyToken, error) {
43-
user, err := perm.UserFromCtx(ctx)
44-
if err != nil {
45-
return 0, err
46-
}
43+
user := perm.UserFromCtx(ctx)
4744

4845
check := hwauthz.NewPermissionCheck(user, perm.PropertyCanUserUpdate, perm.Property(propertyID))
49-
if err = authz.Must(ctx, check); err != nil {
46+
if err := authz.Must(ctx, check); err != nil {
5047
return 0, err
5148
}
5249

services/property-svc/internal/property/perm/permission.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,19 @@ type User uuid.UUID
2020
func (t User) Type() string { return "user" }
2121
func (t User) ID() string { return uuid.UUID(t).String() }
2222

23-
func UserFromCtx(ctx context.Context) (User, error) {
24-
userID, err := auth.GetUserID(ctx)
25-
if err != nil {
26-
return User{}, err
27-
}
28-
return User(userID), nil
23+
func UserFromCtx(ctx context.Context) User {
24+
userID := auth.MustGetUserID(ctx)
25+
return User(userID)
2926
}
3027

3128
type Organization uuid.UUID
3229

3330
func (p Organization) Type() string { return "organization" }
3431
func (p Organization) ID() string { return uuid.UUID(p).String() }
3532

36-
func OrganizationFromCtx(ctx context.Context) (Organization, error) {
37-
organizationID, err := auth.GetOrganizationID(ctx)
38-
if err != nil {
39-
return Organization{}, err
40-
}
41-
return Organization(organizationID), nil
33+
func OrganizationFromCtx(ctx context.Context) Organization {
34+
organizationID := auth.MustGetOrganizationID(ctx)
35+
return Organization(organizationID)
4236
}
4337

4438
// Direct Relations

services/property-svc/internal/property/queries/v1/get_properties_by_subject_type.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ type GetPropertiesQueryHandler func(
2222

2323
func NewGetPropertiesQueryHandler(authz hwauthz.AuthZ) GetPropertiesQueryHandler {
2424
return func(ctx context.Context, subjectType *pb.SubjectType) ([]*models.PropertyWithConsistency, error) {
25-
user, err := perm.UserFromCtx(ctx)
26-
if err != nil {
27-
return nil, err
28-
}
25+
user := perm.UserFromCtx(ctx)
2926

3027
propertyRepo := property_repo.New(hwdb.GetDB())
3128

services/property-svc/internal/property/queries/v1/get_property_by_id.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ type GetPropertyByIDQueryHandler func(
2222

2323
func NewGetPropertyByIDQueryHandler(authz hwauthz.AuthZ) GetPropertyByIDQueryHandler {
2424
return func(ctx context.Context, propertyID uuid.UUID) (*models.Property, common.ConsistencyToken, error) {
25-
user, err := perm.UserFromCtx(ctx)
26-
if err != nil {
27-
return nil, 0, err
28-
}
25+
user := perm.UserFromCtx(ctx)
2926

3027
// Verify user is allowed to see this property
3128
check := hwauthz.NewPermissionCheck(user, perm.PropertyCanUserGet, perm.Property(propertyID))
32-
if err = authz.Must(ctx, check); err != nil {
29+
if err := authz.Must(ctx, check); err != nil {
3330
return nil, 0, err
3431
}
3532

0 commit comments

Comments
 (0)