• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

goto / guardian / 9344046161

03 Jun 2024 03:59AM UTC coverage: 74.915% (-0.3%) from 75.222%
9344046161

Pull #125

github

rahmatrhd
chore: add additional checking for isExpired
Pull Request #125: feat(grant): add restore grant API

34 of 108 new or added lines in 4 files covered. (31.48%)

73 existing lines in 3 files now uncovered.

9664 of 12900 relevant lines covered (74.91%)

4.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.36
/api/handler/v1beta1/adapter.go
1
package v1beta1
2

3
import (
4
        "fmt"
5

6
        guardianv1beta1 "github.com/goto/guardian/api/proto/gotocompany/guardian/v1beta1"
7
        "github.com/goto/guardian/domain"
8
        "github.com/mitchellh/mapstructure"
9
        "google.golang.org/protobuf/types/known/structpb"
10
        "google.golang.org/protobuf/types/known/timestamppb"
11
)
12

13
type adapter struct{}
14

15
func NewAdapter() *adapter {
124✔
16
        return &adapter{}
124✔
17
}
124✔
18

19
func (a *adapter) FromProviderProto(p *guardianv1beta1.Provider) (*domain.Provider, error) {
×
20
        provider := &domain.Provider{
×
21
                ID:   p.GetId(),
×
22
                Type: p.GetType(),
×
23
                URN:  p.GetUrn(),
×
24
        }
×
25

×
26
        if p.GetConfig() != nil {
×
27
                provider.Config = a.FromProviderConfigProto(p.GetConfig())
×
28
        }
×
29

30
        if p.GetCreatedAt() != nil {
×
31
                provider.CreatedAt = p.GetCreatedAt().AsTime()
×
32
        }
×
33
        if p.GetUpdatedAt() != nil {
×
34
                provider.UpdatedAt = p.GetUpdatedAt().AsTime()
×
35
        }
×
36

37
        return provider, nil
×
38
}
39

40
func (a *adapter) FromProviderConfigProto(pc *guardianv1beta1.ProviderConfig) *domain.ProviderConfig {
6✔
41
        providerConfig := &domain.ProviderConfig{
6✔
42
                Type:        pc.GetType(),
6✔
43
                URN:         pc.GetUrn(),
6✔
44
                Labels:      pc.GetLabels(),
6✔
45
                Credentials: pc.GetCredentials().AsInterface(),
6✔
46
        }
6✔
47

6✔
48
        if pc.GetAppeal() != nil {
7✔
49
                appealConfig := &domain.AppealConfig{}
1✔
50
                appealConfig.AllowPermanentAccess = pc.GetAppeal().GetAllowPermanentAccess()
1✔
51
                appealConfig.AllowActiveAccessExtensionIn = pc.GetAppeal().GetAllowActiveAccessExtensionIn()
1✔
52
                providerConfig.Appeal = appealConfig
1✔
53
        }
1✔
54

55
        if pc.GetResources() != nil {
8✔
56
                resources := []*domain.ResourceConfig{}
2✔
57
                for _, r := range pc.GetResources() {
4✔
58
                        roles := []*domain.Role{}
2✔
59
                        for _, roleProto := range r.GetRoles() {
4✔
60
                                role := &domain.Role{
2✔
61
                                        ID:          roleProto.GetId(),
2✔
62
                                        Name:        roleProto.GetName(),
2✔
63
                                        Description: roleProto.GetDescription(),
2✔
64
                                }
2✔
65

2✔
66
                                if roleProto.Permissions != nil {
2✔
67
                                        permissions := []interface{}{}
×
68
                                        for _, p := range roleProto.GetPermissions() {
×
69
                                                permissions = append(permissions, p.AsInterface())
×
70
                                        }
×
71
                                        role.Permissions = permissions
×
72
                                }
73

74
                                roles = append(roles, role)
2✔
75
                        }
76

77
                        resources = append(resources, &domain.ResourceConfig{
2✔
78
                                Type:   r.GetType(),
2✔
79
                                Filter: r.GetFilter(),
2✔
80
                                Policy: a.fromPolicyConfigProto(r.GetPolicy()),
2✔
81
                                Roles:  roles,
2✔
82
                        })
2✔
83
                }
84
                providerConfig.Resources = resources
2✔
85
        }
86

87
        if pc.GetParameters() != nil {
7✔
88
                parameters := []*domain.ProviderParameter{}
1✔
89
                for _, p := range pc.GetParameters() {
2✔
90
                        parameters = append(parameters, &domain.ProviderParameter{
1✔
91
                                Key:         p.GetKey(),
1✔
92
                                Label:       p.GetLabel(),
1✔
93
                                Required:    p.GetRequired(),
1✔
94
                                Description: p.GetDescription(),
1✔
95
                        })
1✔
96
                }
1✔
97
                providerConfig.Parameters = parameters
1✔
98
        }
99

100
        if pc.GetAllowedAccountTypes() != nil {
8✔
101
                providerConfig.AllowedAccountTypes = pc.GetAllowedAccountTypes()
2✔
102
        }
2✔
103

104
        return providerConfig
6✔
105
}
106

107
func (a *adapter) ToProviderProto(p *domain.Provider) (*guardianv1beta1.Provider, error) {
8✔
108
        providerProto := &guardianv1beta1.Provider{
8✔
109
                Id:   p.ID,
8✔
110
                Type: p.Type,
8✔
111
                Urn:  p.URN,
8✔
112
        }
8✔
113

8✔
114
        if p.Config != nil {
16✔
115
                config, err := a.ToProviderConfigProto(p.Config)
8✔
116
                if err != nil {
12✔
117
                        return nil, err
4✔
118
                }
4✔
119
                providerProto.Config = config
4✔
120
        }
121

122
        if !p.CreatedAt.IsZero() {
8✔
123
                providerProto.CreatedAt = timestamppb.New(p.CreatedAt)
4✔
124
        }
4✔
125
        if !p.UpdatedAt.IsZero() {
8✔
126
                providerProto.UpdatedAt = timestamppb.New(p.UpdatedAt)
4✔
127
        }
4✔
128

129
        return providerProto, nil
4✔
130
}
131

132
func (a *adapter) ToProviderConfigProto(pc *domain.ProviderConfig) (*guardianv1beta1.ProviderConfig, error) {
8✔
133
        providerConfigProto := &guardianv1beta1.ProviderConfig{
8✔
134
                Type:   pc.Type,
8✔
135
                Urn:    pc.URN,
8✔
136
                Labels: pc.Labels,
8✔
137
        }
8✔
138

8✔
139
        if pc.Credentials != nil {
11✔
140
                credentials, err := structpb.NewValue(pc.Credentials)
3✔
141
                if err != nil {
6✔
142
                        return nil, err
3✔
143
                }
3✔
144
                providerConfigProto.Credentials = credentials
×
145
        }
146

147
        if pc.Appeal != nil {
7✔
148
                providerConfigProto.Appeal = &guardianv1beta1.ProviderConfig_AppealConfig{
2✔
149
                        AllowPermanentAccess:         pc.Appeal.AllowPermanentAccess,
2✔
150
                        AllowActiveAccessExtensionIn: pc.Appeal.AllowActiveAccessExtensionIn,
2✔
151
                }
2✔
152
        }
2✔
153

154
        if pc.Resources != nil {
10✔
155
                resources := []*guardianv1beta1.ProviderConfig_ResourceConfig{}
5✔
156
                for _, rc := range pc.Resources {
10✔
157
                        roles := []*guardianv1beta1.Role{}
5✔
158
                        for _, role := range rc.Roles {
10✔
159
                                roleProto, err := a.ToRole(role)
5✔
160
                                if err != nil {
6✔
161
                                        return nil, err
1✔
162
                                }
1✔
163
                                roles = append(roles, roleProto)
4✔
164
                        }
165

166
                        resources = append(resources, &guardianv1beta1.ProviderConfig_ResourceConfig{
4✔
167
                                Type:   rc.Type,
4✔
168
                                Policy: a.toPolicyConfigProto(rc.Policy),
4✔
169
                                Roles:  roles,
4✔
170
                        })
4✔
171
                }
172
                providerConfigProto.Resources = resources
4✔
173
        }
174

175
        if pc.Parameters != nil {
5✔
176
                parameters := []*guardianv1beta1.ProviderConfig_ProviderParameter{}
1✔
177
                for _, p := range pc.Parameters {
2✔
178
                        parameters = append(parameters, &guardianv1beta1.ProviderConfig_ProviderParameter{
1✔
179
                                Key:         p.Key,
1✔
180
                                Label:       p.Label,
1✔
181
                                Required:    p.Required,
1✔
182
                                Description: p.Description,
1✔
183
                        })
1✔
184
                }
1✔
185
                providerConfigProto.Parameters = parameters
1✔
186
        }
187

188
        if pc.AllowedAccountTypes != nil {
8✔
189
                providerConfigProto.AllowedAccountTypes = pc.AllowedAccountTypes
4✔
190
        }
4✔
191

192
        return providerConfigProto, nil
4✔
193
}
194

195
func (a *adapter) ToProviderTypeProto(pt domain.ProviderType) *guardianv1beta1.ProviderType {
1✔
196
        return &guardianv1beta1.ProviderType{
1✔
197
                Name:          pt.Name,
1✔
198
                ResourceTypes: pt.ResourceTypes,
1✔
199
        }
1✔
200
}
1✔
201

202
func (a *adapter) ToRole(role *domain.Role) (*guardianv1beta1.Role, error) {
7✔
203
        roleProto := &guardianv1beta1.Role{
7✔
204
                Id:          role.ID,
7✔
205
                Name:        role.Name,
7✔
206
                Description: role.Description,
7✔
207
        }
7✔
208

7✔
209
        if role.Permissions != nil {
10✔
210
                permissions := []*structpb.Value{}
3✔
211
                for _, p := range role.Permissions {
6✔
212
                        permission, err := structpb.NewValue(p)
3✔
213
                        if err != nil {
5✔
214
                                return nil, err
2✔
215
                        }
2✔
216
                        permissions = append(permissions, permission)
1✔
217
                }
218
                roleProto.Permissions = permissions
1✔
219
        }
220

221
        return roleProto, nil
5✔
222
}
223

224
func (a *adapter) FromPolicyProto(p *guardianv1beta1.Policy) *domain.Policy {
8✔
225
        policy := &domain.Policy{
8✔
226
                ID:          p.GetId(),
8✔
227
                Version:     uint(p.GetVersion()),
8✔
228
                Description: p.GetDescription(),
8✔
229
                Labels:      p.GetLabels(),
8✔
230
        }
8✔
231

8✔
232
        if p.GetSteps() != nil {
10✔
233
                var steps []*domain.Step
2✔
234
                for _, s := range p.GetSteps() {
4✔
235
                        steps = append(steps, &domain.Step{
2✔
236
                                Name:            s.GetName(),
2✔
237
                                Description:     s.GetDescription(),
2✔
238
                                When:            s.GetWhen(),
2✔
239
                                Strategy:        domain.ApprovalStepStrategy(s.GetStrategy()),
2✔
240
                                RejectionReason: s.GetRejectionReason(),
2✔
241
                                ApproveIf:       s.GetApproveIf(),
2✔
242
                                AllowFailed:     s.GetAllowFailed(),
2✔
243
                                Approvers:       s.GetApprovers(),
2✔
244
                        })
2✔
245
                }
2✔
246
                policy.Steps = steps
2✔
247
        }
248

249
        if p.GetRequirements() != nil {
10✔
250
                var requirements []*domain.Requirement
2✔
251
                for _, r := range p.GetRequirements() {
4✔
252
                        var on *domain.RequirementTrigger
2✔
253
                        if r.GetOn() != nil {
4✔
254
                                var conditions []*domain.Condition
2✔
255
                                if r.GetOn().GetConditions() != nil {
2✔
256
                                        for _, c := range r.GetOn().GetConditions() {
×
257
                                                conditions = append(conditions, a.fromConditionProto(c))
×
258
                                        }
×
259
                                }
260

261
                                on = &domain.RequirementTrigger{
2✔
262
                                        ProviderType: r.GetOn().GetProviderType(),
2✔
263
                                        ProviderURN:  r.GetOn().GetProviderUrn(),
2✔
264
                                        ResourceType: r.GetOn().GetResourceType(),
2✔
265
                                        ResourceURN:  r.GetOn().GetResourceUrn(),
2✔
266
                                        Role:         r.GetOn().GetRole(),
2✔
267
                                        Conditions:   conditions,
2✔
268
                                        Expression:   r.GetOn().GetExpression(),
2✔
269
                                }
2✔
270
                        }
271

272
                        var additionalAppeals []*domain.AdditionalAppeal
2✔
273
                        if r.GetAppeals() != nil {
4✔
274
                                for _, aa := range r.GetAppeals() {
4✔
275
                                        var resource *domain.ResourceIdentifier
2✔
276
                                        if aa.GetResource() != nil {
4✔
277
                                                resource = &domain.ResourceIdentifier{
2✔
278
                                                        ProviderType: aa.GetResource().GetProviderType(),
2✔
279
                                                        ProviderURN:  aa.GetResource().GetProviderUrn(),
2✔
280
                                                        Type:         aa.GetResource().GetType(),
2✔
281
                                                        URN:          aa.GetResource().GetUrn(),
2✔
282
                                                        ID:           aa.GetResource().GetId(),
2✔
283
                                                }
2✔
284
                                        }
2✔
285

286
                                        additionalAppeals = append(additionalAppeals, &domain.AdditionalAppeal{
2✔
287
                                                Resource: resource,
2✔
288
                                                Role:     aa.GetRole(),
2✔
289
                                                Options:  a.fromAppealOptionsProto(aa.GetOptions()),
2✔
290
                                                Policy:   a.fromPolicyConfigProto(aa.GetPolicy()),
2✔
291
                                        })
2✔
292
                                }
293
                        }
294

295
                        requirements = append(requirements, &domain.Requirement{
2✔
296
                                On:      on,
2✔
297
                                Appeals: additionalAppeals,
2✔
298
                        })
2✔
299
                        policy.Requirements = requirements
2✔
300
                }
301
        }
302

303
        if p.GetIam() != nil {
10✔
304
                policy.IAM = &domain.IAMConfig{
2✔
305
                        Provider: domain.IAMProviderType(p.GetIam().GetProvider()),
2✔
306
                        Config:   p.GetIam().GetConfig().AsInterface(),
2✔
307
                        Schema:   p.GetIam().GetSchema(),
2✔
308
                }
2✔
309
        }
2✔
310

311
        if p.GetAppeal() != nil {
10✔
312
                var durationOptions []domain.AppealDurationOption
2✔
313
                var questions []domain.Question
2✔
314
                var metadataSources map[string]*domain.AppealMetadataSource
2✔
315
                for _, d := range p.GetAppeal().GetDurationOptions() {
6✔
316
                        option := domain.AppealDurationOption{
4✔
317
                                Name:  d.GetName(),
4✔
318
                                Value: d.GetValue(),
4✔
319
                        }
4✔
320
                        durationOptions = append(durationOptions, option)
4✔
321
                }
4✔
322
                for _, q := range p.GetAppeal().GetQuestions() {
3✔
323
                        question := domain.Question{
1✔
324
                                Key:         q.GetKey(),
1✔
325
                                Question:    q.GetQuestion(),
1✔
326
                                Required:    q.GetRequired(),
1✔
327
                                Description: q.GetDescription(),
1✔
328
                        }
1✔
329
                        questions = append(questions, question)
1✔
330
                }
1✔
331

332
                if mds := p.GetAppeal().GetMetadataSources(); mds != nil {
3✔
333
                        metadataSources = make(map[string]*domain.AppealMetadataSource)
1✔
334
                        for key, metadataCfg := range mds {
2✔
335
                                metadataSources[key] = &domain.AppealMetadataSource{
1✔
336
                                        Name:        metadataCfg.GetName(),
1✔
337
                                        Description: metadataCfg.GetDescription(),
1✔
338
                                        Type:        metadataCfg.GetType(),
1✔
339
                                        Config:      metadataCfg.GetConfig().AsInterface(),
1✔
340
                                        Value:       metadataCfg.GetValue().AsInterface(),
1✔
341
                                }
1✔
342
                        }
1✔
343
                }
344

345
                policy.AppealConfig = &domain.PolicyAppealConfig{
2✔
346
                        DurationOptions:              durationOptions,
2✔
347
                        AllowOnBehalf:                p.GetAppeal().GetAllowOnBehalf(),
2✔
348
                        Questions:                    questions,
2✔
349
                        AllowPermanentAccess:         p.GetAppeal().GetAllowPermanentAccess(),
2✔
350
                        AllowActiveAccessExtensionIn: p.GetAppeal().GetAllowActiveAccessExtensionIn(),
2✔
351
                        AllowCreatorDetailsFailure:   p.GetAppeal().GetAllowCreatorDetailsFailure(),
2✔
352
                        MetadataSources:              metadataSources,
2✔
353
                }
2✔
354
        }
355

356
        if p.GetCreatedAt() != nil {
8✔
357
                policy.CreatedAt = p.GetCreatedAt().AsTime()
×
358
        }
×
359
        if p.GetUpdatedAt() != nil {
8✔
360
                policy.UpdatedAt = p.GetUpdatedAt().AsTime()
×
361
        }
×
362

363
        return policy
8✔
364
}
365

366
func (a *adapter) ToPolicyProto(p *domain.Policy) (*guardianv1beta1.Policy, error) {
8✔
367
        policyProto := &guardianv1beta1.Policy{
8✔
368
                Id:          p.ID,
8✔
369
                Version:     uint32(p.Version),
8✔
370
                Description: p.Description,
8✔
371
                Labels:      p.Labels,
8✔
372
        }
8✔
373

8✔
374
        if p.Steps != nil {
12✔
375
                var steps []*guardianv1beta1.Policy_ApprovalStep
4✔
376
                for _, s := range p.Steps {
8✔
377
                        steps = append(steps, &guardianv1beta1.Policy_ApprovalStep{
4✔
378
                                Name:            s.Name,
4✔
379
                                Description:     s.Description,
4✔
380
                                When:            s.When,
4✔
381
                                Strategy:        string(s.Strategy),
4✔
382
                                RejectionReason: s.RejectionReason,
4✔
383
                                ApproveIf:       s.ApproveIf,
4✔
384
                                AllowFailed:     s.AllowFailed,
4✔
385
                                Approvers:       s.Approvers,
4✔
386
                        })
4✔
387
                }
4✔
388
                policyProto.Steps = steps
4✔
389
        }
390

391
        if p.Requirements != nil {
12✔
392
                var requirements []*guardianv1beta1.Policy_Requirement
4✔
393
                for _, r := range p.Requirements {
8✔
394
                        var on *guardianv1beta1.Policy_Requirement_RequirementTrigger
4✔
395
                        if r.On != nil {
8✔
396
                                var conditions []*guardianv1beta1.Condition
4✔
397
                                if r.On.Conditions != nil {
4✔
398
                                        for _, c := range r.On.Conditions {
×
399
                                                condition, err := a.toConditionProto(c)
×
400
                                                if err != nil {
×
401
                                                        return nil, err
×
402
                                                }
×
403
                                                conditions = append(conditions, condition)
×
404
                                        }
405
                                }
406

407
                                on = &guardianv1beta1.Policy_Requirement_RequirementTrigger{
4✔
408
                                        ProviderType: r.On.ProviderType,
4✔
409
                                        ProviderUrn:  r.On.ProviderURN,
4✔
410
                                        ResourceType: r.On.ResourceType,
4✔
411
                                        ResourceUrn:  r.On.ResourceURN,
4✔
412
                                        Role:         r.On.Role,
4✔
413
                                        Conditions:   conditions,
4✔
414
                                        Expression:   r.On.Expression,
4✔
415
                                }
4✔
416
                        }
417

418
                        var additionalAppeals []*guardianv1beta1.Policy_Requirement_AdditionalAppeal
4✔
419
                        if r.Appeals != nil {
8✔
420
                                for _, aa := range r.Appeals {
8✔
421
                                        var resource *guardianv1beta1.Policy_Requirement_AdditionalAppeal_ResourceIdentifier
4✔
422
                                        if aa.Resource != nil {
8✔
423
                                                resource = &guardianv1beta1.Policy_Requirement_AdditionalAppeal_ResourceIdentifier{
4✔
424
                                                        ProviderType: aa.Resource.ProviderType,
4✔
425
                                                        ProviderUrn:  aa.Resource.ProviderURN,
4✔
426
                                                        Type:         aa.Resource.Type,
4✔
427
                                                        Urn:          aa.Resource.URN,
4✔
428
                                                        Id:           aa.Resource.ID,
4✔
429
                                                }
4✔
430
                                        }
4✔
431

432
                                        additionalAppeals = append(additionalAppeals, &guardianv1beta1.Policy_Requirement_AdditionalAppeal{
4✔
433
                                                Resource: resource,
4✔
434
                                                Role:     aa.Role,
4✔
435
                                                Options:  a.toAppealOptionsProto(aa.Options),
4✔
436
                                                Policy:   a.toPolicyConfigProto(aa.Policy),
4✔
437
                                        })
4✔
438
                                }
439
                        }
440

441
                        requirements = append(requirements, &guardianv1beta1.Policy_Requirement{
4✔
442
                                On:      on,
4✔
443
                                Appeals: additionalAppeals,
4✔
444
                        })
4✔
445
                        policyProto.Requirements = requirements
4✔
446
                }
447
        }
448

449
        if p.HasIAMConfig() {
15✔
450
                config, err := structpb.NewValue(p.IAM.Config)
7✔
451
                if err != nil {
9✔
452
                        return nil, err
2✔
453
                }
2✔
454

455
                policyProto.Iam = &guardianv1beta1.Policy_IAM{
5✔
456
                        Provider: string(p.IAM.Provider),
5✔
457
                        Config:   config,
5✔
458
                        Schema:   p.IAM.Schema,
5✔
459
                }
5✔
460
        }
461

462
        appealConfig, err := a.ToPolicyAppealConfigProto(p)
6✔
463
        if err != nil {
6✔
464
                return nil, fmt.Errorf("failed to convert appeal config to proto: %w", err)
×
465
        }
×
466
        policyProto.Appeal = appealConfig
6✔
467

6✔
468
        if !p.CreatedAt.IsZero() {
10✔
469
                policyProto.CreatedAt = timestamppb.New(p.CreatedAt)
4✔
470
        }
4✔
471
        if !p.UpdatedAt.IsZero() {
10✔
472
                policyProto.UpdatedAt = timestamppb.New(p.UpdatedAt)
4✔
473
        }
4✔
474

475
        return policyProto, nil
6✔
476
}
477

478
func (a *adapter) ToPolicyAppealConfigProto(p *domain.Policy) (*guardianv1beta1.PolicyAppealConfig, error) {
7✔
479
        if p.AppealConfig == nil {
8✔
480
                return nil, nil
1✔
481
        }
1✔
482

483
        policyAppealConfigProto := &guardianv1beta1.PolicyAppealConfig{}
6✔
484
        var durationOptions []*guardianv1beta1.PolicyAppealConfig_DurationOptions
6✔
485
        if p.AppealConfig.DurationOptions != nil {
10✔
486
                for _, d := range p.AppealConfig.DurationOptions {
12✔
487
                        durationOptions = append(durationOptions, &guardianv1beta1.PolicyAppealConfig_DurationOptions{
8✔
488
                                Name:  d.Name,
8✔
489
                                Value: d.Value,
8✔
490
                        })
8✔
491
                }
8✔
492
        }
493
        policyAppealConfigProto.DurationOptions = durationOptions
6✔
494
        policyAppealConfigProto.AllowOnBehalf = p.AppealConfig.AllowOnBehalf
6✔
495
        policyAppealConfigProto.AllowPermanentAccess = p.AppealConfig.AllowPermanentAccess
6✔
496
        policyAppealConfigProto.AllowActiveAccessExtensionIn = p.AppealConfig.AllowActiveAccessExtensionIn
6✔
497
        policyAppealConfigProto.AllowCreatorDetailsFailure = p.AppealConfig.AllowCreatorDetailsFailure
6✔
498

6✔
499
        for _, q := range p.AppealConfig.Questions {
7✔
500
                policyAppealConfigProto.Questions = append(policyAppealConfigProto.Questions, &guardianv1beta1.PolicyAppealConfig_Question{
1✔
501
                        Key:         q.Key,
1✔
502
                        Question:    q.Question,
1✔
503
                        Required:    q.Required,
1✔
504
                        Description: q.Description,
1✔
505
                })
1✔
506
        }
1✔
507

508
        for key, metadataSource := range p.AppealConfig.MetadataSources {
9✔
509
                if policyAppealConfigProto.MetadataSources == nil {
6✔
510
                        policyAppealConfigProto.MetadataSources = make(map[string]*guardianv1beta1.PolicyAppealConfig_MetadataSource)
3✔
511
                }
3✔
512
                policyAppealConfigProto.MetadataSources[key] = &guardianv1beta1.PolicyAppealConfig_MetadataSource{
3✔
513
                        Name:        metadataSource.Name,
3✔
514
                        Description: metadataSource.Description,
3✔
515
                        Type:        metadataSource.Type,
3✔
516
                }
3✔
517

3✔
518
                if metadataSource.Config != nil {
4✔
519
                        cfg, err := structpb.NewValue(metadataSource.Config)
1✔
520
                        if err != nil {
1✔
521
                                return nil, err
×
522
                        }
×
523
                        policyAppealConfigProto.MetadataSources[key].Config = cfg
1✔
524
                }
525
                if metadataSource.Value != nil {
4✔
526
                        value, err := structpb.NewValue(metadataSource.Value)
1✔
527
                        if err != nil {
1✔
528
                                return nil, err
×
529
                        }
×
530
                        policyAppealConfigProto.MetadataSources[key].Value = value
1✔
531
                }
532
        }
533

534
        return policyAppealConfigProto, nil
6✔
535
}
536

537
func (a *adapter) FromResourceProto(r *guardianv1beta1.Resource) *domain.Resource {
4✔
538
        resource := &domain.Resource{
4✔
539
                ID:           r.GetId(),
4✔
540
                ProviderType: r.GetProviderType(),
4✔
541
                ProviderURN:  r.GetProviderUrn(),
4✔
542
                Type:         r.GetType(),
4✔
543
                URN:          r.GetUrn(),
4✔
544
                Name:         r.GetName(),
4✔
545
                Labels:       r.GetLabels(),
4✔
546
                IsDeleted:    r.GetIsDeleted(),
4✔
547
        }
4✔
548

4✔
549
        if r.GetParentId() != "" {
4✔
550
                id := r.GetParentId()
×
551
                resource.ParentID = &id
×
552
        }
×
553

554
        if r.GetChildren() != nil {
4✔
555
                for _, c := range r.GetChildren() {
×
556
                        resource.Children = append(resource.Children, a.FromResourceProto(c))
×
557
                }
×
558
        }
559

560
        if r.GetDetails() != nil {
4✔
561
                resource.Details = r.GetDetails().AsMap()
×
562
        }
×
563

564
        if r.GetCreatedAt() != nil {
4✔
565
                resource.CreatedAt = r.GetCreatedAt().AsTime()
×
566
        }
×
567
        if r.GetUpdatedAt() != nil {
4✔
568
                resource.UpdatedAt = r.GetUpdatedAt().AsTime()
×
569
        }
×
570

571
        return resource
4✔
572
}
573

574
func (a *adapter) ToResourceProto(r *domain.Resource) (*guardianv1beta1.Resource, error) {
21✔
575
        resourceProto := &guardianv1beta1.Resource{
21✔
576
                Id:           r.ID,
21✔
577
                ProviderType: r.ProviderType,
21✔
578
                ProviderUrn:  r.ProviderURN,
21✔
579
                Type:         r.Type,
21✔
580
                Urn:          r.URN,
21✔
581
                Name:         r.Name,
21✔
582
                Labels:       r.Labels,
21✔
583
                IsDeleted:    r.IsDeleted,
21✔
584
        }
21✔
585

21✔
586
        if r.ParentID != nil {
21✔
587
                resourceProto.ParentId = *r.ParentID
×
588
        }
×
589

590
        if r.GlobalURN != "" {
22✔
591
                resourceProto.GlobalUrn = r.GlobalURN
1✔
592
        }
1✔
593

594
        if r.Children != nil {
21✔
595
                for _, c := range r.Children {
×
596
                        childProto, err := a.ToResourceProto(c)
×
597
                        if err != nil {
×
598
                                return nil, fmt.Errorf("failed to convert child resource to proto %q: %w", c.ID, err)
×
599
                        }
×
600
                        resourceProto.Children = append(resourceProto.Children, childProto)
×
601
                }
602
        }
603

604
        if r.Details != nil {
27✔
605
                details, err := structpb.NewStruct(r.Details)
6✔
606
                if err != nil {
11✔
607
                        return nil, err
5✔
608
                }
5✔
609
                resourceProto.Details = details
1✔
610
        }
611

612
        if !r.CreatedAt.IsZero() {
19✔
613
                resourceProto.CreatedAt = timestamppb.New(r.CreatedAt)
3✔
614
        }
3✔
615
        if !r.UpdatedAt.IsZero() {
19✔
616
                resourceProto.UpdatedAt = timestamppb.New(r.UpdatedAt)
3✔
617
        }
3✔
618

619
        return resourceProto, nil
16✔
620
}
621

622
func (a *adapter) ToAppealProto(appeal *domain.Appeal) (*guardianv1beta1.Appeal, error) {
23✔
623
        appealProto := &guardianv1beta1.Appeal{
23✔
624
                Id:            appeal.ID,
23✔
625
                ResourceId:    appeal.ResourceID,
23✔
626
                PolicyId:      appeal.PolicyID,
23✔
627
                PolicyVersion: uint32(appeal.PolicyVersion),
23✔
628
                Status:        appeal.Status,
23✔
629
                AccountId:     appeal.AccountID,
23✔
630
                AccountType:   appeal.AccountType,
23✔
631
                CreatedBy:     appeal.CreatedBy,
23✔
632
                Role:          appeal.Role,
23✔
633
                Permissions:   appeal.Permissions,
23✔
634
                Options:       a.toAppealOptionsProto(appeal.Options),
23✔
635
                Labels:        appeal.Labels,
23✔
636
                Description:   appeal.Description,
23✔
637
        }
23✔
638

23✔
639
        if appeal.Resource != nil {
33✔
640
                r, err := a.ToResourceProto(appeal.Resource)
10✔
641
                if err != nil {
10✔
642
                        return nil, err
×
643
                }
×
644
                appealProto.Resource = r
10✔
645
        }
646

647
        if appeal.Creator != nil {
40✔
648
                creator, err := structpb.NewValue(appeal.Creator)
17✔
649
                if err != nil {
27✔
650
                        return nil, err
10✔
651
                }
10✔
652
                appealProto.Creator = creator
7✔
653
        }
654

655
        if appeal.Approvals != nil {
21✔
656
                approvals := []*guardianv1beta1.Approval{}
8✔
657
                for _, approval := range appeal.Approvals {
16✔
658
                        approvalProto, err := a.ToApprovalProto(approval)
8✔
659
                        if err != nil {
8✔
660
                                return nil, err
×
661
                        }
×
662

663
                        approvals = append(approvals, approvalProto)
8✔
664
                }
665
                appealProto.Approvals = approvals
8✔
666
        }
667

668
        if appeal.Details != nil {
21✔
669
                details, err := structpb.NewStruct(appeal.Details)
8✔
670
                if err != nil {
8✔
671
                        return nil, err
×
672
                }
×
673
                appealProto.Details = details
8✔
674
        }
675

676
        if !appeal.CreatedAt.IsZero() {
23✔
677
                appealProto.CreatedAt = timestamppb.New(appeal.CreatedAt)
10✔
678
        }
10✔
679
        if !appeal.UpdatedAt.IsZero() {
23✔
680
                appealProto.UpdatedAt = timestamppb.New(appeal.UpdatedAt)
10✔
681
        }
10✔
682

683
        grantProto, err := a.ToGrantProto(appeal.Grant)
13✔
684
        if err != nil {
13✔
685
                return nil, fmt.Errorf("parsing grant: %w", err)
×
686
        }
×
687
        appealProto.Grant = grantProto
13✔
688

13✔
689
        return appealProto, nil
13✔
690
}
691

692
func (a *adapter) FromCreateAppealProto(ca *guardianv1beta1.CreateAppealRequest, authenticatedUser string) ([]*domain.Appeal, error) {
4✔
693
        var appeals []*domain.Appeal
4✔
694

4✔
695
        for _, r := range ca.GetResources() {
6✔
696
                appeal := &domain.Appeal{
2✔
697
                        AccountID:   ca.GetAccountId(),
2✔
698
                        AccountType: ca.GetAccountType(),
2✔
699
                        CreatedBy:   authenticatedUser,
2✔
700
                        ResourceID:  r.GetId(),
2✔
701
                        Role:        r.GetRole(),
2✔
702
                        Description: ca.GetDescription(),
2✔
703
                }
2✔
704

2✔
705
                if r.GetOptions() != nil {
3✔
706
                        var options *domain.AppealOptions
1✔
707
                        if err := mapstructure.Decode(r.GetOptions().AsMap(), &options); err != nil {
1✔
708
                                return nil, err
×
709
                        }
×
710
                        appeal.Options = options
1✔
711
                }
712

713
                if r.GetDetails() != nil {
3✔
714
                        appeal.Details = r.GetDetails().AsMap()
1✔
715
                }
1✔
716

717
                appeals = append(appeals, appeal)
2✔
718
        }
719

720
        return appeals, nil
4✔
721
}
722

723
func (a *adapter) ToApprovalProto(approval *domain.Approval) (*guardianv1beta1.Approval, error) {
12✔
724
        approvalProto := &guardianv1beta1.Approval{
12✔
725
                Id:            approval.ID,
12✔
726
                Name:          approval.Name,
12✔
727
                AppealId:      approval.AppealID,
12✔
728
                Status:        approval.Status,
12✔
729
                Reason:        approval.Reason,
12✔
730
                PolicyId:      approval.PolicyID,
12✔
731
                PolicyVersion: uint32(approval.PolicyVersion),
12✔
732
                Approvers:     approval.Approvers,
12✔
733
                CreatedAt:     timestamppb.New(approval.CreatedAt),
12✔
734
                UpdatedAt:     timestamppb.New(approval.UpdatedAt),
12✔
735
        }
12✔
736

12✔
737
        if approval.Appeal != nil {
16✔
738
                appeal, err := a.ToAppealProto(approval.Appeal)
4✔
739
                if err != nil {
6✔
740
                        return nil, err
2✔
741
                }
2✔
742
                approvalProto.Appeal = appeal
2✔
743
        }
744

745
        if approval.Actor != nil {
11✔
746
                approvalProto.Actor = *approval.Actor
1✔
747
        }
1✔
748

749
        if !approval.CreatedAt.IsZero() {
20✔
750
                approvalProto.CreatedAt = timestamppb.New(approval.CreatedAt)
10✔
751
        }
10✔
752
        if !approval.UpdatedAt.IsZero() {
20✔
753
                approvalProto.UpdatedAt = timestamppb.New(approval.UpdatedAt)
10✔
754
        }
10✔
755

756
        return approvalProto, nil
10✔
757
}
758

759
func (a *adapter) FromGrantProto(g *guardianv1beta1.Grant) *domain.Grant {
×
760
        if g == nil {
×
761
                return nil
×
762
        }
×
763

764
        grant := &domain.Grant{
×
NEW
765
                ID:                   g.GetId(),
×
NEW
766
                Status:               domain.GrantStatus(g.GetStatus()),
×
NEW
767
                StatusInProvider:     domain.GrantStatus(g.GetStatusInProvider()),
×
NEW
768
                AccountID:            g.GetAccountId(),
×
NEW
769
                AccountType:          g.GetAccountType(),
×
NEW
770
                ResourceID:           g.GetResourceId(),
×
NEW
771
                Role:                 g.GetRole(),
×
NEW
772
                Permissions:          g.GetPermissions(),
×
NEW
773
                AppealID:             g.GetAppealId(),
×
NEW
774
                Source:               domain.GrantSource(g.Source),
×
NEW
775
                RevokedBy:            g.GetRevokedBy(),
×
NEW
776
                RevokeReason:         g.GetRevokeReason(),
×
NEW
777
                CreatedBy:            g.GetCreatedBy(),
×
NEW
778
                Owner:                g.GetOwner(),
×
NEW
779
                Resource:             a.FromResourceProto(g.GetResource()),
×
NEW
780
                ExpirationDateReason: g.GetExpirationDateReason(),
×
NEW
781
                RestoreReason:        g.GetRestoreReason(),
×
NEW
782
                RestoredBy:           g.GetRestoredBy(),
×
783
        }
×
784

×
785
        if g.GetExpirationDate() != nil {
×
786
                t := g.GetExpirationDate().AsTime()
×
787
                grant.ExpirationDate = &t
×
788
        }
×
789
        if g.GetRevokedAt() != nil {
×
790
                t := g.GetRevokedAt().AsTime()
×
791
                grant.RevokedAt = &t
×
792
        }
×
793
        if g.GetCreatedAt() != nil {
×
794
                grant.CreatedAt = g.GetCreatedAt().AsTime()
×
795
        }
×
796
        if g.GetUpdatedAt() != nil {
×
797
                grant.UpdatedAt = g.GetUpdatedAt().AsTime()
×
798
        }
×
NEW
799
        if g.GetRestoredAt() != nil {
×
NEW
800
                t := g.GetRestoredAt().AsTime()
×
NEW
801
                grant.RestoredAt = &t
×
NEW
802
        }
×
803

804
        return grant
×
805
}
806

807
func (a *adapter) ToGrantProto(grant *domain.Grant) (*guardianv1beta1.Grant, error) {
19✔
808
        if grant == nil {
32✔
809
                return nil, nil
13✔
810
        }
13✔
811

812
        grantProto := &guardianv1beta1.Grant{
6✔
813
                Id:                   grant.ID,
6✔
814
                Status:               string(grant.Status),
6✔
815
                StatusInProvider:     string(grant.StatusInProvider),
6✔
816
                AccountId:            grant.AccountID,
6✔
817
                AccountType:          grant.AccountType,
6✔
818
                ResourceId:           grant.ResourceID,
6✔
819
                Role:                 grant.Role,
6✔
820
                Permissions:          grant.Permissions,
6✔
821
                IsPermanent:          grant.IsPermanent,
6✔
822
                AppealId:             grant.AppealID,
6✔
823
                Source:               string(grant.Source),
6✔
824
                RevokedBy:            grant.RevokedBy,
6✔
825
                RevokeReason:         grant.RevokeReason,
6✔
826
                CreatedBy:            grant.CreatedBy,
6✔
827
                Owner:                grant.Owner,
6✔
828
                ExpirationDateReason: grant.ExpirationDateReason,
6✔
829
                RestoreReason:        grant.RestoreReason,
6✔
830
                RestoredBy:           grant.RestoredBy,
6✔
831
        }
6✔
832

6✔
833
        if grant.ExpirationDate != nil {
9✔
834
                grantProto.ExpirationDate = timestamppb.New(*grant.ExpirationDate)
3✔
835
        }
3✔
836
        if grant.RevokedAt != nil {
9✔
837
                grantProto.RevokedAt = timestamppb.New(*grant.RevokedAt)
3✔
838
        }
3✔
839
        if !grant.CreatedAt.IsZero() {
9✔
840
                grantProto.CreatedAt = timestamppb.New(grant.CreatedAt)
3✔
841
        }
3✔
842
        if !grant.UpdatedAt.IsZero() {
10✔
843
                grantProto.UpdatedAt = timestamppb.New(grant.UpdatedAt)
4✔
844
        }
4✔
845
        if grant.Resource != nil {
11✔
846
                resourceProto, err := a.ToResourceProto(grant.Resource)
5✔
847
                if err != nil {
7✔
848
                        return nil, fmt.Errorf("parsing resource: %w", err)
2✔
849
                }
2✔
850
                grantProto.Resource = resourceProto
3✔
851
        }
852
        if grant.Appeal != nil {
7✔
853
                appealProto, err := a.ToAppealProto(grant.Appeal)
3✔
854
                if err != nil {
3✔
855
                        return nil, fmt.Errorf("parsing appeal: %w", err)
×
856
                }
×
857
                grantProto.Appeal = appealProto
3✔
858
        }
859
        if grant.RestoredAt != nil {
4✔
NEW
UNCOV
860
                grantProto.RestoredAt = timestamppb.New(*grant.RestoredAt)
×
NEW
UNCOV
861
        }
×
862

863
        return grantProto, nil
4✔
864
}
865

866
func (a *adapter) ToActivityProto(activity *domain.Activity) (*guardianv1beta1.ProviderActivity, error) {
×
UNCOV
867
        if activity == nil {
×
868
                return nil, nil
×
869
        }
×
870

871
        activityProto := &guardianv1beta1.ProviderActivity{
×
872
                Id:                 activity.ID,
×
873
                ProviderId:         activity.ProviderID,
×
874
                ResourceId:         activity.ResourceID,
×
875
                ProviderActivityId: activity.ProviderActivityID,
×
876
                AccountType:        activity.AccountType,
×
877
                AccountId:          activity.AccountID,
×
878
                Authorizations:     activity.Authorizations,
×
879
                RelatedPermissions: activity.RelatedPermissions,
×
880
                Type:               activity.Type,
×
881
        }
×
882

×
UNCOV
883
        if !activity.Timestamp.IsZero() {
×
884
                activityProto.Timestamp = timestamppb.New(activity.Timestamp)
×
885
        }
×
886

887
        if activity.Metadata != nil {
×
888
                metadataStruct, err := structpb.NewStruct(activity.Metadata)
×
889
                if err != nil {
×
890
                        return nil, fmt.Errorf("parsing metadata: %w", err)
×
891
                }
×
892
                activityProto.Metadata = metadataStruct
×
893
        }
894

895
        if !activity.CreatedAt.IsZero() {
×
896
                activityProto.CreatedAt = timestamppb.New(activity.CreatedAt)
×
897
        }
×
898

899
        if activity.Provider != nil {
×
900
                providerProto, err := a.ToProviderProto(activity.Provider)
×
901
                if err != nil {
×
902
                        return nil, fmt.Errorf("parsing provider: %w", err)
×
903
                }
×
904
                activityProto.Provider = providerProto
×
905
        }
906

907
        if activity.Resource != nil {
×
908
                resourceProto, err := a.ToResourceProto(activity.Resource)
×
909
                if err != nil {
×
910
                        return nil, fmt.Errorf("parsing resource: %w", err)
×
911
                }
×
912
                activityProto.Resource = resourceProto
×
913
        }
914

915
        return activityProto, nil
×
916
}
917

918
func (a *adapter) ToCommentProto(c *domain.Comment) *guardianv1beta1.AppealComment {
3✔
919
        if c == nil {
3✔
920
                return nil
×
921
        }
×
922

923
        commentProto := &guardianv1beta1.AppealComment{
3✔
924
                Id:        c.ID,
3✔
925
                AppealId:  c.ParentID,
3✔
926
                CreatedBy: c.CreatedBy,
3✔
927
                Body:      c.Body,
3✔
928
        }
3✔
929

3✔
930
        if !c.CreatedAt.IsZero() {
6✔
931
                commentProto.CreatedAt = timestamppb.New(c.CreatedAt)
3✔
932
        }
3✔
933
        if !c.UpdatedAt.IsZero() {
6✔
934
                commentProto.UpdatedAt = timestamppb.New(c.UpdatedAt)
3✔
935
        }
3✔
936

937
        return commentProto
3✔
938
}
939

940
func (a *adapter) fromConditionProto(c *guardianv1beta1.Condition) *domain.Condition {
×
941
        if c == nil {
×
942
                return nil
×
943
        }
×
944

945
        var match *domain.MatchCondition
×
946
        if c.GetMatch() != nil {
×
947
                match = &domain.MatchCondition{
×
948
                        Eq: c.GetMatch().GetEq(),
×
949
                }
×
950
        }
×
951

952
        return &domain.Condition{
×
953
                Field: c.GetField(),
×
954
                Match: match,
×
955
        }
×
956
}
957

958
func (a *adapter) ToAppealActivityProto(e *domain.Event) (*guardianv1beta1.AppealActivity, error) {
×
959
        if e == nil {
×
960
                return nil, nil
×
961
        }
×
962

963
        activityProto := &guardianv1beta1.AppealActivity{
×
964
                AppealId:  e.ParentID,
×
965
                Timestamp: timestamppb.New(e.Timestamp),
×
966
                Type:      e.Type,
×
967
                Actor:     e.Actor,
×
968
        }
×
969

×
970
        if e.Data != nil {
×
971
                data, err := structpb.NewStruct(e.Data)
×
972
                if err != nil {
×
973
                        return nil, err
×
974
                }
×
975
                activityProto.Data = data
×
976
        }
977
        return activityProto, nil
×
978
}
979

980
func (a *adapter) toConditionProto(c *domain.Condition) (*guardianv1beta1.Condition, error) {
×
981
        if c == nil {
×
982
                return nil, nil
×
983
        }
×
984

985
        var match *guardianv1beta1.Condition_MatchCondition
×
986
        if c.Match != nil {
×
987
                eq, err := structpb.NewValue(c.Match.Eq)
×
988
                if err != nil {
×
989
                        return nil, err
×
990
                }
×
991

992
                match = &guardianv1beta1.Condition_MatchCondition{
×
993
                        Eq: eq,
×
994
                }
×
995
        }
996

997
        return &guardianv1beta1.Condition{
×
998
                Field: c.Field,
×
999
                Match: match,
×
1000
        }, nil
×
1001
}
1002

1003
func (a *adapter) fromAppealOptionsProto(o *guardianv1beta1.AppealOptions) *domain.AppealOptions {
2✔
1004
        if o == nil {
3✔
1005
                return nil
1✔
1006
        }
1✔
1007

1008
        options := &domain.AppealOptions{
1✔
1009
                Duration: o.GetDuration(),
1✔
1010
        }
1✔
1011

1✔
1012
        if o.GetExpirationDate() != nil {
1✔
1013
                expDate := o.GetExpirationDate().AsTime()
×
1014
                options.ExpirationDate = &expDate
×
1015
        }
×
1016

1017
        return options
1✔
1018
}
1019

1020
func (a *adapter) toAppealOptionsProto(o *domain.AppealOptions) *guardianv1beta1.AppealOptions {
27✔
1021
        if o == nil {
43✔
1022
                return nil
16✔
1023
        }
16✔
1024

1025
        optionsProto := &guardianv1beta1.AppealOptions{
11✔
1026
                Duration: o.Duration,
11✔
1027
        }
11✔
1028

11✔
1029
        if o.ExpirationDate != nil {
19✔
1030
                optionsProto.ExpirationDate = timestamppb.New(*o.ExpirationDate)
8✔
1031
        }
8✔
1032

1033
        return optionsProto
11✔
1034
}
1035

1036
func (a *adapter) fromPolicyConfigProto(c *guardianv1beta1.PolicyConfig) *domain.PolicyConfig {
4✔
1037
        if c == nil {
4✔
1038
                return nil
×
1039
        }
×
1040

1041
        return &domain.PolicyConfig{
4✔
1042
                ID:      c.GetId(),
4✔
1043
                Version: int(c.GetVersion()),
4✔
1044
        }
4✔
1045
}
1046

1047
func (a *adapter) toPolicyConfigProto(c *domain.PolicyConfig) *guardianv1beta1.PolicyConfig {
8✔
1048
        if c == nil {
8✔
1049
                return nil
×
1050
        }
×
1051

1052
        return &guardianv1beta1.PolicyConfig{
8✔
1053
                Id:      c.ID,
8✔
1054
                Version: int32(c.Version),
8✔
1055
        }
8✔
1056
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc