Skip to content

Commit

Permalink
feat: introduce grant diff in update
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmatrhd committed Aug 22, 2024
1 parent bbf994a commit 0561484
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
11 changes: 10 additions & 1 deletion core/grant/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,21 @@ func (s *Service) Update(ctx context.Context, payload *domain.GrantUpdate) (*dom
s.logger.Info(ctx, "grant updated", "grant_id", grant.ID, "updatedGrant", latestGrant)

go func() {
diff, err := latestGrant.Compare(grant)
if err != nil {
s.logger.Error(ctx, "failed to compare grant", "error", err)
return
}
for _, d := range diff {
d.Actor = payload.Actor
}

ctx := context.WithoutCancel(ctx)
if err := s.auditLogger.Log(ctx, AuditKeyUpdate, map[string]interface{}{
"grant_id": payload.ID,
"payload": payload,
"updated_grant": latestGrant,
// TODO: diff
"diff": diff,
}); err != nil {
s.logger.Error(ctx, "failed to record audit log", "error", err)
}
Expand Down
22 changes: 22 additions & 0 deletions domain/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"sort"
"strings"
"time"

"github.com/goto/guardian/pkg/diff"
)

type GrantStatus string
Expand Down Expand Up @@ -125,12 +127,32 @@ func (g *Grant) GetPermissions() []string {
return permissions
}

func (g *Grant) Compare(old *Grant) ([]*DiffItem, error) {
diff, err := diff.Compare(old, g)
if err != nil {
return nil, err
}

diffItems := make([]*DiffItem, 0, len(diff))
for _, d := range diff {
diffItems = append(diffItems, &DiffItem{
Op: d.Op,
Path: d.Path,
OldValue: d.OldValue,
NewValue: d.NewValue,
})
}
return diffItems, nil
}

type GrantUpdate struct {
ID string `json:"id" yaml:"id"`
Owner *string `json:"owner,omitempty" yaml:"owner,omitempty"`
IsPermanent *bool `json:"is_permanent,omitempty" yaml:"is_permanent,omitempty"`
ExpirationDate *time.Time `json:"expiration_date,omitempty" yaml:"expiration_date,omitempty"`
ExpirationDateReason *string `json:"expiration_date_reason,omitempty" yaml:"expiration_date_reason,omitempty"`

Actor string `json:"actor" yaml:"actor"`
}

func (gu *GrantUpdate) IsUpdatingExpirationDate() bool {
Expand Down
1 change: 1 addition & 0 deletions jobs/revoke_grants_by_user_criteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func (h *handler) reassignGrantsOwnership(ctx context.Context, ownedGrants []*do
payload := &domain.GrantUpdate{
ID: g.ID,
Owner: &newOwner,
Actor: domain.SystemActorName,
}
if _, err := h.grantService.Update(ctx, payload); err != nil {
failedGrants = append(failedGrants, g)
Expand Down

0 comments on commit 0561484

Please sign in to comment.