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

goto / guardian / 21750572062

06 Feb 2026 12:27PM UTC coverage: 65.983% (+0.007%) from 65.976%
21750572062

Pull #269

github

Lifosmin Simon
chore: validation fix
Pull Request #269: chore: validation fix

10 of 14 new or added lines in 1 file covered. (71.43%)

117 existing lines in 2 files now uncovered.

13025 of 19740 relevant lines covered (65.98%)

5.37 hits per line

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

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

3
import (
4
        "encoding/json"
5
        "fmt"
6
        "time"
7

8
        "github.com/mitchellh/mapstructure"
9
        "google.golang.org/protobuf/types/known/structpb"
10
        "google.golang.org/protobuf/types/known/timestamppb"
11

12
        guardianv1beta1 "github.com/goto/guardian/api/proto/gotocompany/guardian/v1beta1"
13
        "github.com/goto/guardian/domain"
14
)
15

16
type adapter struct{}
17

18
func NewAdapter() *adapter {
156✔
19
        return &adapter{}
156✔
20
}
156✔
21

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

×
29
        if p.GetConfig() != nil {
×
30
                provider.Config = a.FromProviderConfigProto(p.GetConfig())
×
UNCOV
31
        }
×
32

33
        if p.GetCreatedAt() != nil {
×
34
                provider.CreatedAt = p.GetCreatedAt().AsTime()
×
35
        }
×
36
        if p.GetUpdatedAt() != nil {
×
37
                provider.UpdatedAt = p.GetUpdatedAt().AsTime()
×
UNCOV
38
        }
×
39

UNCOV
40
        return provider, nil
×
41
}
42

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

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

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

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

77
                                roles = append(roles, role)
2✔
78
                        }
79

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

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

103
        if pc.GetAllowedAccountTypes() != nil {
8✔
104
                providerConfig.AllowedAccountTypes = pc.GetAllowedAccountTypes()
2✔
105
        }
2✔
106

107
        if pc.GetPolicies() != nil {
8✔
108
                policies := []*domain.ProviderPolicy{}
2✔
109
                for _, p := range pc.GetPolicies() {
4✔
110
                        policies = append(policies, &domain.ProviderPolicy{
2✔
111
                                When:   p.GetWhen(),
2✔
112
                                Policy: p.GetPolicy(),
2✔
113
                        })
2✔
114
                }
2✔
115
                providerConfig.Policies = policies
2✔
116
        }
117

118
        return providerConfig
6✔
119
}
120

121
func (a *adapter) ToProviderProto(p *domain.Provider) (*guardianv1beta1.Provider, error) {
12✔
122
        providerProto := &guardianv1beta1.Provider{
12✔
123
                Id:   p.ID,
12✔
124
                Type: p.Type,
12✔
125
                Urn:  p.URN,
12✔
126
        }
12✔
127

12✔
128
        if p.Config != nil {
24✔
129
                config, err := a.ToProviderConfigProto(p.Config)
12✔
130
                if err != nil {
16✔
131
                        return nil, err
4✔
132
                }
4✔
133
                providerProto.Config = config
8✔
134
        }
135

136
        if !p.CreatedAt.IsZero() {
16✔
137
                providerProto.CreatedAt = timestamppb.New(p.CreatedAt)
8✔
138
        }
8✔
139
        if !p.UpdatedAt.IsZero() {
16✔
140
                providerProto.UpdatedAt = timestamppb.New(p.UpdatedAt)
8✔
141
        }
8✔
142

143
        return providerProto, nil
8✔
144
}
145

146
func (a *adapter) ToProviderConfigProto(pc *domain.ProviderConfig) (*guardianv1beta1.ProviderConfig, error) {
12✔
147
        providerConfigProto := &guardianv1beta1.ProviderConfig{
12✔
148
                Type:   pc.Type,
12✔
149
                Urn:    pc.URN,
12✔
150
                Labels: pc.Labels,
12✔
151
        }
12✔
152

12✔
153
        if pc.Credentials != nil {
15✔
154
                credentials, err := structpb.NewValue(pc.Credentials)
3✔
155
                if err != nil {
6✔
156
                        return nil, err
3✔
157
                }
3✔
UNCOV
158
                providerConfigProto.Credentials = credentials
×
159
        }
160

161
        if pc.Appeal != nil {
15✔
162
                providerConfigProto.Appeal = &guardianv1beta1.ProviderConfig_AppealConfig{
6✔
163
                        AllowPermanentAccess:         pc.Appeal.AllowPermanentAccess,
6✔
164
                        AllowActiveAccessExtensionIn: pc.Appeal.AllowActiveAccessExtensionIn,
6✔
165
                }
6✔
166
        }
6✔
167

168
        if pc.Resources != nil {
18✔
169
                resources := []*guardianv1beta1.ProviderConfig_ResourceConfig{}
9✔
170
                for _, rc := range pc.Resources {
18✔
171
                        roles := []*guardianv1beta1.Role{}
9✔
172
                        for _, role := range rc.Roles {
18✔
173
                                roleProto, err := a.ToRole(role)
9✔
174
                                if err != nil {
10✔
175
                                        return nil, err
1✔
176
                                }
1✔
177
                                roles = append(roles, roleProto)
8✔
178
                        }
179

180
                        resources = append(resources, &guardianv1beta1.ProviderConfig_ResourceConfig{
8✔
181
                                Type:   rc.Type,
8✔
182
                                Policy: a.toPolicyConfigProto(rc.Policy),
8✔
183
                                Roles:  roles,
8✔
184
                        })
8✔
185
                }
186
                providerConfigProto.Resources = resources
8✔
187
        }
188

189
        if pc.Parameters != nil {
9✔
190
                parameters := []*guardianv1beta1.ProviderConfig_ProviderParameter{}
1✔
191
                for _, p := range pc.Parameters {
2✔
192
                        parameters = append(parameters, &guardianv1beta1.ProviderConfig_ProviderParameter{
1✔
193
                                Key:         p.Key,
1✔
194
                                Label:       p.Label,
1✔
195
                                Required:    p.Required,
1✔
196
                                Description: p.Description,
1✔
197
                        })
1✔
198
                }
1✔
199
                providerConfigProto.Parameters = parameters
1✔
200
        }
201

202
        if pc.AllowedAccountTypes != nil {
16✔
203
                providerConfigProto.AllowedAccountTypes = pc.AllowedAccountTypes
8✔
204
        }
8✔
205

206
        if pc.Policies != nil {
12✔
207
                policies := []*guardianv1beta1.ProviderPolicy{}
4✔
208
                for _, p := range pc.Policies {
8✔
209
                        policies = append(policies, &guardianv1beta1.ProviderPolicy{
4✔
210
                                Policy: p.Policy,
4✔
211
                                When:   p.When,
4✔
212
                        })
4✔
213
                }
4✔
214
                providerConfigProto.Policies = policies
4✔
215
        }
216

217
        return providerConfigProto, nil
8✔
218
}
219

220
func (a *adapter) ToProviderTypeProto(pt domain.ProviderType) *guardianv1beta1.ProviderType {
1✔
221
        return &guardianv1beta1.ProviderType{
1✔
222
                Name:          pt.Name,
1✔
223
                ResourceTypes: pt.ResourceTypes,
1✔
224
        }
1✔
225
}
1✔
226

227
func (a *adapter) ToRole(role *domain.Role) (*guardianv1beta1.Role, error) {
11✔
228
        roleProto := &guardianv1beta1.Role{
11✔
229
                Id:          role.ID,
11✔
230
                Name:        role.Name,
11✔
231
                Description: role.Description,
11✔
232
        }
11✔
233

11✔
234
        if role.Permissions != nil {
14✔
235
                permissions := []*structpb.Value{}
3✔
236
                for _, p := range role.Permissions {
6✔
237
                        permission, err := structpb.NewValue(p)
3✔
238
                        if err != nil {
5✔
239
                                return nil, err
2✔
240
                        }
2✔
241
                        permissions = append(permissions, permission)
1✔
242
                }
243
                roleProto.Permissions = permissions
1✔
244
        }
245

246
        return roleProto, nil
9✔
247
}
248

249
func (a *adapter) FromPolicyProto(p *guardianv1beta1.Policy) *domain.Policy {
15✔
250
        policy := &domain.Policy{
15✔
251
                ID:          p.GetId(),
15✔
252
                Version:     uint(p.GetVersion()),
15✔
253
                Description: p.GetDescription(),
15✔
254
                Labels:      p.GetLabels(),
15✔
255
        }
15✔
256

15✔
257
        if p.GetSteps() != nil {
24✔
258
                var steps []*domain.Step
9✔
259
                for _, s := range p.GetSteps() {
18✔
260
                        stepProto := &domain.Step{
9✔
261
                                Name:                  s.GetName(),
9✔
262
                                Description:           s.GetDescription(),
9✔
263
                                When:                  s.GetWhen(),
9✔
264
                                Strategy:              domain.ApprovalStepStrategy(s.GetStrategy()),
9✔
265
                                RejectionReason:       s.GetRejectionReason(),
9✔
266
                                ApproveIf:             s.GetApproveIf(),
9✔
267
                                AllowFailed:           s.GetAllowFailed(),
9✔
268
                                Approvers:             s.GetApprovers(),
9✔
269
                                DontAllowSelfApproval: s.GetDontAllowSelfApproval(),
9✔
270
                                TermsAndConditions:    s.GetTermsAndConditions(),
9✔
271
                        }
9✔
272
                        if s.Details != nil {
9✔
273
                                stepProto.Details = s.GetDetails().AsMap()
×
UNCOV
274
                        }
×
275
                        steps = append(steps, stepProto)
9✔
276
                }
277
                policy.Steps = steps
9✔
278
        }
279

280
        if cs := p.GetCustomSteps(); cs != nil {
15✔
281
                policy.CustomSteps = &domain.CustomSteps{
×
282
                        Type:   cs.GetType(),
×
283
                        Config: cs.GetConfig(),
×
284
                }
×
UNCOV
285
        }
×
286

287
        if p.GetRequirements() != nil {
17✔
288
                var requirements []*domain.Requirement
2✔
289
                for _, r := range p.GetRequirements() {
4✔
290
                        var on *domain.RequirementTrigger
2✔
291
                        if r.GetOn() != nil {
4✔
292
                                var conditions []*domain.Condition
2✔
293
                                if r.GetOn().GetConditions() != nil {
2✔
294
                                        for _, c := range r.GetOn().GetConditions() {
×
295
                                                conditions = append(conditions, a.fromConditionProto(c))
×
UNCOV
296
                                        }
×
297
                                }
298

299
                                on = &domain.RequirementTrigger{
2✔
300
                                        ProviderType: r.GetOn().GetProviderType(),
2✔
301
                                        ProviderURN:  r.GetOn().GetProviderUrn(),
2✔
302
                                        ResourceType: r.GetOn().GetResourceType(),
2✔
303
                                        ResourceURN:  r.GetOn().GetResourceUrn(),
2✔
304
                                        Role:         r.GetOn().GetRole(),
2✔
305
                                        Conditions:   conditions,
2✔
306
                                        Expression:   r.GetOn().GetExpression(),
2✔
307
                                }
2✔
308
                        }
309

310
                        var additionalAppeals []*domain.AdditionalAppeal
2✔
311
                        if r.GetAppeals() != nil {
4✔
312
                                for _, aa := range r.GetAppeals() {
4✔
313
                                        var resource *domain.ResourceIdentifier
2✔
314
                                        if aa.GetResource() != nil {
4✔
315
                                                resource = &domain.ResourceIdentifier{
2✔
316
                                                        ProviderType: aa.GetResource().GetProviderType(),
2✔
317
                                                        ProviderURN:  aa.GetResource().GetProviderUrn(),
2✔
318
                                                        Type:         aa.GetResource().GetType(),
2✔
319
                                                        URN:          aa.GetResource().GetUrn(),
2✔
320
                                                        ID:           aa.GetResource().GetId(),
2✔
321
                                                }
2✔
322
                                        }
2✔
323

324
                                        additionalAppeals = append(additionalAppeals, &domain.AdditionalAppeal{
2✔
325
                                                Resource:    resource,
2✔
326
                                                Role:        aa.GetRole(),
2✔
327
                                                Options:     a.fromAppealOptionsProto(aa.GetOptions()),
2✔
328
                                                Policy:      a.fromPolicyConfigProto(aa.GetPolicy()),
2✔
329
                                                AccountType: aa.GetAccountType(),
2✔
330
                                        })
2✔
331
                                }
332
                        }
333

334
                        var postHooks []*domain.PostAppealHook
2✔
335
                        if r.GetPostHooks() != nil {
2✔
336
                                for _, hookProto := range r.GetPostHooks() {
×
337
                                        hook := a.fromPostAppealHookProto(hookProto)
×
338
                                        postHooks = append(postHooks, hook)
×
UNCOV
339
                                }
×
340
                        }
341

342
                        requirements = append(requirements, &domain.Requirement{
2✔
343
                                On:        on,
2✔
344
                                Appeals:   additionalAppeals,
2✔
345
                                PostHooks: postHooks,
2✔
346
                        })
2✔
347
                        policy.Requirements = requirements
2✔
348
                }
349
        }
350

351
        if p.GetIam() != nil {
17✔
352
                policy.IAM = &domain.IAMConfig{
2✔
353
                        Provider: domain.IAMProviderType(p.GetIam().GetProvider()),
2✔
354
                        Config:   p.GetIam().GetConfig().AsInterface(),
2✔
355
                        Schema:   p.GetIam().GetSchema(),
2✔
356
                }
2✔
357
        }
2✔
358

359
        if p.GetAppeal() != nil {
23✔
360
                var durationOptions []domain.AppealDurationOption
8✔
361
                var questions []domain.Question
8✔
362
                var metadataSources map[string]*domain.AppealMetadataSource
8✔
363

8✔
364
                for _, d := range p.GetAppeal().GetDurationOptions() {
12✔
365
                        option := domain.AppealDurationOption{
4✔
366
                                Name:  d.GetName(),
4✔
367
                                Value: d.GetValue(),
4✔
368
                        }
4✔
369
                        durationOptions = append(durationOptions, option)
4✔
370
                }
4✔
371
                for _, q := range p.GetAppeal().GetQuestions() {
9✔
372
                        question := domain.Question{
1✔
373
                                Key:         q.GetKey(),
1✔
374
                                Question:    q.GetQuestion(),
1✔
375
                                Required:    q.GetRequired(),
1✔
376
                                Description: q.GetDescription(),
1✔
377
                        }
1✔
378
                        questions = append(questions, question)
1✔
379
                }
1✔
380

381
                if mds := p.GetAppeal().GetMetadataSources(); mds != nil {
9✔
382
                        metadataSources = make(map[string]*domain.AppealMetadataSource)
1✔
383
                        for key, metadataCfg := range mds {
2✔
384
                                metadataSources[key] = &domain.AppealMetadataSource{
1✔
385
                                        Name:        metadataCfg.GetName(),
1✔
386
                                        Description: metadataCfg.GetDescription(),
1✔
387
                                        Type:        metadataCfg.GetType(),
1✔
388
                                        Config:      metadataCfg.GetConfig().AsInterface(),
1✔
389
                                        Value:       metadataCfg.GetValue().AsInterface(),
1✔
390
                                }
1✔
391
                        }
1✔
392
                }
393

394
                var labelingRules []domain.LabelingRule
8✔
395
                if lrs := p.GetAppeal().GetLabelingRules(); lrs != nil {
11✔
396
                        for _, lr := range lrs {
7✔
397
                                rule := domain.LabelingRule{
4✔
398
                                        RuleName:     lr.GetRuleName(),
4✔
399
                                        Description:  lr.GetDescription(),
4✔
400
                                        When:         lr.GetWhen(),
4✔
401
                                        Labels:       lr.GetLabels(),
4✔
402
                                        Priority:     int(lr.GetPriority()),
4✔
403
                                        AllowFailure: lr.GetAllowFailure(),
4✔
404
                                }
4✔
405

4✔
406
                                if lm := lr.GetLabelMetadata(); lm != nil {
5✔
407
                                        rule.LabelMetadata = make(map[string]*domain.LabelMetadataConfig)
1✔
408
                                        for key, metadata := range lm {
2✔
409
                                                rule.LabelMetadata[key] = &domain.LabelMetadataConfig{
1✔
410
                                                        Category:   metadata.GetCategory(),
1✔
411
                                                        Attributes: metadata.GetAttributes().AsMap(),
1✔
412
                                                }
1✔
413
                                        }
1✔
414
                                }
415

416
                                labelingRules = append(labelingRules, rule)
4✔
417
                        }
418
                }
419

420
                var userLabelConfig *domain.UserLabelConfig
8✔
421
                if mlc := p.GetAppeal().GetUserLabelConfig(); mlc != nil {
10✔
422
                        userLabelConfig = &domain.UserLabelConfig{
2✔
423
                                AllowUserLabels: mlc.GetAllowUserLabels(),
2✔
424
                                AllowedKeys:     mlc.GetAllowedKeys(),
2✔
425
                                RequiredKeys:    mlc.GetRequiredKeys(),
2✔
426
                                MaxLabels:       int(mlc.GetMaxLabels()),
2✔
427
                                KeyPattern:      mlc.GetKeyPattern(),
2✔
428
                                ValuePattern:    mlc.GetValuePattern(),
2✔
429
                                AllowOverride:   mlc.GetAllowOverride(),
2✔
430
                        }
2✔
431
                }
2✔
432

433
                policy.AppealConfig = &domain.PolicyAppealConfig{
8✔
434
                        DurationOptions:              durationOptions,
8✔
435
                        AllowOnBehalf:                p.GetAppeal().GetAllowOnBehalf(),
8✔
436
                        Questions:                    questions,
8✔
437
                        AllowPermanentAccess:         p.GetAppeal().GetAllowPermanentAccess(),
8✔
438
                        AllowActiveAccessExtensionIn: p.GetAppeal().GetAllowActiveAccessExtensionIn(),
8✔
439
                        AllowCreatorDetailsFailure:   p.GetAppeal().GetAllowCreatorDetailsFailure(),
8✔
440
                        MetadataSources:              metadataSources,
8✔
441
                        TermsAndConditions:           p.GetAppeal().GetTermsAndConditions(),
8✔
442
                        LabelingRules:                labelingRules,
8✔
443
                        UserLabelConfig:              userLabelConfig,
8✔
444
                }
8✔
445
        }
446

447
        if p.GetCreatedAt() != nil {
15✔
448
                policy.CreatedAt = p.GetCreatedAt().AsTime()
×
UNCOV
449
        }
×
450
        if p.GetUpdatedAt() != nil {
15✔
451
                policy.UpdatedAt = p.GetUpdatedAt().AsTime()
×
UNCOV
452
        }
×
453

454
        return policy
15✔
455
}
456

457
func (a *adapter) ToPolicyProto(p *domain.Policy) (*guardianv1beta1.Policy, error) {
26✔
458
        policyProto := &guardianv1beta1.Policy{
26✔
459
                Id:          p.ID,
26✔
460
                Version:     uint32(p.Version),
26✔
461
                Description: p.Description,
26✔
462
                Labels:      p.Labels,
26✔
463
        }
26✔
464

26✔
465
        if p.Steps != nil {
38✔
466
                var steps []*guardianv1beta1.Policy_ApprovalStep
12✔
467
                for _, s := range p.Steps {
24✔
468
                        approvalStepProto := &guardianv1beta1.Policy_ApprovalStep{
12✔
469
                                Name:                  s.Name,
12✔
470
                                Description:           s.Description,
12✔
471
                                When:                  s.When,
12✔
472
                                Strategy:              string(s.Strategy),
12✔
473
                                RejectionReason:       s.RejectionReason,
12✔
474
                                ApproveIf:             s.ApproveIf,
12✔
475
                                AllowFailed:           s.AllowFailed,
12✔
476
                                Approvers:             s.Approvers,
12✔
477
                                DontAllowSelfApproval: s.DontAllowSelfApproval,
12✔
478
                                TermsAndConditions:    s.TermsAndConditions,
12✔
479
                        }
12✔
480
                        if s.Details != nil {
12✔
481
                                details, err := structpb.NewStruct(s.Details)
×
482
                                if err != nil {
×
483
                                        return nil, err
×
484
                                }
×
UNCOV
485
                                approvalStepProto.Details = details
×
486
                        }
487
                        steps = append(steps, approvalStepProto)
12✔
488
                }
489
                policyProto.Steps = steps
12✔
490
        }
491

492
        if p.Requirements != nil {
30✔
493
                var requirements []*guardianv1beta1.Policy_Requirement
4✔
494
                for _, r := range p.Requirements {
8✔
495
                        var on *guardianv1beta1.Policy_Requirement_RequirementTrigger
4✔
496
                        if r.On != nil {
8✔
497
                                var conditions []*guardianv1beta1.Condition
4✔
498
                                if r.On.Conditions != nil {
4✔
499
                                        for _, c := range r.On.Conditions {
×
500
                                                condition, err := a.toConditionProto(c)
×
501
                                                if err != nil {
×
502
                                                        return nil, err
×
503
                                                }
×
UNCOV
504
                                                conditions = append(conditions, condition)
×
505
                                        }
506
                                }
507

508
                                on = &guardianv1beta1.Policy_Requirement_RequirementTrigger{
4✔
509
                                        ProviderType: r.On.ProviderType,
4✔
510
                                        ProviderUrn:  r.On.ProviderURN,
4✔
511
                                        ResourceType: r.On.ResourceType,
4✔
512
                                        ResourceUrn:  r.On.ResourceURN,
4✔
513
                                        Role:         r.On.Role,
4✔
514
                                        Conditions:   conditions,
4✔
515
                                        Expression:   r.On.Expression,
4✔
516
                                }
4✔
517
                        }
518

519
                        var additionalAppeals []*guardianv1beta1.Policy_Requirement_AdditionalAppeal
4✔
520
                        if r.Appeals != nil {
8✔
521
                                for _, aa := range r.Appeals {
8✔
522
                                        var resource *guardianv1beta1.Policy_Requirement_AdditionalAppeal_ResourceIdentifier
4✔
523
                                        if aa.Resource != nil {
8✔
524
                                                resource = &guardianv1beta1.Policy_Requirement_AdditionalAppeal_ResourceIdentifier{
4✔
525
                                                        ProviderType: aa.Resource.ProviderType,
4✔
526
                                                        ProviderUrn:  aa.Resource.ProviderURN,
4✔
527
                                                        Type:         aa.Resource.Type,
4✔
528
                                                        Urn:          aa.Resource.URN,
4✔
529
                                                        Id:           aa.Resource.ID,
4✔
530
                                                }
4✔
531
                                        }
4✔
532

533
                                        additionalAppeals = append(additionalAppeals, &guardianv1beta1.Policy_Requirement_AdditionalAppeal{
4✔
534
                                                Resource: resource,
4✔
535
                                                Role:     aa.Role,
4✔
536
                                                Options:  a.toAppealOptionsProto(aa.Options),
4✔
537
                                                Policy:   a.toPolicyConfigProto(aa.Policy),
4✔
538
                                        })
4✔
539
                                }
540
                        }
541

542
                        var postHooks []*guardianv1beta1.Policy_Requirement_PostAppealHook
4✔
543
                        if r.PostHooks != nil {
4✔
544
                                for _, hook := range r.PostHooks {
×
545
                                        hookProto, err := a.toPostAppealHookProto(hook)
×
546
                                        if err != nil {
×
547
                                                return nil, fmt.Errorf("converting post hook %q: %w", hook.Name, err)
×
548
                                        }
×
UNCOV
549
                                        postHooks = append(postHooks, hookProto)
×
550
                                }
551
                        }
552

553
                        requirements = append(requirements, &guardianv1beta1.Policy_Requirement{
4✔
554
                                On:        on,
4✔
555
                                Appeals:   additionalAppeals,
4✔
556
                                PostHooks: postHooks,
4✔
557
                        })
4✔
558
                        policyProto.Requirements = requirements
4✔
559
                }
560
        }
561

562
        if p.HasIAMConfig() {
33✔
563
                config, err := structpb.NewValue(p.IAM.Config)
7✔
564
                if err != nil {
9✔
565
                        return nil, err
2✔
566
                }
2✔
567

568
                policyProto.Iam = &guardianv1beta1.Policy_IAM{
5✔
569
                        Provider: string(p.IAM.Provider),
5✔
570
                        Config:   config,
5✔
571
                        Schema:   p.IAM.Schema,
5✔
572
                }
5✔
573
        }
574

575
        appealConfig, err := a.ToPolicyAppealConfigProto(p)
24✔
576
        if err != nil {
24✔
577
                return nil, fmt.Errorf("failed to convert appeal config to proto: %w", err)
×
UNCOV
578
        }
×
579
        policyProto.Appeal = appealConfig
24✔
580

24✔
581
        if !p.CreatedAt.IsZero() {
28✔
582
                policyProto.CreatedAt = timestamppb.New(p.CreatedAt)
4✔
583
        }
4✔
584
        if !p.UpdatedAt.IsZero() {
28✔
585
                policyProto.UpdatedAt = timestamppb.New(p.UpdatedAt)
4✔
586
        }
4✔
587

588
        return policyProto, nil
24✔
589
}
590

591
func (a *adapter) ToPolicyAppealConfigProto(p *domain.Policy) (*guardianv1beta1.PolicyAppealConfig, error) {
25✔
592
        if p.AppealConfig == nil {
37✔
593
                return nil, nil
12✔
594
        }
12✔
595

596
        policyAppealConfigProto := &guardianv1beta1.PolicyAppealConfig{}
13✔
597
        var durationOptions []*guardianv1beta1.PolicyAppealConfig_DurationOptions
13✔
598
        if p.AppealConfig.DurationOptions != nil {
17✔
599
                for _, d := range p.AppealConfig.DurationOptions {
12✔
600
                        durationOptions = append(durationOptions, &guardianv1beta1.PolicyAppealConfig_DurationOptions{
8✔
601
                                Name:  d.Name,
8✔
602
                                Value: d.Value,
8✔
603
                        })
8✔
604
                }
8✔
605
        }
606
        policyAppealConfigProto.DurationOptions = durationOptions
13✔
607
        policyAppealConfigProto.AllowOnBehalf = p.AppealConfig.AllowOnBehalf
13✔
608
        policyAppealConfigProto.AllowPermanentAccess = p.AppealConfig.AllowPermanentAccess
13✔
609
        policyAppealConfigProto.AllowActiveAccessExtensionIn = p.AppealConfig.AllowActiveAccessExtensionIn
13✔
610
        policyAppealConfigProto.AllowCreatorDetailsFailure = p.AppealConfig.AllowCreatorDetailsFailure
13✔
611

13✔
612
        for _, q := range p.AppealConfig.Questions {
14✔
613
                policyAppealConfigProto.Questions = append(policyAppealConfigProto.Questions, &guardianv1beta1.PolicyAppealConfig_Question{
1✔
614
                        Key:         q.Key,
1✔
615
                        Question:    q.Question,
1✔
616
                        Required:    q.Required,
1✔
617
                        Description: q.Description,
1✔
618
                })
1✔
619
        }
1✔
620

621
        for key, metadataSource := range p.AppealConfig.MetadataSources {
16✔
622
                if policyAppealConfigProto.MetadataSources == nil {
6✔
623
                        policyAppealConfigProto.MetadataSources = make(map[string]*guardianv1beta1.PolicyAppealConfig_MetadataSource)
3✔
624
                }
3✔
625
                policyAppealConfigProto.MetadataSources[key] = &guardianv1beta1.PolicyAppealConfig_MetadataSource{
3✔
626
                        Name:        metadataSource.Name,
3✔
627
                        Description: metadataSource.Description,
3✔
628
                        Type:        metadataSource.Type,
3✔
629
                }
3✔
630

3✔
631
                if metadataSource.Config != nil {
4✔
632
                        cfg, err := structpb.NewValue(metadataSource.Config)
1✔
633
                        if err != nil {
1✔
634
                                return nil, err
×
UNCOV
635
                        }
×
636
                        policyAppealConfigProto.MetadataSources[key].Config = cfg
1✔
637
                }
638
                if metadataSource.Value != nil {
4✔
639
                        value, err := structpb.NewValue(metadataSource.Value)
1✔
640
                        if err != nil {
1✔
641
                                return nil, err
×
UNCOV
642
                        }
×
643
                        policyAppealConfigProto.MetadataSources[key].Value = value
1✔
644
                }
645
        }
646
        policyAppealConfigProto.TermsAndConditions = p.AppealConfig.TermsAndConditions
13✔
647

13✔
648
        if p.AppealConfig.LabelingRules != nil {
17✔
649
                for _, rule := range p.AppealConfig.LabelingRules {
8✔
650
                        ruleProto := &guardianv1beta1.LabelingRule{
4✔
651
                                RuleName:     rule.RuleName,
4✔
652
                                Description:  rule.Description,
4✔
653
                                When:         rule.When,
4✔
654
                                Labels:       rule.Labels,
4✔
655
                                Priority:     int32(rule.Priority),
4✔
656
                                AllowFailure: rule.AllowFailure,
4✔
657
                        }
4✔
658

4✔
659
                        if rule.LabelMetadata != nil {
5✔
660
                                ruleProto.LabelMetadata = make(map[string]*guardianv1beta1.LabelMetadataConfig)
1✔
661
                                for key, metadata := range rule.LabelMetadata {
2✔
662
                                        attrs, err := structpb.NewStruct(metadata.Attributes)
1✔
663
                                        if err != nil {
1✔
664
                                                return nil, fmt.Errorf("converting label metadata attributes: %w", err)
×
UNCOV
665
                                        }
×
666
                                        ruleProto.LabelMetadata[key] = &guardianv1beta1.LabelMetadataConfig{
1✔
667
                                                Category:   metadata.Category,
1✔
668
                                                Attributes: attrs,
1✔
669
                                        }
1✔
670
                                }
671
                        }
672

673
                        policyAppealConfigProto.LabelingRules = append(policyAppealConfigProto.LabelingRules, ruleProto)
4✔
674
                }
675
        }
676

677
        if p.AppealConfig.UserLabelConfig != nil {
15✔
678
                policyAppealConfigProto.UserLabelConfig = &guardianv1beta1.UserLabelConfig{
2✔
679
                        AllowUserLabels: p.AppealConfig.UserLabelConfig.AllowUserLabels,
2✔
680
                        AllowedKeys:     p.AppealConfig.UserLabelConfig.AllowedKeys,
2✔
681
                        RequiredKeys:    p.AppealConfig.UserLabelConfig.RequiredKeys,
2✔
682
                        MaxLabels:       int32(p.AppealConfig.UserLabelConfig.MaxLabels),
2✔
683
                        KeyPattern:      p.AppealConfig.UserLabelConfig.KeyPattern,
2✔
684
                        ValuePattern:    p.AppealConfig.UserLabelConfig.ValuePattern,
2✔
685
                        AllowOverride:   p.AppealConfig.UserLabelConfig.AllowOverride,
2✔
686
                }
2✔
687
        }
2✔
688

689
        return policyAppealConfigProto, nil
13✔
690
}
691

692
func (a *adapter) FromResourceProto(r *guardianv1beta1.Resource) *domain.Resource {
4✔
693
        resource := &domain.Resource{
4✔
694
                ID:           r.GetId(),
4✔
695
                ProviderType: r.GetProviderType(),
4✔
696
                ProviderURN:  r.GetProviderUrn(),
4✔
697
                Type:         r.GetType(),
4✔
698
                URN:          r.GetUrn(),
4✔
699
                Name:         r.GetName(),
4✔
700
                Labels:       r.GetLabels(),
4✔
701
                IsDeleted:    r.GetIsDeleted(),
4✔
702
                GroupID:      r.GetGroupId(),
4✔
703
                GroupType:    r.GetGroupType(),
4✔
704
        }
4✔
705

4✔
706
        if r.GetParentId() != "" {
4✔
707
                id := r.GetParentId()
×
708
                resource.ParentID = &id
×
UNCOV
709
        }
×
710

711
        if r.GetChildren() != nil {
4✔
712
                for _, c := range r.GetChildren() {
×
713
                        resource.Children = append(resource.Children, a.FromResourceProto(c))
×
UNCOV
714
                }
×
715
        }
716

717
        if r.GetDetails() != nil {
4✔
718
                resource.Details = r.GetDetails().AsMap()
×
UNCOV
719
        }
×
720

721
        if r.GetCreatedAt() != nil {
4✔
722
                resource.CreatedAt = r.GetCreatedAt().AsTime()
×
UNCOV
723
        }
×
724
        if r.GetUpdatedAt() != nil {
4✔
725
                resource.UpdatedAt = r.GetUpdatedAt().AsTime()
×
UNCOV
726
        }
×
727

728
        return resource
4✔
729
}
730

731
func (a *adapter) ToResourceProto(r *domain.Resource) (*guardianv1beta1.Resource, error) {
29✔
732
        resourceProto := &guardianv1beta1.Resource{
29✔
733
                Id:           r.ID,
29✔
734
                ProviderType: r.ProviderType,
29✔
735
                ProviderUrn:  r.ProviderURN,
29✔
736
                Type:         r.Type,
29✔
737
                Urn:          r.URN,
29✔
738
                Name:         r.Name,
29✔
739
                Labels:       r.Labels,
29✔
740
                IsDeleted:    r.IsDeleted,
29✔
741
                GroupId:      r.GroupID,
29✔
742
                GroupType:    r.GroupType,
29✔
743
        }
29✔
744

29✔
745
        if r.ParentID != nil {
29✔
746
                resourceProto.ParentId = *r.ParentID
×
UNCOV
747
        }
×
748

749
        if r.GlobalURN != "" {
30✔
750
                resourceProto.GlobalUrn = r.GlobalURN
1✔
751
        }
1✔
752

753
        if r.Children != nil {
29✔
754
                for _, c := range r.Children {
×
755
                        childProto, err := a.ToResourceProto(c)
×
756
                        if err != nil {
×
757
                                return nil, fmt.Errorf("failed to convert child resource to proto %q: %w", c.ID, err)
×
758
                        }
×
UNCOV
759
                        resourceProto.Children = append(resourceProto.Children, childProto)
×
760
                }
761
        }
762

763
        if r.Details != nil {
37✔
764
                details, err := structpb.NewStruct(r.Details)
8✔
765
                if err != nil {
14✔
766
                        return nil, err
6✔
767
                }
6✔
768
                resourceProto.Details = details
2✔
769
        }
770

771
        if !r.CreatedAt.IsZero() {
26✔
772
                resourceProto.CreatedAt = timestamppb.New(r.CreatedAt)
3✔
773
        }
3✔
774
        if !r.UpdatedAt.IsZero() {
26✔
775
                resourceProto.UpdatedAt = timestamppb.New(r.UpdatedAt)
3✔
776
        }
3✔
777

778
        return resourceProto, nil
23✔
779
}
780

781
func (a *adapter) ToAppealProto(appeal *domain.Appeal) (*guardianv1beta1.Appeal, error) {
37✔
782
        appealProto := &guardianv1beta1.Appeal{
37✔
783
                Id:             appeal.ID,
37✔
784
                ResourceId:     appeal.ResourceID,
37✔
785
                PolicyId:       appeal.PolicyID,
37✔
786
                PolicyVersion:  uint32(appeal.PolicyVersion),
37✔
787
                Status:         appeal.Status,
37✔
788
                AccountId:      appeal.AccountID,
37✔
789
                AccountType:    appeal.AccountType,
37✔
790
                GroupId:        appeal.GroupID,
37✔
791
                GroupType:      appeal.GroupType,
37✔
792
                CreatedBy:      appeal.CreatedBy,
37✔
793
                Role:           appeal.Role,
37✔
794
                Permissions:    appeal.Permissions,
37✔
795
                Options:        a.toAppealOptionsProto(appeal.Options),
37✔
796
                Labels:         appeal.Labels,
37✔
797
                LabelsMetadata: a.toLabelMetadataProto(appeal.LabelsMetadata),
37✔
798
                Description:    appeal.Description,
37✔
799
                Revision:       uint32(appeal.Revision),
37✔
800
        }
37✔
801

37✔
802
        if appeal.Resource != nil {
52✔
803
                r, err := a.ToResourceProto(appeal.Resource)
15✔
804
                if err != nil {
15✔
805
                        return nil, err
×
UNCOV
806
                }
×
807
                appealProto.Resource = r
15✔
808
        }
809

810
        if appeal.Creator != nil {
56✔
811
                creator, err := structpb.NewValue(appeal.Creator)
19✔
812
                if err != nil {
29✔
813
                        return nil, err
10✔
814
                }
10✔
815
                appealProto.Creator = creator
9✔
816
        }
817

818
        if appeal.Approvals != nil {
36✔
819
                approvals := []*guardianv1beta1.Approval{}
9✔
820
                for _, approval := range appeal.Approvals {
18✔
821
                        approvalProto, err := a.ToApprovalProto(approval)
9✔
822
                        if err != nil {
9✔
823
                                return nil, err
×
UNCOV
824
                        }
×
825

826
                        approvals = append(approvals, approvalProto)
9✔
827
                }
828
                appealProto.Approvals = approvals
9✔
829
        }
830

831
        if appeal.Details != nil {
40✔
832
                details, err := structpb.NewStruct(appeal.Details)
13✔
833
                if err != nil {
14✔
834
                        return nil, err
1✔
835
                }
1✔
836
                appealProto.Details = details
12✔
837
        }
838

839
        if !appeal.CreatedAt.IsZero() {
42✔
840
                appealProto.CreatedAt = timestamppb.New(appeal.CreatedAt)
16✔
841
        }
16✔
842
        if !appeal.UpdatedAt.IsZero() {
42✔
843
                appealProto.UpdatedAt = timestamppb.New(appeal.UpdatedAt)
16✔
844
        }
16✔
845

846
        grantProto, err := a.ToGrantProto(appeal.Grant)
26✔
847
        if err != nil {
26✔
848
                return nil, fmt.Errorf("parsing grant: %w", err)
×
UNCOV
849
        }
×
850
        appealProto.Grant = grantProto
26✔
851

26✔
852
        return appealProto, nil
26✔
853
}
854

855
func (a *adapter) FromCreateAppealProto(ca *guardianv1beta1.CreateAppealRequest, authenticatedUser string) ([]*domain.Appeal, error) {
14✔
856
        var appeals []*domain.Appeal
14✔
857

14✔
858
        for _, r := range ca.GetResources() {
27✔
859
                appeal := &domain.Appeal{
13✔
860
                        AccountID:   ca.GetAccountId(),
13✔
861
                        AccountType: ca.GetAccountType(),
13✔
862
                        GroupID:     r.GetGroupId(),
13✔
863
                        GroupType:   r.GetGroupType(),
13✔
864
                        CreatedBy:   authenticatedUser,
13✔
865
                        ResourceID:  r.GetId(),
13✔
866
                        Role:        r.GetRole(),
13✔
867
                        Description: ca.GetDescription(),
13✔
868
                        UserLabels:  ca.GetUserLabels(),
13✔
869
                }
13✔
870

13✔
871
                if r.GetOptions() != nil {
19✔
872
                        var options *domain.AppealOptions
6✔
873
                        jsonBytes, err := json.Marshal(r.GetOptions().AsMap())
6✔
874
                        if err != nil {
6✔
875
                                return nil, err
×
UNCOV
876
                        }
×
877

878
                        if err := json.Unmarshal(jsonBytes, &options); err != nil {
7✔
879
                                return nil, err
1✔
880
                        }
1✔
881
                        appeal.Options = options
5✔
882
                }
883

884
                if r.GetDetails() != nil {
14✔
885
                        appeal.Details = r.GetDetails().AsMap()
2✔
886
                }
2✔
887

888
                appeals = append(appeals, appeal)
12✔
889
        }
890

891
        return appeals, nil
13✔
892
}
893

894
func (a *adapter) FromPatchAppealProto(ua *guardianv1beta1.PatchAppealRequest, authenticatedUser string) (*domain.Appeal, error) {
2✔
895
        appeal := &domain.Appeal{
2✔
896
                ID:          ua.GetId(),
2✔
897
                AccountID:   ua.GetAccountId(),
2✔
898
                AccountType: ua.GetAccountType(),
2✔
899
                CreatedBy:   authenticatedUser,
2✔
900
                ResourceID:  ua.GetResourceId(),
2✔
901
                Role:        ua.GetRole(),
2✔
902
                Description: ua.GetDescription(),
2✔
903
        }
2✔
904

2✔
905
        if ua.GetOptions() != nil {
3✔
906
                var options *domain.AppealOptions
1✔
907
                if err := mapstructure.Decode(ua.GetOptions().AsMap(), &options); err != nil {
1✔
908
                        return nil, err
×
909
                }
×
910
                appeal.Options = options
1✔
911
        }
912

913
        if ua.GetDetails() != nil {
3✔
914
                appeal.Details = ua.GetDetails().AsMap()
1✔
915
        }
1✔
916

917
        return appeal, nil
2✔
918
}
919

920
func (a *adapter) ToApprovalProto(approval *domain.Approval) (*guardianv1beta1.Approval, error) {
13✔
921
        approvalProto := &guardianv1beta1.Approval{
13✔
922
                Id:                    approval.ID,
13✔
923
                Name:                  approval.Name,
13✔
924
                AppealId:              approval.AppealID,
13✔
925
                Status:                approval.Status,
13✔
926
                Reason:                approval.Reason,
13✔
927
                PolicyId:              approval.PolicyID,
13✔
928
                PolicyVersion:         uint32(approval.PolicyVersion),
13✔
929
                Approvers:             approval.Approvers,
13✔
930
                CreatedAt:             timestamppb.New(approval.CreatedAt),
13✔
931
                UpdatedAt:             timestamppb.New(approval.UpdatedAt),
13✔
932
                IsStale:               approval.IsStale,
13✔
933
                AppealRevision:        uint32(approval.AppealRevision),
13✔
934
                AllowFailed:           approval.AllowFailed,
13✔
935
                DontAllowSelfApproval: approval.DontAllowSelfApproval,
13✔
936
        }
13✔
937
        if approval.Details != nil {
13✔
UNCOV
938
                details, err := structpb.NewStruct(approval.Details)
×
UNCOV
939
                if err != nil {
×
UNCOV
940
                        return nil, err
×
UNCOV
941
                }
×
942
                approvalProto.Details = details
×
943
        }
944

945
        if approval.Appeal != nil {
17✔
946
                appeal, err := a.ToAppealProto(approval.Appeal)
4✔
947
                if err != nil {
6✔
948
                        return nil, err
2✔
949
                }
2✔
950
                approvalProto.Appeal = appeal
2✔
951
        }
952

953
        if approval.Actor != nil {
12✔
954
                approvalProto.Actor = *approval.Actor
1✔
955
        }
1✔
956

957
        if !approval.CreatedAt.IsZero() {
22✔
958
                approvalProto.CreatedAt = timestamppb.New(approval.CreatedAt)
11✔
959
        }
11✔
960
        if !approval.UpdatedAt.IsZero() {
22✔
961
                approvalProto.UpdatedAt = timestamppb.New(approval.UpdatedAt)
11✔
962
        }
11✔
963

964
        return approvalProto, nil
11✔
965
}
966

UNCOV
967
func (a *adapter) FromGrantProto(g *guardianv1beta1.Grant) *domain.Grant {
×
UNCOV
968
        if g == nil {
×
UNCOV
969
                return nil
×
UNCOV
970
        }
×
971

972
        grant := &domain.Grant{
×
973
                ID:                   g.GetId(),
×
974
                Status:               domain.GrantStatus(g.GetStatus()),
×
UNCOV
975
                StatusInProvider:     domain.GrantStatus(g.GetStatusInProvider()),
×
976
                AccountID:            g.GetAccountId(),
×
977
                AccountType:          g.GetAccountType(),
×
978
                GroupID:              g.GetGroupId(),
×
979
                GroupType:            g.GetGroupType(),
×
980
                ResourceID:           g.GetResourceId(),
×
981
                Role:                 g.GetRole(),
×
982
                Permissions:          g.GetPermissions(),
×
983
                AppealID:             g.GetAppealId(),
×
984
                Source:               domain.GrantSource(g.Source),
×
985
                RevokedBy:            g.GetRevokedBy(),
×
986
                RevokeReason:         g.GetRevokeReason(),
×
987
                CreatedBy:            g.GetCreatedBy(),
×
988
                Owner:                g.GetOwner(),
×
989
                Resource:             a.FromResourceProto(g.GetResource()),
×
990
                ExpirationDateReason: g.GetExpirationDateReason(),
×
991
                RestoreReason:        g.GetRestoreReason(),
×
992
                RestoredBy:           g.GetRestoredBy(),
×
993
        }
×
994

×
995
        if g.GetExpirationDate() != nil {
×
996
                t := g.GetExpirationDate().AsTime()
×
997
                grant.ExpirationDate = &t
×
998
        }
×
999
        if g.GetRevokedAt() != nil {
×
1000
                t := g.GetRevokedAt().AsTime()
×
1001
                grant.RevokedAt = &t
×
1002
        }
×
1003
        if g.GetCreatedAt() != nil {
×
1004
                grant.CreatedAt = g.GetCreatedAt().AsTime()
×
1005
        }
×
1006
        if g.GetUpdatedAt() != nil {
×
1007
                grant.UpdatedAt = g.GetUpdatedAt().AsTime()
×
1008
        }
×
1009
        if g.GetRestoredAt() != nil {
×
1010
                t := g.GetRestoredAt().AsTime()
×
1011
                grant.RestoredAt = &t
×
1012
        }
×
1013

1014
        return grant
×
1015
}
1016

1017
func (a *adapter) ToGrantProto(grant *domain.Grant) (*guardianv1beta1.Grant, error) {
42✔
1018
        if grant == nil {
69✔
1019
                return nil, nil
27✔
1020
        }
27✔
1021

1022
        grantProto := &guardianv1beta1.Grant{
15✔
1023
                Id:                   grant.ID,
15✔
1024
                Status:               string(grant.Status),
15✔
1025
                StatusInProvider:     string(grant.StatusInProvider),
15✔
1026
                AccountId:            grant.AccountID,
15✔
1027
                AccountType:          grant.AccountType,
15✔
1028
                GroupId:              grant.GroupID,
15✔
1029
                GroupType:            grant.GroupType,
15✔
1030
                ResourceId:           grant.ResourceID,
15✔
1031
                Role:                 grant.Role,
15✔
1032
                Permissions:          grant.Permissions,
15✔
1033
                IsPermanent:          grant.IsPermanent,
15✔
1034
                AppealId:             grant.AppealID,
15✔
1035
                Source:               string(grant.Source),
15✔
1036
                RevokedBy:            grant.RevokedBy,
15✔
1037
                RevokeReason:         grant.RevokeReason,
15✔
1038
                CreatedBy:            grant.CreatedBy,
15✔
1039
                Owner:                grant.Owner,
15✔
1040
                ExpirationDateReason: grant.ExpirationDateReason,
15✔
1041
                RestoreReason:        grant.RestoreReason,
15✔
1042
                RestoredBy:           grant.RestoredBy,
15✔
1043
                PendingAppealId:      grant.PendingAppealID,
15✔
1044
        }
15✔
1045

15✔
1046
        if grant.ExpirationDate != nil {
20✔
1047
                grantProto.ExpirationDate = timestamppb.New(*grant.ExpirationDate)
5✔
1048
        }
5✔
1049
        if grant.RevokedAt != nil {
19✔
1050
                grantProto.RevokedAt = timestamppb.New(*grant.RevokedAt)
4✔
1051
        }
4✔
1052
        if !grant.CreatedAt.IsZero() {
20✔
1053
                grantProto.CreatedAt = timestamppb.New(grant.CreatedAt)
5✔
1054
        }
5✔
1055
        if !grant.UpdatedAt.IsZero() {
23✔
1056
                grantProto.UpdatedAt = timestamppb.New(grant.UpdatedAt)
8✔
1057
        }
8✔
1058
        if grant.Resource != nil {
23✔
1059
                resourceProto, err := a.ToResourceProto(grant.Resource)
8✔
1060
                if err != nil {
11✔
1061
                        return nil, fmt.Errorf("parsing resource: %w", err)
3✔
1062
                }
3✔
1063
                grantProto.Resource = resourceProto
5✔
1064
        }
1065
        if grant.Appeal != nil {
18✔
1066
                appealProto, err := a.ToAppealProto(grant.Appeal)
6✔
1067
                if err != nil {
7✔
1068
                        return nil, fmt.Errorf("parsing appeal: %w", err)
1✔
1069
                }
1✔
1070
                grantProto.Appeal = appealProto
5✔
1071
        }
1072
        if grant.RestoredAt != nil {
11✔
UNCOV
1073
                grantProto.RestoredAt = timestamppb.New(*grant.RestoredAt)
×
UNCOV
1074
        }
×
1075

1076
        return grantProto, nil
11✔
1077
}
1078

1079
func (a *adapter) FromUpdateGrantRequestProto(grantUpdate *guardianv1beta1.UpdateGrantRequest) *domain.GrantUpdate {
4✔
1080
        if grantUpdate == nil {
4✔
UNCOV
1081
                return nil
×
UNCOV
1082
        }
×
1083

1084
        gu := &domain.GrantUpdate{
4✔
1085
                ID:                   grantUpdate.GetId(),
4✔
1086
                ExpirationDateReason: grantUpdate.ExpirationDateReason,
4✔
1087
        }
4✔
1088
        if grantUpdate.GetOwner() != "" {
8✔
1089
                gu.Owner = &grantUpdate.Owner
4✔
1090
        }
4✔
1091

1092
        if grantUpdate.ExpirationDate != nil {
4✔
UNCOV
1093
                expDate := grantUpdate.GetExpirationDate().AsTime()
×
UNCOV
1094
                gu.ExpirationDate = &expDate
×
UNCOV
1095
        }
×
1096

1097
        return gu
4✔
1098
}
1099

UNCOV
1100
func (a *adapter) ToActivityProto(activity *domain.Activity) (*guardianv1beta1.ProviderActivity, error) {
×
UNCOV
1101
        if activity == nil {
×
UNCOV
1102
                return nil, nil
×
UNCOV
1103
        }
×
1104

1105
        activityProto := &guardianv1beta1.ProviderActivity{
×
1106
                Id:                 activity.ID,
×
1107
                ProviderId:         activity.ProviderID,
×
UNCOV
1108
                ResourceId:         activity.ResourceID,
×
1109
                ProviderActivityId: activity.ProviderActivityID,
×
1110
                AccountType:        activity.AccountType,
×
1111
                AccountId:          activity.AccountID,
×
1112
                Authorizations:     activity.Authorizations,
×
1113
                RelatedPermissions: activity.RelatedPermissions,
×
1114
                Type:               activity.Type,
×
1115
        }
×
1116

×
1117
        if !activity.Timestamp.IsZero() {
×
1118
                activityProto.Timestamp = timestamppb.New(activity.Timestamp)
×
1119
        }
×
1120

1121
        if activity.Metadata != nil {
×
1122
                metadataStruct, err := structpb.NewStruct(activity.Metadata)
×
1123
                if err != nil {
×
UNCOV
1124
                        return nil, fmt.Errorf("parsing metadata: %w", err)
×
1125
                }
×
1126
                activityProto.Metadata = metadataStruct
×
1127
        }
1128

1129
        if !activity.CreatedAt.IsZero() {
×
1130
                activityProto.CreatedAt = timestamppb.New(activity.CreatedAt)
×
UNCOV
1131
        }
×
1132

1133
        if activity.Provider != nil {
×
1134
                providerProto, err := a.ToProviderProto(activity.Provider)
×
1135
                if err != nil {
×
UNCOV
1136
                        return nil, fmt.Errorf("parsing provider: %w", err)
×
1137
                }
×
1138
                activityProto.Provider = providerProto
×
1139
        }
1140

1141
        if activity.Resource != nil {
×
1142
                resourceProto, err := a.ToResourceProto(activity.Resource)
×
UNCOV
1143
                if err != nil {
×
UNCOV
1144
                        return nil, fmt.Errorf("parsing resource: %w", err)
×
1145
                }
×
1146
                activityProto.Resource = resourceProto
×
1147
        }
1148

1149
        return activityProto, nil
×
1150
}
1151

1152
func (a *adapter) ToCommentProto(c *domain.Comment) *guardianv1beta1.AppealComment {
3✔
1153
        if c == nil {
3✔
UNCOV
1154
                return nil
×
UNCOV
1155
        }
×
1156

1157
        commentProto := &guardianv1beta1.AppealComment{
3✔
1158
                Id:        c.ID,
3✔
1159
                AppealId:  c.ParentID,
3✔
1160
                CreatedBy: c.CreatedBy,
3✔
1161
                Body:      c.Body,
3✔
1162
        }
3✔
1163

3✔
1164
        if !c.CreatedAt.IsZero() {
6✔
1165
                commentProto.CreatedAt = timestamppb.New(c.CreatedAt)
3✔
1166
        }
3✔
1167
        if !c.UpdatedAt.IsZero() {
6✔
1168
                commentProto.UpdatedAt = timestamppb.New(c.UpdatedAt)
3✔
1169
        }
3✔
1170

1171
        return commentProto
3✔
1172
}
1173

UNCOV
1174
func (a *adapter) fromConditionProto(c *guardianv1beta1.Condition) *domain.Condition {
×
UNCOV
1175
        if c == nil {
×
UNCOV
1176
                return nil
×
UNCOV
1177
        }
×
1178

1179
        var match *domain.MatchCondition
×
1180
        if c.GetMatch() != nil {
×
1181
                match = &domain.MatchCondition{
×
UNCOV
1182
                        Eq: c.GetMatch().GetEq(),
×
1183
                }
×
1184
        }
×
1185

1186
        return &domain.Condition{
×
1187
                Field: c.GetField(),
×
1188
                Match: match,
×
UNCOV
1189
        }
×
1190
}
1191

1192
func (a *adapter) ToAppealActivityProto(e *domain.Event) (*guardianv1beta1.AppealActivity, error) {
×
1193
        if e == nil {
×
UNCOV
1194
                return nil, nil
×
UNCOV
1195
        }
×
1196

1197
        activityProto := &guardianv1beta1.AppealActivity{
×
1198
                AppealId:  e.ParentID,
×
1199
                Timestamp: timestamppb.New(e.Timestamp),
×
UNCOV
1200
                Type:      e.Type,
×
1201
                Actor:     e.Actor,
×
1202
        }
×
1203

×
1204
        if e.Data != nil {
×
1205
                data, err := structpb.NewStruct(e.Data)
×
1206
                if err != nil {
×
1207
                        return nil, err
×
1208
                }
×
1209
                activityProto.Data = data
×
1210
        }
1211
        return activityProto, nil
×
1212
}
1213

1214
func (a *adapter) ToSummaryProto(s *domain.SummaryResult) (*guardianv1beta1.SummaryResult, error) {
11✔
1215
        if s == nil {
22✔
1216
                return nil, nil
11✔
1217
        }
11✔
1218

UNCOV
1219
        var appliedParameters *guardianv1beta1.SummaryParameters
×
UNCOV
1220
        if s.AppliedParameters != nil {
×
UNCOV
1221
                filters, err := toProtoMap(s.AppliedParameters.Filters)
×
UNCOV
1222
                if err != nil {
×
1223
                        return nil, fmt.Errorf("parsing filters: %w", err)
×
1224
                }
×
1225

1226
                appliedParameters = &guardianv1beta1.SummaryParameters{
×
1227
                        GroupBys: s.AppliedParameters.GroupBys,
×
1228
                        Filters:  filters,
×
UNCOV
1229
                }
×
1230
        }
1231

1232
        summaryProto := &guardianv1beta1.SummaryResult{
×
1233
                AppliedParameters: appliedParameters,
×
UNCOV
1234
                Count:             s.Count,
×
UNCOV
1235

×
1236
                Groups:      make([]*guardianv1beta1.SummaryResult_Group, len(s.SummaryGroups)),
×
1237
                GroupsCount: s.GroupsCount,
×
1238

×
1239
                Uniques:      make([]*guardianv1beta1.SummaryResult_Unique, len(s.SummaryUniques)),
×
1240
                UniquesCount: s.UniquesCount,
×
1241

×
1242
                Labels:      make([]*guardianv1beta1.SummaryResult_Label, len(s.SummaryLabels)),
×
1243
                LabelsCount: s.LabelsCount,
×
1244
        }
×
1245

×
1246
        // convert summary groups
×
1247
        for i, group := range s.SummaryGroups {
×
1248
                groupFields, err := toProtoMap(group.GroupFields)
×
1249
                if err != nil {
×
1250
                        return nil, fmt.Errorf("parsing group fields: %w", err)
×
1251
                }
×
1252

1253
                summaryProto.Groups[i] = &guardianv1beta1.SummaryResult_Group{
×
1254
                        GroupFields: groupFields,
×
1255
                        Count:       group.Count,
×
UNCOV
1256
                }
×
1257

×
1258
                if len(group.DistinctCounts) > 0 {
×
1259
                        summaryProto.Groups[i].DistinctCounts = group.DistinctCounts
×
1260
                }
×
1261
        }
1262

1263
        // convert summary uniques
1264
        for i, unique := range s.SummaryUniques {
×
UNCOV
1265
                values, err := toProtoList(unique.Values)
×
UNCOV
1266
                if err != nil {
×
UNCOV
1267
                        return nil, fmt.Errorf("parsing unique values: %w", err)
×
1268
                }
×
1269

1270
                summaryProto.Uniques[i] = &guardianv1beta1.SummaryResult_Unique{
×
1271
                        Field:  unique.Field,
×
1272
                        Values: values,
×
UNCOV
1273
                        Count:  unique.Count,
×
1274
                }
×
1275
        }
1276

1277
        // convert summary labels
1278
        for i, label := range s.SummaryLabels {
×
UNCOV
1279
                summaryProto.Labels[i] = &guardianv1beta1.SummaryResult_Label{
×
UNCOV
1280
                        Key:    label.Key,
×
UNCOV
1281
                        Values: label.Values,
×
1282
                        Count:  label.Count,
×
1283
                }
×
1284
        }
×
1285

1286
        return summaryProto, nil
×
1287
}
1288

1289
func (a *adapter) FromTimeProto(t *timestamppb.Timestamp) time.Time {
44✔
1290
        if t == nil || (t.GetSeconds() == 1 && t.GetNanos() == 2) {
88✔
1291
                return time.Time{}.UTC()
44✔
1292
        }
44✔
UNCOV
1293
        return t.AsTime().UTC()
×
1294
}
1295

UNCOV
1296
func (a *adapter) toConditionProto(c *domain.Condition) (*guardianv1beta1.Condition, error) {
×
1297
        if c == nil {
×
UNCOV
1298
                return nil, nil
×
UNCOV
1299
        }
×
1300

1301
        var match *guardianv1beta1.Condition_MatchCondition
×
1302
        if c.Match != nil {
×
1303
                eq, err := structpb.NewValue(c.Match.Eq)
×
UNCOV
1304
                if err != nil {
×
1305
                        return nil, err
×
1306
                }
×
1307

1308
                match = &guardianv1beta1.Condition_MatchCondition{
×
1309
                        Eq: eq,
×
1310
                }
×
1311
        }
1312

1313
        return &guardianv1beta1.Condition{
×
1314
                Field: c.Field,
×
UNCOV
1315
                Match: match,
×
UNCOV
1316
        }, nil
×
1317
}
1318

1319
func (a *adapter) toPostAppealHookProto(hook *domain.PostAppealHook) (*guardianv1beta1.Policy_Requirement_PostAppealHook, error) {
×
1320
        if hook == nil {
×
UNCOV
1321
                return nil, nil
×
UNCOV
1322
        }
×
1323

1324
        var config *structpb.Struct
×
1325
        if hook.Config != nil {
×
1326
                configMap, ok := hook.Config.(map[string]interface{})
×
UNCOV
1327
                if !ok {
×
1328
                        return nil, fmt.Errorf("invalid hook config type: %T, expected map[string]interface{}", hook.Config)
×
1329
                }
×
1330
                var err error
×
1331
                config, err = structpb.NewStruct(configMap)
×
1332
                if err != nil {
×
1333
                        return nil, fmt.Errorf("converting hook config to struct: %w", err)
×
1334
                }
×
1335
        }
1336

1337
        return &guardianv1beta1.Policy_Requirement_PostAppealHook{
×
1338
                Name:        hook.Name,
×
UNCOV
1339
                Description: hook.Description,
×
UNCOV
1340
                Type:        hook.Type,
×
1341
                Config:      config,
×
1342
                AllowFailed: hook.AllowFailed,
×
1343
        }, nil
×
1344
}
1345

1346
func (a *adapter) fromPostAppealHookProto(proto *guardianv1beta1.Policy_Requirement_PostAppealHook) *domain.PostAppealHook {
×
1347
        if proto == nil {
×
UNCOV
1348
                return nil
×
UNCOV
1349
        }
×
1350

1351
        var config interface{}
×
1352
        if proto.Config != nil {
×
1353
                config = proto.Config.AsMap()
×
UNCOV
1354
        }
×
1355

1356
        return &domain.PostAppealHook{
×
1357
                Name:        proto.Name,
×
1358
                Description: proto.Description,
×
UNCOV
1359
                Type:        proto.Type,
×
1360
                Config:      config,
×
1361
                AllowFailed: proto.AllowFailed,
×
1362
        }
×
1363
}
1364

1365
func (a *adapter) fromAppealOptionsProto(o *guardianv1beta1.AppealOptions) *domain.AppealOptions {
2✔
1366
        if o == nil {
3✔
1367
                return nil
1✔
1368
        }
1✔
1369

1370
        options := &domain.AppealOptions{
1✔
1371
                Duration: o.GetDuration(),
1✔
1372
        }
1✔
1373

1✔
1374
        if o.GetExpirationDate() != nil {
1✔
UNCOV
1375
                expDate := o.GetExpirationDate().AsTime()
×
UNCOV
1376
                options.ExpirationDate = &expDate
×
UNCOV
1377
        }
×
1378

1379
        return options
1✔
1380
}
1381

1382
func (a *adapter) toAppealOptionsProto(o *domain.AppealOptions) *guardianv1beta1.AppealOptions {
41✔
1383
        if o == nil {
67✔
1384
                return nil
26✔
1385
        }
26✔
1386

1387
        optionsProto := &guardianv1beta1.AppealOptions{
15✔
1388
                Duration: o.Duration,
15✔
1389
        }
15✔
1390

15✔
1391
        if o.ExpirationDate != nil {
26✔
1392
                optionsProto.ExpirationDate = timestamppb.New(*o.ExpirationDate)
11✔
1393
        }
11✔
1394

1395
        return optionsProto
15✔
1396
}
1397

1398
func (a *adapter) fromPolicyConfigProto(c *guardianv1beta1.PolicyConfig) *domain.PolicyConfig {
4✔
1399
        if c == nil {
4✔
UNCOV
1400
                return nil
×
UNCOV
1401
        }
×
1402

1403
        return &domain.PolicyConfig{
4✔
1404
                ID:      c.GetId(),
4✔
1405
                Version: int(c.GetVersion()),
4✔
1406
        }
4✔
1407
}
1408

1409
func (a *adapter) toPolicyConfigProto(c *domain.PolicyConfig) *guardianv1beta1.PolicyConfig {
12✔
1410
        if c == nil {
15✔
1411
                return nil
3✔
1412
        }
3✔
1413

1414
        return &guardianv1beta1.PolicyConfig{
9✔
1415
                Id:      c.ID,
9✔
1416
                Version: int32(c.Version),
9✔
1417
        }
9✔
1418
}
1419

UNCOV
1420
func toGoMap(in map[string]*structpb.Value) map[string]any {
×
UNCOV
1421
        out := make(map[string]any, len(in))
×
UNCOV
1422
        for k, v := range in {
×
UNCOV
1423
                out[k] = v.AsInterface()
×
1424
        }
×
1425
        return out
×
1426
}
1427

1428
func toProtoMap(in map[string]any) (map[string]*structpb.Value, error) {
×
1429
        out := make(map[string]*structpb.Value, len(in))
×
UNCOV
1430
        for k, v := range in {
×
UNCOV
1431
                val, err := structpb.NewValue(v)
×
1432
                if err != nil {
×
1433
                        return nil, err
×
1434
                }
×
1435
                out[k] = val
×
1436
        }
1437
        return out, nil
×
1438
}
1439

UNCOV
1440
func toProtoList(in []interface{}) ([]*structpb.Value, error) {
×
1441
        out := make([]*structpb.Value, len(in))
×
UNCOV
1442
        for k, v := range in {
×
UNCOV
1443
                val, err := structpb.NewValue(v)
×
1444
                if err != nil {
×
1445
                        return nil, err
×
1446
                }
×
1447
                out[k] = val
×
1448
        }
1449
        return out, nil
×
1450
}
1451

1452
func (a *adapter) toLabelMetadataProto(metadata map[string]*domain.LabelMetadata) map[string]*guardianv1beta1.LabelMetadata {
37✔
1453
        if metadata == nil {
70✔
1454
                return nil
33✔
1455
        }
33✔
1456

1457
        result := make(map[string]*guardianv1beta1.LabelMetadata, len(metadata))
4✔
1458
        for key, meta := range metadata {
9✔
1459
                protoMeta := &guardianv1beta1.LabelMetadata{
5✔
1460
                        Value:       meta.Value,
5✔
1461
                        DerivedFrom: meta.DerivedFrom,
5✔
1462
                        Source:      string(meta.Source),
5✔
1463
                        Category:    meta.Category,
5✔
1464
                        AppliedBy:   meta.AppliedBy,
5✔
1465
                }
5✔
1466

5✔
1467
                if meta.Attributes != nil {
6✔
1468
                        attrs, err := structpb.NewStruct(meta.Attributes)
1✔
1469
                        if err == nil {
2✔
1470
                                protoMeta.Attributes = attrs
1✔
1471
                        }
1✔
1472
                }
1473

1474
                if !meta.AppliedAt.IsZero() {
9✔
1475
                        protoMeta.AppliedAt = timestamppb.New(meta.AppliedAt)
4✔
1476
                }
4✔
1477

1478
                result[key] = protoMeta
5✔
1479
        }
1480

1481
        return result
4✔
1482
}
1483

1484
func (a *adapter) FromLabelFiltersProto(labels map[string]*guardianv1beta1.LabelValues) map[string][]string {
28✔
1485
        if labels == nil {
49✔
1486
                return nil
21✔
1487
        }
21✔
1488

1489
        result := make(map[string][]string, len(labels))
7✔
1490
        for key, values := range labels {
16✔
1491
                if values != nil && values.Values != nil {
16✔
1492
                        result[key] = values.Values
7✔
1493
                }
7✔
1494
        }
1495

1496
        return result
7✔
1497
}
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

© 2026 Coveralls, Inc