2022-09-06 00:15:47 +08:00
|
|
|
package acimpl
|
2022-08-24 19:29:17 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2022-08-25 18:50:27 +08:00
|
|
|
"errors"
|
2024-07-05 17:31:23 +08:00
|
|
|
"time"
|
2022-08-24 19:29:17 +08:00
|
|
|
|
2024-07-05 17:31:23 +08:00
|
|
|
openfgav1 "github.com/openfga/api/proto/openfga/v1"
|
2022-08-24 19:29:17 +08:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
|
2024-06-13 12:11:35 +08:00
|
|
|
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
2022-08-24 19:29:17 +08:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2024-07-05 17:31:23 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/authz/zanzana"
|
2024-05-09 17:18:03 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2022-08-24 19:29:17 +08:00
|
|
|
)
|
|
|
|
|
|
2024-07-05 17:31:23 +08:00
|
|
|
var (
|
|
|
|
|
errAccessNotImplemented = errors.New("access control not implemented for resource")
|
|
|
|
|
)
|
|
|
|
|
|
2022-08-24 19:29:17 +08:00
|
|
|
var _ accesscontrol.AccessControl = new(AccessControl)
|
|
|
|
|
|
2024-07-05 17:31:23 +08:00
|
|
|
func ProvideAccessControl(features featuremgmt.FeatureToggles, zclient zanzana.Client) *AccessControl {
|
2022-08-24 19:29:17 +08:00
|
|
|
logger := log.New("accesscontrol")
|
|
|
|
|
return &AccessControl{
|
2024-07-05 17:31:23 +08:00
|
|
|
features, logger, accesscontrol.NewResolvers(logger), zclient,
|
2022-08-24 19:29:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 17:31:23 +08:00
|
|
|
func ProvideAccessControlTest() *AccessControl {
|
|
|
|
|
return ProvideAccessControl(featuremgmt.WithFeatures(), zanzana.NewNoopClient())
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-24 19:29:17 +08:00
|
|
|
type AccessControl struct {
|
2024-05-10 18:56:52 +08:00
|
|
|
features featuremgmt.FeatureToggles
|
2022-08-24 19:29:17 +08:00
|
|
|
log log.Logger
|
|
|
|
|
resolvers accesscontrol.Resolvers
|
2024-07-05 17:31:23 +08:00
|
|
|
zclient zanzana.Client
|
2022-08-24 19:29:17 +08:00
|
|
|
}
|
|
|
|
|
|
2023-08-09 15:35:50 +08:00
|
|
|
func (a *AccessControl) Evaluate(ctx context.Context, user identity.Requester, evaluator accesscontrol.Evaluator) (bool, error) {
|
2024-07-05 17:31:23 +08:00
|
|
|
if a.features.IsEnabledGlobally(featuremgmt.FlagZanzana) {
|
|
|
|
|
return a.evaluateCompare(ctx, user, evaluator)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return a.evaluate(ctx, user, evaluator)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *AccessControl) evaluate(ctx context.Context, user identity.Requester, evaluator accesscontrol.Evaluator) (bool, error) {
|
2022-08-24 19:29:17 +08:00
|
|
|
timer := prometheus.NewTimer(metrics.MAccessEvaluationsSummary)
|
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
metrics.MAccessEvaluationCount.Inc()
|
|
|
|
|
|
2023-08-09 15:35:50 +08:00
|
|
|
if user == nil || user.IsNil() {
|
2023-09-05 00:49:47 +08:00
|
|
|
a.log.Warn("No entity set for access control evaluation")
|
2022-09-09 15:07:45 +08:00
|
|
|
return false, nil
|
2022-08-24 19:29:17 +08:00
|
|
|
}
|
2023-08-09 15:35:50 +08:00
|
|
|
|
2024-02-01 19:37:01 +08:00
|
|
|
// If the user is in no organization, then the evaluation must happen based on the user's global permissions
|
|
|
|
|
permissions := user.GetPermissions()
|
|
|
|
|
if user.GetOrgID() == accesscontrol.NoOrgID {
|
|
|
|
|
permissions = user.GetGlobalPermissions()
|
|
|
|
|
}
|
|
|
|
|
if len(permissions) == 0 {
|
2024-03-05 15:50:19 +08:00
|
|
|
a.debug(ctx, user, "No permissions set", evaluator)
|
2023-08-09 15:35:50 +08:00
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 15:50:19 +08:00
|
|
|
a.debug(ctx, user, "Evaluating permissions", evaluator)
|
2022-08-25 18:50:27 +08:00
|
|
|
// Test evaluation without scope resolver first, this will prevent 403 for wildcard scopes when resource does not exist
|
2024-02-01 19:37:01 +08:00
|
|
|
if evaluator.Evaluate(permissions) {
|
2022-08-25 18:50:27 +08:00
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 15:35:50 +08:00
|
|
|
resolvedEvaluator, err := evaluator.MutateScopes(ctx, a.resolvers.GetScopeAttributeMutator(user.GetOrgID()))
|
2022-08-24 19:29:17 +08:00
|
|
|
if err != nil {
|
2022-08-25 18:50:27 +08:00
|
|
|
if errors.Is(err, accesscontrol.ErrResolverNotFound) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
2022-08-24 19:29:17 +08:00
|
|
|
return false, err
|
|
|
|
|
}
|
2022-08-25 18:50:27 +08:00
|
|
|
|
2024-03-05 15:50:19 +08:00
|
|
|
a.debug(ctx, user, "Evaluating resolved permissions", resolvedEvaluator)
|
2024-02-01 19:37:01 +08:00
|
|
|
return resolvedEvaluator.Evaluate(permissions), nil
|
2022-08-24 19:29:17 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-05 17:31:23 +08:00
|
|
|
func (a *AccessControl) evaluateZanzana(ctx context.Context, user identity.Requester, evaluator accesscontrol.Evaluator) (bool, error) {
|
|
|
|
|
eval, err := evaluator.MutateScopes(ctx, a.resolvers.GetScopeAttributeMutator(user.GetOrgID()))
|
|
|
|
|
if err != nil {
|
|
|
|
|
if !errors.Is(err, accesscontrol.ErrResolverNotFound) {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
eval = evaluator
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return eval.EvaluateCustom(func(action, scope string) (bool, error) {
|
|
|
|
|
kind, _, identifier := accesscontrol.SplitScope(scope)
|
|
|
|
|
key, ok := zanzana.TranslateToTuple(user.GetUID().String(), action, kind, identifier, user.GetOrgID())
|
|
|
|
|
if !ok {
|
|
|
|
|
// unsupported translation
|
|
|
|
|
return false, errAccessNotImplemented
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := a.zclient.Check(ctx, &openfgav1.CheckRequest{
|
|
|
|
|
TupleKey: &openfgav1.CheckRequestTupleKey{
|
|
|
|
|
User: key.User,
|
|
|
|
|
Relation: key.Relation,
|
|
|
|
|
Object: key.Object,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res.Allowed, nil
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type evalResult struct {
|
|
|
|
|
runner string
|
|
|
|
|
decision bool
|
|
|
|
|
err error
|
|
|
|
|
duration time.Duration
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// evaluateCompare run RBAC and zanzana checks in parallel and then compare result
|
|
|
|
|
func (a *AccessControl) evaluateCompare(ctx context.Context, user identity.Requester, evaluator accesscontrol.Evaluator) (bool, error) {
|
|
|
|
|
res := make(chan evalResult, 2)
|
|
|
|
|
go func() {
|
|
|
|
|
start := time.Now()
|
|
|
|
|
hasAccess, err := a.evaluateZanzana(ctx, user, evaluator)
|
|
|
|
|
res <- evalResult{"zanzana", hasAccess, err, time.Since(start)}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
start := time.Now()
|
|
|
|
|
hasAccess, err := a.evaluate(ctx, user, evaluator)
|
|
|
|
|
res <- evalResult{"grafana", hasAccess, err, time.Since(start)}
|
|
|
|
|
}()
|
|
|
|
|
first, second := <-res, <-res
|
|
|
|
|
close(res)
|
|
|
|
|
|
|
|
|
|
if second.runner == "grafana" {
|
|
|
|
|
first, second = second, first
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !errors.Is(second.err, errAccessNotImplemented) {
|
|
|
|
|
if second.err != nil {
|
|
|
|
|
a.log.Error("zanzana evaluation failed", "error", second.err)
|
|
|
|
|
} else if first.decision != second.decision {
|
|
|
|
|
a.log.Warn(
|
|
|
|
|
"zanzana evaluation result does not match grafana",
|
|
|
|
|
"grafana_decision", first.decision,
|
|
|
|
|
"zanana_decision", second.decision,
|
|
|
|
|
"grafana_ms", first.duration,
|
|
|
|
|
"zanzana_ms", second.duration,
|
|
|
|
|
"eval", evaluator.GoString(),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
a.log.Debug("zanzana evaluation is correct", "grafana_ms", first.duration, "zanzana_ms", second.duration)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return first.decision, first.err
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-24 19:29:17 +08:00
|
|
|
func (a *AccessControl) RegisterScopeAttributeResolver(prefix string, resolver accesscontrol.ScopeAttributeResolver) {
|
|
|
|
|
a.resolvers.AddScopeAttributeResolver(prefix, resolver)
|
|
|
|
|
}
|
2024-03-05 15:50:19 +08:00
|
|
|
|
|
|
|
|
func (a *AccessControl) debug(ctx context.Context, ident identity.Requester, msg string, eval accesscontrol.Evaluator) {
|
|
|
|
|
namespace, id := ident.GetNamespacedID()
|
2024-03-08 05:34:22 +08:00
|
|
|
a.log.FromContext(ctx).Debug(msg, "namespace", namespace, "id", id, "orgID", ident.GetOrgID(), "permissions", eval.GoString())
|
2024-03-05 15:50:19 +08:00
|
|
|
}
|