Skip to content

Commit f05bb58

Browse files
bsushmithsinghvikash11rahmatrhd
authored
feat: add appeal duration options to policy (#225)
* feat: add appeal duration options to policy * docs: update documentation * feat: add `0h` check for creating appeal expiration_date * chore: add comments * docs: update documentation * feat: add get policy appeal config api * feat: add appeal field to policy apis * feat: add default value for appeal in policies table * feat: update proto version * fix: fix tests * fix: Fix test cases * feat: initialize the duration_options * fix: fix test cases * feat: update proto version * test: fix test cases * chore: fix lint failures * feat: use time.Duration to check if the duration is zero * docs: update documentation for duration options * feat(provider): use a pointer for policy.Appeal field * feat(provider): rename `appeal` to `appeal_config` in policy struct * rename `policy.Appeal` to `policy.AppealConfig` in both domain and repository models * refactor: minor code refactor * docs: update documentation * feat: add migration queries for policy.appeal_config column * feat: update proto version * fix: validate appealconfig Co-authored-by: Rahmat Hidayat <[email protected]> Co-authored-by: Vikash <[email protected]> Co-authored-by: Rahmat Hidayat <[email protected]>
1 parent 1458d7b commit f05bb58

File tree

17 files changed

+2353
-1296
lines changed

17 files changed

+2353
-1296
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ NAME="github.com/odpf/guardian"
22
LAST_COMMIT := $(shell git rev-parse --short HEAD)
33
LAST_TAG := "$(shell git rev-list --tags --max-count=1)"
44
APP_VERSION := "$(shell git describe --tags ${LAST_TAG})-next"
5-
PROTON_COMMIT := "41664917668e2ecd53f46fc6a06b8d27aab57962"
5+
PROTON_COMMIT := "8266bc0373a9a801a338975c4ce9137a7e7c4fe9"
66

77
.PHONY: all build test clean dist vet proto install
88

api/handler/v1beta1/adapter.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,20 @@ func (a *adapter) FromPolicyProto(p *guardianv1beta1.Policy) *domain.Policy {
278278
}
279279
}
280280

281+
if p.GetAppeal() != nil {
282+
var durationOptions []domain.AppealDurationOption
283+
for _, d := range p.GetAppeal().GetDurationOptions() {
284+
option := domain.AppealDurationOption{
285+
Name: d.GetName(),
286+
Value: d.GetValue(),
287+
}
288+
durationOptions = append(durationOptions, option)
289+
}
290+
policy.AppealConfig = &domain.PolicyAppealConfig{
291+
DurationOptions: durationOptions,
292+
}
293+
}
294+
281295
if p.GetCreatedAt() != nil {
282296
policy.CreatedAt = p.GetCreatedAt().AsTime()
283297
}
@@ -383,6 +397,8 @@ func (a *adapter) ToPolicyProto(p *domain.Policy) (*guardianv1beta1.Policy, erro
383397
}
384398
}
385399

400+
policyProto.Appeal = a.ToPolicyAppealConfigProto(p)
401+
386402
if !p.CreatedAt.IsZero() {
387403
policyProto.CreatedAt = timestamppb.New(p.CreatedAt)
388404
}
@@ -393,6 +409,25 @@ func (a *adapter) ToPolicyProto(p *domain.Policy) (*guardianv1beta1.Policy, erro
393409
return policyProto, nil
394410
}
395411

412+
func (a *adapter) ToPolicyAppealConfigProto(p *domain.Policy) *guardianv1beta1.PolicyAppealConfig {
413+
if p.AppealConfig == nil {
414+
return nil
415+
}
416+
417+
policyAppealConfigProto := &guardianv1beta1.PolicyAppealConfig{}
418+
var durationOptions []*guardianv1beta1.PolicyAppealConfig_DurationOptions
419+
if p.AppealConfig.DurationOptions != nil {
420+
for _, d := range p.AppealConfig.DurationOptions {
421+
durationOptions = append(durationOptions, &guardianv1beta1.PolicyAppealConfig_DurationOptions{
422+
Name: d.Name,
423+
Value: d.Value,
424+
})
425+
}
426+
}
427+
policyAppealConfigProto.DurationOptions = durationOptions
428+
return policyAppealConfigProto
429+
}
430+
396431
func (a *adapter) FromResourceProto(r *guardianv1beta1.Resource) *domain.Resource {
397432
resource := &domain.Resource{
398433
ID: r.GetId(),

api/handler/v1beta1/grpc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type ProtoAdapter interface {
2828
FromPolicyProto(*guardianv1beta1.Policy) *domain.Policy
2929
ToPolicyProto(*domain.Policy) (*guardianv1beta1.Policy, error)
3030

31+
ToPolicyAppealConfigProto(policy *domain.Policy) *guardianv1beta1.PolicyAppealConfig
32+
3133
FromResourceProto(*guardianv1beta1.Resource) *domain.Resource
3234
ToResourceProto(*domain.Resource) (*guardianv1beta1.Resource, error)
3335

api/handler/v1beta1/policy.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,21 @@ func (s *GRPCServer) UpdatePolicy(ctx context.Context, req *guardianv1beta1.Upda
9191
Policy: policyProto,
9292
}, nil
9393
}
94+
95+
func (s *GRPCServer) GetPolicyAppealConfig(ctx context.Context, req *guardianv1beta1.GetPolicyPreferencesRequest) (*guardianv1beta1.GetPolicyPreferencesResponse, error) {
96+
p, err := s.policyService.GetOne(ctx, req.GetId(), uint(req.GetVersion()))
97+
if err != nil {
98+
switch err {
99+
case policy.ErrPolicyNotFound:
100+
return nil, status.Error(codes.NotFound, "policy not found")
101+
default:
102+
return nil, status.Errorf(codes.Internal, "failed to retrieve policy: %v", err)
103+
}
104+
}
105+
106+
appealConfigProto := s.adapter.ToPolicyAppealConfigProto(p)
107+
108+
return &guardianv1beta1.GetPolicyPreferencesResponse{
109+
Appeal: appealConfigProto,
110+
}, nil
111+
}

api/handler/v1beta1/policy_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ func (s *GrpcHandlersSuite) TestGetPolicy() {
117117
Config: map[string]interface{}{"foo": "bar"},
118118
Schema: map[string]string{"foo": "bar"},
119119
},
120+
AppealConfig: &domain.PolicyAppealConfig{
121+
DurationOptions: []domain.AppealDurationOption{
122+
{Name: "1 Day", Value: "24h"},
123+
{Name: "3 Days", Value: "72h"},
124+
},
125+
},
120126
CreatedAt: timeNow,
121127
UpdatedAt: timeNow,
122128
}
@@ -160,6 +166,12 @@ func (s *GrpcHandlersSuite) TestGetPolicy() {
160166
Config: expectedIAMConfig,
161167
Schema: dummyPolicy.IAM.Schema,
162168
},
169+
Appeal: &guardianv1beta1.PolicyAppealConfig{
170+
DurationOptions: []*guardianv1beta1.PolicyAppealConfig_DurationOptions{
171+
{Name: "1 Day", Value: "24h"},
172+
{Name: "3 Days", Value: "72h"},
173+
},
174+
},
163175
CreatedAt: timestamppb.New(timeNow),
164176
UpdatedAt: timestamppb.New(timeNow),
165177
},
@@ -273,6 +285,12 @@ func (s *GrpcHandlersSuite) TestCreatePolicy() {
273285
Config: map[string]interface{}{"foo": "bar"},
274286
Schema: map[string]string{"foo": "bar"},
275287
},
288+
AppealConfig: &domain.PolicyAppealConfig{
289+
DurationOptions: []domain.AppealDurationOption{
290+
{Name: "1 Day", Value: "24h"},
291+
{Name: "3 Days", Value: "72h"},
292+
},
293+
},
276294
}
277295
expectedVersion := uint(1)
278296
expectedIAMConfig, err := structpb.NewValue(expectedPolicy.IAM.Config)
@@ -318,6 +336,12 @@ func (s *GrpcHandlersSuite) TestCreatePolicy() {
318336
Config: expectedIAMConfig,
319337
Schema: expectedPolicy.IAM.Schema,
320338
},
339+
Appeal: &guardianv1beta1.PolicyAppealConfig{
340+
DurationOptions: []*guardianv1beta1.PolicyAppealConfig_DurationOptions{
341+
{Name: "1 Day", Value: "24h"},
342+
{Name: "3 Days", Value: "72h"},
343+
},
344+
},
321345
CreatedAt: timestamppb.New(timeNow),
322346
UpdatedAt: timestamppb.New(timeNow),
323347
},
@@ -369,6 +393,12 @@ func (s *GrpcHandlersSuite) TestCreatePolicy() {
369393
Config: expectedIAMConfig,
370394
Schema: expectedPolicy.IAM.Schema,
371395
},
396+
Appeal: &guardianv1beta1.PolicyAppealConfig{
397+
DurationOptions: []*guardianv1beta1.PolicyAppealConfig_DurationOptions{
398+
{Name: "1 Day", Value: "24h"},
399+
{Name: "3 Days", Value: "72h"},
400+
},
401+
},
372402
},
373403
}
374404
res, err := s.grpcServer.CreatePolicy(context.Background(), req)
@@ -455,6 +485,12 @@ func (s *GrpcHandlersSuite) TestUpdatePolicy() {
455485
Config: map[string]interface{}{"foo": "bar"},
456486
Schema: map[string]string{"foo": "bar"},
457487
},
488+
AppealConfig: &domain.PolicyAppealConfig{
489+
DurationOptions: []domain.AppealDurationOption{
490+
{Name: "1 Day", Value: "24h"},
491+
{Name: "3 Days", Value: "72h"},
492+
},
493+
},
458494
}
459495
expectedVersion := uint(1)
460496
expectedIAMConfig, err := structpb.NewValue(expectedPolicy.IAM.Config)
@@ -497,6 +533,12 @@ func (s *GrpcHandlersSuite) TestUpdatePolicy() {
497533
Config: expectedIAMConfig,
498534
Schema: expectedPolicy.IAM.Schema,
499535
},
536+
Appeal: &guardianv1beta1.PolicyAppealConfig{
537+
DurationOptions: []*guardianv1beta1.PolicyAppealConfig_DurationOptions{
538+
{Name: "1 Day", Value: "24h"},
539+
{Name: "3 Days", Value: "72h"},
540+
},
541+
},
500542
CreatedAt: timestamppb.New(timeNow),
501543
UpdatedAt: timestamppb.New(timeNow),
502544
},
@@ -545,6 +587,12 @@ func (s *GrpcHandlersSuite) TestUpdatePolicy() {
545587
Config: expectedIAMConfig,
546588
Schema: expectedPolicy.IAM.Schema,
547589
},
590+
Appeal: &guardianv1beta1.PolicyAppealConfig{
591+
DurationOptions: []*guardianv1beta1.PolicyAppealConfig_DurationOptions{
592+
{Name: "1 Day", Value: "24h"},
593+
{Name: "3 Days", Value: "72h"},
594+
},
595+
},
548596
},
549597
}
550598
res, err := s.grpcServer.UpdatePolicy(context.Background(), req)

0 commit comments

Comments
 (0)