Skip to content

Commit c230a24

Browse files
authored
NOISSUE - Use structured requests in callouts (#3191)
Signed-off-by: dusan <borovcanindusan1@gmail.com>
1 parent 42b2a8c commit c230a24

File tree

7 files changed

+372
-389
lines changed

7 files changed

+372
-389
lines changed

channels/middleware/authorization.go

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package middleware
66
import (
77
"context"
88
"fmt"
9-
"maps"
109
"time"
1110

1211
"github.com/absmach/supermq/auth"
@@ -130,11 +129,13 @@ func (am *authorizationMiddleware) CreateChannels(ctx context.Context, session a
130129
}
131130
}
132131
}
132+
133133
params := map[string]any{
134134
"entities": chs,
135135
"count": len(chs),
136136
}
137-
if err := am.callOut(ctx, session, channels.OpCreateChannel.String(channels.OperationNames), params); err != nil {
137+
138+
if err := am.callOut(ctx, session, channels.OpCreateChannel.String(channels.OperationNames), "", params); err != nil {
138139
return []channels.Channel{}, []roles.RoleProvision{}, err
139140
}
140141

@@ -164,12 +165,11 @@ func (am *authorizationMiddleware) ViewChannel(ctx context.Context, session auth
164165
}); err != nil {
165166
return channels.Channel{}, errors.Wrap(err, errView)
166167
}
167-
params := map[string]any{
168-
"entity_id": id,
169-
}
170-
if err := am.callOut(ctx, session, channels.OpViewChannel.String(channels.OperationNames), params); err != nil {
168+
169+
if err := am.callOut(ctx, session, channels.OpViewChannel.String(channels.OperationNames), id, nil); err != nil {
171170
return channels.Channel{}, err
172171
}
172+
173173
return am.svc.ViewChannel(ctx, session, id, withRoles)
174174
}
175175

@@ -190,12 +190,15 @@ func (am *authorizationMiddleware) ListChannels(ctx context.Context, session aut
190190
if err := am.checkSuperAdmin(ctx, session); err == nil {
191191
session.SuperAdmin = true
192192
}
193+
193194
params := map[string]any{
194195
"pagemeta": pm,
195196
}
196-
if err := am.callOut(ctx, session, channels.OpListChannels.String(channels.OperationNames), params); err != nil {
197+
198+
if err := am.callOut(ctx, session, channels.OpListChannels.String(channels.OperationNames), "", params); err != nil {
197199
return channels.ChannelsPage{}, err
198200
}
201+
199202
return am.svc.ListChannels(ctx, session, pm)
200203
}
201204

@@ -215,13 +218,16 @@ func (am *authorizationMiddleware) ListUserChannels(ctx context.Context, session
215218
if err := am.checkSuperAdmin(ctx, session); err != nil {
216219
return channels.ChannelsPage{}, errors.Wrap(err, errList)
217220
}
221+
218222
params := map[string]any{
219223
"user_id": userID,
220224
"pagemeta": pm,
221225
}
222-
if err := am.callOut(ctx, session, channels.OpListUserChannels.String(channels.OperationNames), params); err != nil {
226+
227+
if err := am.callOut(ctx, session, channels.OpListUserChannels.String(channels.OperationNames), "", params); err != nil {
223228
return channels.ChannelsPage{}, err
224229
}
230+
225231
return am.svc.ListUserChannels(ctx, session, userID, pm)
226232
}
227233

@@ -248,12 +254,11 @@ func (am *authorizationMiddleware) UpdateChannel(ctx context.Context, session au
248254
}); err != nil {
249255
return channels.Channel{}, errors.Wrap(err, errUpdate)
250256
}
251-
params := map[string]any{
252-
"entity_id": channel.ID,
253-
}
254-
if err := am.callOut(ctx, session, channels.OpUpdateChannel.String(channels.OperationNames), params); err != nil {
257+
258+
if err := am.callOut(ctx, session, channels.OpUpdateChannel.String(channels.OperationNames), channel.ID, nil); err != nil {
255259
return channels.Channel{}, err
256260
}
261+
257262
return am.svc.UpdateChannel(ctx, session, channel)
258263
}
259264

@@ -280,12 +285,11 @@ func (am *authorizationMiddleware) UpdateChannelTags(ctx context.Context, sessio
280285
}); err != nil {
281286
return channels.Channel{}, errors.Wrap(err, errUpdateTags)
282287
}
283-
params := map[string]any{
284-
"entity_id": channel.ID,
285-
}
286-
if err := am.callOut(ctx, session, channels.OpUpdateChannelTags.String(channels.OperationNames), params); err != nil {
288+
289+
if err := am.callOut(ctx, session, channels.OpUpdateChannelTags.String(channels.OperationNames), channel.ID, nil); err != nil {
287290
return channels.Channel{}, err
288291
}
292+
289293
return am.svc.UpdateChannelTags(ctx, session, channel)
290294
}
291295

@@ -312,12 +316,11 @@ func (am *authorizationMiddleware) EnableChannel(ctx context.Context, session au
312316
}); err != nil {
313317
return channels.Channel{}, errors.Wrap(err, errEnable)
314318
}
315-
params := map[string]any{
316-
"entity_id": id,
317-
}
318-
if err := am.callOut(ctx, session, channels.OpEnableChannel.String(channels.OperationNames), params); err != nil {
319+
320+
if err := am.callOut(ctx, session, channels.OpEnableChannel.String(channels.OperationNames), id, nil); err != nil {
319321
return channels.Channel{}, err
320322
}
323+
321324
return am.svc.EnableChannel(ctx, session, id)
322325
}
323326

@@ -344,12 +347,11 @@ func (am *authorizationMiddleware) DisableChannel(ctx context.Context, session a
344347
}); err != nil {
345348
return channels.Channel{}, errors.Wrap(err, errDisable)
346349
}
347-
params := map[string]any{
348-
"entity_id": id,
349-
}
350-
if err := am.callOut(ctx, session, channels.OpDisableChannel.String(channels.OperationNames), params); err != nil {
350+
351+
if err := am.callOut(ctx, session, channels.OpDisableChannel.String(channels.OperationNames), id, nil); err != nil {
351352
return channels.Channel{}, err
352353
}
354+
353355
return am.svc.DisableChannel(ctx, session, id)
354356
}
355357

@@ -375,10 +377,8 @@ func (am *authorizationMiddleware) RemoveChannel(ctx context.Context, session au
375377
}); err != nil {
376378
return errors.Wrap(err, errDelete)
377379
}
378-
params := map[string]any{
379-
"entity_id": id,
380-
}
381-
if err := am.callOut(ctx, session, channels.OpDeleteChannel.String(channels.OperationNames), params); err != nil {
380+
381+
if err := am.callOut(ctx, session, channels.OpDeleteChannel.String(channels.OperationNames), id, nil); err != nil {
382382
return err
383383
}
384384

@@ -435,14 +435,17 @@ func (am *authorizationMiddleware) Connect(ctx context.Context, session authn.Se
435435
return errors.Wrap(err, errClientConnectChannels)
436436
}
437437
}
438+
438439
params := map[string]any{
439440
"channel_ids": chIDs,
440441
"client_ids": thIDs,
441442
"connection_types": connTypes,
442443
}
443-
if err := am.callOut(ctx, session, channels.OpConnectClient.String(channels.OperationNames), params); err != nil {
444+
445+
if err := am.callOut(ctx, session, channels.OpConnectClient.String(channels.OperationNames), "", params); err != nil {
444446
return err
445447
}
448+
446449
return am.svc.Connect(ctx, session, chIDs, thIDs, connTypes)
447450
}
448451

@@ -497,14 +500,17 @@ func (am *authorizationMiddleware) Disconnect(ctx context.Context, session authn
497500
return errors.Wrap(err, errClientDisConnectChannels)
498501
}
499502
}
503+
500504
params := map[string]any{
501505
"channel_ids": chIDs,
502506
"client_ids": thIDs,
503507
"connection_types": connTypes,
504508
}
505-
if err := am.callOut(ctx, session, channels.OpDisconnectClient.String(channels.OperationNames), params); err != nil {
509+
510+
if err := am.callOut(ctx, session, channels.OpDisconnectClient.String(channels.OperationNames), "", params); err != nil {
506511
return err
507512
}
513+
508514
return am.svc.Disconnect(ctx, session, chIDs, thIDs, connTypes)
509515
}
510516

@@ -541,13 +547,15 @@ func (am *authorizationMiddleware) SetParentGroup(ctx context.Context, session a
541547
}); err != nil {
542548
return errors.Wrap(err, errGroupSetChildChannels)
543549
}
550+
544551
params := map[string]any{
545-
"entity_id": id,
546552
"parent_group_id": parentGroupID,
547553
}
548-
if err := am.callOut(ctx, session, channels.OpSetParentGroup.String(channels.OperationNames), params); err != nil {
554+
555+
if err := am.callOut(ctx, session, channels.OpSetParentGroup.String(channels.OperationNames), id, params); err != nil {
549556
return err
550557
}
558+
551559
return am.svc.SetParentGroup(ctx, session, parentGroupID, id)
552560
}
553561

@@ -589,13 +597,15 @@ func (am *authorizationMiddleware) RemoveParentGroup(ctx context.Context, sessio
589597
}); err != nil {
590598
return errors.Wrap(err, errGroupRemoveChildChannels)
591599
}
600+
592601
params := map[string]any{
593-
"entity_id": id,
594602
"parent_group_id": ch.ParentGroup,
595603
}
596-
if err := am.callOut(ctx, session, channels.OpRemoveParentGroup.String(channels.OperationNames), params); err != nil {
604+
605+
if err := am.callOut(ctx, session, channels.OpRemoveParentGroup.String(channels.OperationNames), id, params); err != nil {
597606
return err
598607
}
608+
599609
return am.svc.RemoveParentGroup(ctx, session, id)
600610
}
601611
return nil
@@ -647,18 +657,21 @@ func (am *authorizationMiddleware) checkSuperAdmin(ctx context.Context, session
647657
return nil
648658
}
649659

650-
func (am *authorizationMiddleware) callOut(ctx context.Context, session authn.Session, op string, params map[string]any) error {
651-
pl := map[string]any{
652-
"entity_type": policies.ChannelType,
653-
"subject_type": policies.UserType,
654-
"subject_id": session.UserID,
655-
"domain": session.DomainID,
656-
"time": time.Now().UTC(),
657-
}
658-
659-
maps.Copy(params, pl)
660-
661-
if err := am.callout.Callout(ctx, op, params); err != nil {
660+
func (am *authorizationMiddleware) callOut(ctx context.Context, session authn.Session, op, entityID string, pld map[string]any) error {
661+
req := callout.Request{
662+
BaseRequest: callout.BaseRequest{
663+
Operation: op,
664+
EntityType: policies.ChannelType,
665+
EntityID: entityID,
666+
CallerID: session.UserID,
667+
CallerType: policies.UserType,
668+
DomainID: session.DomainID,
669+
Time: time.Now().UTC(),
670+
},
671+
Payload: pld,
672+
}
673+
674+
if err := am.callout.Callout(ctx, req); err != nil {
662675
return err
663676
}
664677

0 commit comments

Comments
 (0)