Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Allow more users to rewrap #1813

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all 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
24 changes: 5 additions & 19 deletions service/internal/auth/casbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,22 @@ func NewCasbinEnforcer(c CasbinConfig, logger *logger.Logger) (*Enforcer, error)
// casbinEnforce is a helper function to enforce the policy with casbin
// TODO implement a common type so this can be used for both http and grpc
func (e *Enforcer) Enforce(token jwt.Token, resource, action string) (bool, error) {
var err error
permDeniedError := fmt.Errorf("permission denied")

// extract the role claim from the token
s := e.buildSubjectFromToken(token)
s = append(s, rolePrefix+defaultRole)

if len(s) == 0 {
sub := rolePrefix + defaultRole
e.logger.Debug("enforcing policy", slog.Any("subject", sub), slog.String("resource", resource), slog.String("action", action))
return e.Enforcer.Enforce(sub, resource, action)
}

allowed := false
for _, info := range s {
allowed, err = e.Enforcer.Enforce(info, resource, action)
allowed, err := e.Enforcer.Enforce(info, resource, action)
if err != nil {
e.logger.Error("enforce by role error", slog.String("subject info", info), slog.String("resource", resource), slog.String("action", action), slog.String("error", err.Error()))
continue
}
if allowed {
e.logger.Debug("allowed by policy", slog.String("subject info", info), slog.String("resource", resource), slog.String("action", action))
break
return true, nil
}
}
if !allowed {
e.logger.Debug("permission denied by policy", slog.Any("subject info", s), slog.String("resource", resource), slog.String("action", action))
return false, permDeniedError
}

return true, nil
e.logger.Debug("permission denied by policy", slog.Any("subject.info", s), slog.String("resource", resource), slog.String("action", action))
return false, fmt.Errorf("permission denied")
}

func (e *Enforcer) buildSubjectFromToken(t jwt.Token) casbinSubject {
Expand Down
Loading