Skip to content

Commit

Permalink
merge main branch and resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
singhvikash11 committed Jan 4, 2023
2 parents 86f3edf + 9bea185 commit a52e6d8
Show file tree
Hide file tree
Showing 98 changed files with 11,463 additions and 2,577 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ COMMIT := $(shell git rev-parse --short HEAD)
TAG := "$(shell git rev-list --tags --max-count=1)"
VERSION := "$(shell git describe --tags ${TAG})-next"
BUILD_DIR=dist
PROTON_COMMIT := "4e3b6b5c5b51be27527a07d713e4caf076792afe"
PROTON_COMMIT := "912d3124486acfc44310d92e15169b41a2f5f0ef"

.PHONY: all build clean test tidy vet proto setup format generate

Expand Down
101 changes: 101 additions & 0 deletions api/handler/v1beta1/activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package v1beta1

import (
"context"
"errors"

guardianv1beta1 "github.com/odpf/guardian/api/proto/odpf/guardian/v1beta1"
"github.com/odpf/guardian/core/activity"
"github.com/odpf/guardian/domain"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (s *GRPCServer) GetActivity(ctx context.Context, req *guardianv1beta1.GetActivityRequest) (*guardianv1beta1.GetActivityResponse, error) {
a, err := s.activityService.GetOne(ctx, req.GetId())
if err != nil {
if errors.Is(err, activity.ErrNotFound) {
return nil, status.Errorf(codes.NotFound, "activity not found")
}
return nil, status.Errorf(codes.Internal, "failed to get activity: %v", err)
}

activityProto, err := s.adapter.ToActivityProto(a)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to parse proto: %v", err)
}

return &guardianv1beta1.GetActivityResponse{
Activity: activityProto,
}, nil
}

func (s *GRPCServer) ListActivities(ctx context.Context, req *guardianv1beta1.ListActivitiesRequest) (*guardianv1beta1.ListActivitiesResponse, error) {
filter := domain.ListProviderActivitiesFilter{
ProviderIDs: req.GetProviderIds(),
AccountIDs: req.GetAccountIds(),
ResourceIDs: req.GetResourceIds(),
Types: req.GetTypes(),
}
if req.GetTimestampGte() != nil {
t := req.GetTimestampGte().AsTime()
filter.TimestampGte = &t
}
if req.GetTimestampLte() != nil {
t := req.GetTimestampLte().AsTime()
filter.TimestampLte = &t
}

activities, err := s.activityService.Find(ctx, filter)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list activities: %v", err)
}

activityProtos := []*guardianv1beta1.ProviderActivity{}
for _, a := range activities {
activityProto, err := s.adapter.ToActivityProto(a)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to parse proto: %v", err)
}
activityProtos = append(activityProtos, activityProto)
}

return &guardianv1beta1.ListActivitiesResponse{
Activities: activityProtos,
}, nil
}

func (s *GRPCServer) ImportActivities(ctx context.Context, req *guardianv1beta1.ImportActivitiesRequest) (*guardianv1beta1.ImportActivitiesResponse, error) {
filter := domain.ImportActivitiesFilter{
ProviderID: req.GetProviderId(),
ResourceIDs: req.GetResourceIds(),
AccountIDs: req.GetAccountIds(),
}
if req.GetTimestampGte() != nil {
t := req.GetTimestampGte().AsTime()
filter.TimestampGte = &t
}
if req.GetTimestampLte() != nil {
t := req.GetTimestampLte().AsTime()
filter.TimestampLte = &t
}

activities, err := s.activityService.Import(ctx, filter)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to import activities: %v", err)
}

activityProtos := []*guardianv1beta1.ProviderActivity{}
for _, a := range activities {
activity, err := s.adapter.ToActivityProto(a)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to parse proto: %v", err)
}

activityProtos = append(activityProtos, activity)
}

return &guardianv1beta1.ImportActivitiesResponse{
Activities: activityProtos,
}, nil
}
77 changes: 77 additions & 0 deletions api/handler/v1beta1/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,17 @@ func (a *adapter) FromResourceProto(r *guardianv1beta1.Resource) *domain.Resourc
IsDeleted: r.GetIsDeleted(),
}

if r.GetParentId() != "" {
id := r.GetParentId()
resource.ParentID = &id
}

if r.GetChildren() != nil {
for _, c := range r.GetChildren() {
resource.Children = append(resource.Children, a.FromResourceProto(c))
}
}

if r.GetDetails() != nil {
resource.Details = r.GetDetails().AsMap()
}
Expand All @@ -522,6 +533,20 @@ func (a *adapter) ToResourceProto(r *domain.Resource) (*guardianv1beta1.Resource
IsDeleted: r.IsDeleted,
}

if r.ParentID != nil {
resourceProto.ParentId = *r.ParentID
}

if r.Children != nil {
for _, c := range r.Children {
childProto, err := a.ToResourceProto(c)
if err != nil {
return nil, fmt.Errorf("failed to convert child resource to proto %q: %w", c.ID, err)
}
resourceProto.Children = append(resourceProto.Children, childProto)
}
}

if r.Details != nil {
details, err := structpb.NewStruct(r.Details)
if err != nil {
Expand Down Expand Up @@ -771,6 +796,58 @@ func (a *adapter) ToGrantProto(grant *domain.Grant) (*guardianv1beta1.Grant, err
return grantProto, nil
}

func (a *adapter) ToActivityProto(activity *domain.Activity) (*guardianv1beta1.ProviderActivity, error) {
if activity == nil {
return nil, nil
}

activityProto := &guardianv1beta1.ProviderActivity{
Id: activity.ID,
ProviderId: activity.ProviderID,
ResourceId: activity.ResourceID,
ProviderActivityId: activity.ProviderActivityID,
AccountType: activity.AccountType,
AccountId: activity.AccountID,
Authorizations: activity.Authorizations,
RelatedPermissions: activity.RelatedPermissions,
Type: activity.Type,
}

if !activity.Timestamp.IsZero() {
activityProto.Timestamp = timestamppb.New(activity.Timestamp)
}

if activity.Metadata != nil {
metadataStruct, err := structpb.NewStruct(activity.Metadata)
if err != nil {
return nil, fmt.Errorf("parsing metadata: %w", err)
}
activityProto.Metadata = metadataStruct
}

if !activity.CreatedAt.IsZero() {
activityProto.CreatedAt = timestamppb.New(activity.CreatedAt)
}

if activity.Provider != nil {
providerProto, err := a.ToProviderProto(activity.Provider)
if err != nil {
return nil, fmt.Errorf("parsing provider: %w", err)
}
activityProto.Provider = providerProto
}

if activity.Resource != nil {
resourceProto, err := a.ToResourceProto(activity.Resource)
if err != nil {
return nil, fmt.Errorf("parsing resource: %w", err)
}
activityProto.Resource = resourceProto
}

return activityProto, nil
}

func (a *adapter) fromConditionProto(c *guardianv1beta1.Condition) *domain.Condition {
if c == nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion api/handler/v1beta1/approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *GRPCServer) UpdateApproval(ctx context.Context, req *guardianv1beta1.Up
}

id := req.GetId()
a, err := s.appealService.MakeAction(ctx, domain.ApprovalAction{
a, err := s.appealService.UpdateApproval(ctx, domain.ApprovalAction{
AppealID: id,
ApprovalName: req.GetApprovalName(),
Actor: actor,
Expand Down
6 changes: 3 additions & 3 deletions api/handler/v1beta1/approval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func (s *GrpcHandlersSuite) TestUpdateApproval() {
Action: expectedAction,
Reason: expectedReason,
}
s.appealService.EXPECT().MakeAction(mock.AnythingOfType("*context.valueCtx"), expectedApprovalAction).Return(expectedAppeal, nil).Once()
s.appealService.EXPECT().UpdateApproval(mock.AnythingOfType("*context.valueCtx"), expectedApprovalAction).Return(expectedAppeal, nil).Once()

req := &guardianv1beta1.UpdateApprovalRequest{
Id: expectedID,
Expand Down Expand Up @@ -549,7 +549,7 @@ func (s *GrpcHandlersSuite) TestUpdateApproval() {
s.setup()

expectedUser := "user@example.com"
s.appealService.EXPECT().MakeAction(mock.AnythingOfType("*context.valueCtx"), mock.Anything).
s.appealService.EXPECT().UpdateApproval(mock.AnythingOfType("*context.valueCtx"), mock.Anything).
Return(nil, tc.expectedError).Once()

req := &guardianv1beta1.UpdateApprovalRequest{}
Expand All @@ -575,7 +575,7 @@ func (s *GrpcHandlersSuite) TestUpdateApproval() {
"foo": make(chan int), // invalid json
},
}
s.appealService.EXPECT().MakeAction(mock.AnythingOfType("*context.valueCtx"), mock.Anything).
s.appealService.EXPECT().UpdateApproval(mock.AnythingOfType("*context.valueCtx"), mock.Anything).
Return(invalidAppeal, nil).Once()

req := &guardianv1beta1.UpdateApprovalRequest{}
Expand Down
15 changes: 13 additions & 2 deletions api/handler/v1beta1/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type ProtoAdapter interface {

ToGrantProto(*domain.Grant) (*guardianv1beta1.Grant, error)
FromGrantProto(*guardianv1beta1.Grant) *domain.Grant

ToActivityProto(*domain.Activity) (*guardianv1beta1.ProviderActivity, error)
}

//go:generate mockery --name=resourceService --exported --with-expecter
Expand All @@ -47,6 +49,13 @@ type resourceService interface {
BatchDelete(context.Context, []string) error
}

//go:generate mockery --name=activityService --exported --with-expecter
type activityService interface {
GetOne(context.Context, string) (*domain.Activity, error)
Find(context.Context, domain.ListProviderActivitiesFilter) ([]*domain.Activity, error)
Import(context.Context, domain.ImportActivitiesFilter) ([]*domain.Activity, error)
}

//go:generate mockery --name=providerService --exported --with-expecter
type providerService interface {
Create(context.Context, *domain.Provider) error
Expand Down Expand Up @@ -76,17 +85,16 @@ type appealService interface {
GetByID(context.Context, string) (*domain.Appeal, error)
Find(context.Context, *domain.ListAppealsFilter) ([]*domain.Appeal, error)
Create(context.Context, []*domain.Appeal, ...appeal.CreateAppealOption) error
MakeAction(context.Context, domain.ApprovalAction) (*domain.Appeal, error)
Cancel(context.Context, string) (*domain.Appeal, error)
AddApprover(ctx context.Context, appealID, approvalID, email string) (*domain.Appeal, error)
DeleteApprover(ctx context.Context, appealID, approvalID, email string) (*domain.Appeal, error)
UpdateApproval(ctx context.Context, approvalAction domain.ApprovalAction) (*domain.Appeal, error)
}

//go:generate mockery --name=approvalService --exported --with-expecter
type approvalService interface {
ListApprovals(context.Context, *domain.ListApprovalsFilter) ([]*domain.Approval, error)
BulkInsert(context.Context, []*domain.Approval) error
AdvanceApproval(context.Context, *domain.Appeal) error
}

//go:generate mockery --name=grantService --exported --with-expecter
Expand All @@ -100,6 +108,7 @@ type grantService interface {

type GRPCServer struct {
resourceService resourceService
activityService activityService
providerService providerService
policyService policyService
appealService appealService
Expand All @@ -114,6 +123,7 @@ type GRPCServer struct {

func NewGRPCServer(
resourceService resourceService,
activityService activityService,
providerService providerService,
policyService policyService,
appealService appealService,
Expand All @@ -124,6 +134,7 @@ func NewGRPCServer(
) *GRPCServer {
return &GRPCServer{
resourceService: resourceService,
activityService: activityService,
providerService: providerService,
policyService: policyService,
appealService: appealService,
Expand Down
3 changes: 3 additions & 0 deletions api/handler/v1beta1/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type GrpcHandlersSuite struct {
suite.Suite

resourceService *mocks.ResourceService
activityService *mocks.ActivityService
providerService *mocks.ProviderService
policyService *mocks.PolicyService
appealService *mocks.AppealService
Expand All @@ -28,6 +29,7 @@ func TestGrpcHandler(t *testing.T) {

func (s *GrpcHandlersSuite) setup() {
s.resourceService = new(mocks.ResourceService)
s.activityService = new(mocks.ActivityService)
s.providerService = new(mocks.ProviderService)
s.policyService = new(mocks.PolicyService)
s.appealService = new(mocks.AppealService)
Expand All @@ -36,6 +38,7 @@ func (s *GrpcHandlersSuite) setup() {
s.authenticatedUserHeaderKey = "test-header-key"
s.grpcServer = v1beta1.NewGRPCServer(
s.resourceService,
s.activityService,
s.providerService,
s.policyService,
s.appealService,
Expand Down

0 comments on commit a52e6d8

Please sign in to comment.