Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 69 additions & 12 deletions service/authorization/v2/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ func (as *Service) GetEntitlements(ctx context.Context, req *connect.Request[aut
// When authorization service can consume cached policy, switch to the other PDP (process based on policy passed in)
pdp, err := access.NewJustInTimePDP(ctx, as.logger, as.sdk)
if err != nil {
as.logger.ErrorContext(ctx, "failed to create JIT PDP", slog.String("error", err.Error()))
as.logger.ErrorContext(ctx, "failed to create JIT PDP", slog.Any("error", err))
return nil, connect.NewError(connect.CodeInternal, err)
}

entitlements, err := pdp.GetEntitlements(ctx, entityIdentifier, withComprehensiveHierarchy)
if err != nil {
// TODO: any bad request errors that aren't 500s?
as.logger.ErrorContext(ctx, "failed to get entitlements", slog.String("error", err.Error()))
as.logger.ErrorContext(ctx, "failed to get entitlements", slog.Any("error", err))
return nil, connect.NewError(connect.CodeInternal, err)
}

Expand All @@ -113,23 +113,26 @@ func (as *Service) GetDecision(ctx context.Context, req *connect.Request[authzV2

pdp, err := access.NewJustInTimePDP(ctx, as.logger, as.sdk)
if err != nil {
as.logger.ErrorContext(ctx, "failed to create JIT PDP", slog.String("error", err.Error()))
as.logger.ErrorContext(ctx, "failed to create JIT PDP", slog.Any("error", err))
return nil, connect.NewError(connect.CodeInternal, err)
}

request := req.Msg
entityIdentifier := request.GetEntityIdentifier()
action := request.GetAction()
resource := request.GetResource()

decisions, permitted, err := pdp.GetDecision(ctx, entityIdentifier, action, []*authzV2.Resource{resource})
if err != nil {
// TODO: any bad request errors that aren't 500s?
as.logger.ErrorContext(ctx, "failed to get decision", slog.String("error", err.Error()))
as.logger.ErrorContext(ctx, "failed to get decision", slog.Any("error", err), slog.Any("request", request))
if errors.Is(err, access.ErrFQNNotFound) || errors.Is(err, access.ErrDefinitionNotFound) {
return nil, connect.NewError(connect.CodeNotFound, err)
}
return nil, connect.NewError(connect.CodeInternal, err)
}
resp, err := rollupSingleResourceDecision(permitted, decisions)
if err != nil {
as.logger.ErrorContext(ctx, "failed to rollup single resource decision", slog.String("error", err.Error()))
as.logger.ErrorContext(ctx, "failed to rollup single-resource decision", slog.Any("error", err), slog.Any("request", request))
return nil, connect.NewError(connect.CodeInternal, err)
}
return connect.NewResponse(resp), nil
Expand All @@ -148,7 +151,7 @@ func (as *Service) GetDecisionMultiResource(ctx context.Context, req *connect.Re

pdp, err := access.NewJustInTimePDP(ctx, as.logger, as.sdk)
if err != nil {
as.logger.ErrorContext(ctx, "failed to create JIT PDP", slog.String("error", err.Error()))
as.logger.ErrorContext(ctx, "failed to create JIT PDP", slog.Any("error", err))
return nil, connect.NewError(connect.CodeInternal, err)
}
request := req.Msg
Expand All @@ -158,14 +161,16 @@ func (as *Service) GetDecisionMultiResource(ctx context.Context, req *connect.Re

decisions, allPermitted, err := pdp.GetDecision(ctx, entityIdentifier, action, resources)
if err != nil {
// TODO: any bad request errors that aren't 500s?
as.logger.ErrorContext(ctx, "failed to get decision", slog.String("error", err.Error()))
as.logger.ErrorContext(ctx, "failed to get decision", slog.Any("error", err), slog.Any("request", request))
if errors.Is(err, access.ErrFQNNotFound) || errors.Is(err, access.ErrDefinitionNotFound) {
return nil, connect.NewError(connect.CodeNotFound, err)
}
return nil, connect.NewError(connect.CodeInternal, err)
}

resourceDecisions, err := rollupMultiResourceDecision(decisions)
if err != nil {
as.logger.ErrorContext(ctx, "failed to rollup multi resource decision", slog.String("error", err.Error()))
as.logger.ErrorContext(ctx, "failed to rollup multi-resource decision", slog.Any("error", err), slog.Any("request", request))
return nil, connect.NewError(connect.CodeInternal, err)
}

Expand All @@ -180,8 +185,60 @@ func (as *Service) GetDecisionMultiResource(ctx context.Context, req *connect.Re
}

// GetDecisionBulk for multiple requests, each comprising a combination of entity chain, action, and one or more resources
func (as *Service) GetDecisionBulk(_ context.Context, _ *connect.Request[authzV2.GetDecisionBulkRequest]) (*connect.Response[authzV2.GetDecisionBulkResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("GetDecisionBulk not implemented"))
func (as *Service) GetDecisionBulk(ctx context.Context, req *connect.Request[authzV2.GetDecisionBulkRequest]) (*connect.Response[authzV2.GetDecisionBulkResponse], error) {
as.logger.DebugContext(ctx, "getting decision bulk")

ctx, span := as.Tracer.Start(ctx, "GetDecisionBulk")
defer span.End()

// Extract trace context from the incoming request
propagator := otel.GetTextMapPropagator()
ctx = propagator.Extract(ctx, propagation.HeaderCarrier(req.Header()))

pdp, err := access.NewJustInTimePDP(ctx, as.logger, as.sdk)
if err != nil {
as.logger.ErrorContext(ctx, "failed to create JIT PDP", slog.Any("error", err))
return nil, connect.NewError(connect.CodeInternal, err)
}

multiRequests := req.Msg.GetDecisionRequests()
decisionResponses := make([]*authzV2.GetDecisionMultiResourceResponse, len(multiRequests))

// TODO: revisit performance of this loop after introduction of caching and registered resource values within decisioning,
// as the same entity in multiple requests should only be resolved JIT once, not once per request if the same in each.
for idx, request := range multiRequests {
entityIdentifier := request.GetEntityIdentifier()
action := request.GetAction()
resources := request.GetResources()

decisions, allPermitted, err := pdp.GetDecision(ctx, entityIdentifier, action, resources)
if err != nil {
as.logger.ErrorContext(ctx, "failed to get bulk decision", slog.Any("error", err), slog.Any("request", request))
if errors.Is(err, access.ErrFQNNotFound) || errors.Is(err, access.ErrDefinitionNotFound) {
return nil, connect.NewError(connect.CodeNotFound, err)
}
return nil, connect.NewError(connect.CodeInternal, err)
}

resourceDecisions, err := rollupMultiResourceDecision(decisions)
if err != nil {
as.logger.ErrorContext(ctx, "failed to rollup bulk, multi-resource decision", slog.Any("error", err), slog.Any("request", request), slog.Int("index", idx))
return nil, connect.NewError(connect.CodeInternal, err)
}

decisionResponse := &authzV2.GetDecisionMultiResourceResponse{
AllPermitted: &wrapperspb.BoolValue{
Value: allPermitted,
},
ResourceDecisions: resourceDecisions,
}
decisionResponses[idx] = decisionResponse
}

rsp := &authzV2.GetDecisionBulkResponse{
DecisionResponses: decisionResponses,
}
return connect.NewResponse(rsp), nil
}

// rollupMultiResourceDecision creates a standardized response for multi-resource decisions
Expand Down
Loading
Loading