2021-05-13 13:59:12 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2021-09-10 10:23:26 -05:00
|
|
|
glob "github.com/bmatcuk/doublestar/v4"
|
2021-05-13 13:59:12 -05:00
|
|
|
"github.com/gorilla/mux"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
2021-12-29 10:14:56 -05:00
|
|
|
"zotregistry.io/zot/pkg/common"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/log"
|
2022-08-16 03:57:09 -05:00
|
|
|
localCtx "zotregistry.io/zot/pkg/requestcontext"
|
2021-05-13 13:59:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-11-18 12:35:28 -05:00
|
|
|
// method actions.
|
|
|
|
Create = "create"
|
|
|
|
Read = "read"
|
|
|
|
Update = "update"
|
|
|
|
Delete = "delete"
|
|
|
|
// behaviour actions.
|
|
|
|
DetectManifestCollision = "detectManifestCollision"
|
2023-07-07 11:27:10 -05:00
|
|
|
BASIC = "Basic"
|
|
|
|
BEARER = "Bearer"
|
|
|
|
OPENID = "OpenID"
|
2021-05-13 13:59:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// AccessController authorizes users to act on resources.
|
|
|
|
type AccessController struct {
|
2021-06-08 15:11:18 -05:00
|
|
|
Config *config.AccessControlConfig
|
2021-05-13 13:59:12 -05:00
|
|
|
Log log.Logger
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
func NewAccessController(conf *config.Config) *AccessController {
|
|
|
|
if conf.HTTP.AccessControl == nil {
|
|
|
|
return &AccessController{
|
|
|
|
Config: &config.AccessControlConfig{},
|
|
|
|
Log: log.NewLogger(conf.Log.Level, conf.Log.Output),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 13:59:12 -05:00
|
|
|
return &AccessController{
|
2023-07-07 11:27:10 -05:00
|
|
|
Config: conf.HTTP.AccessControl,
|
|
|
|
Log: log.NewLogger(conf.Log.Level, conf.Log.Output),
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:35:28 -05:00
|
|
|
// getGlobPatterns gets glob patterns from authz config on which <username> has <action> perms.
|
2021-09-10 10:23:26 -05:00
|
|
|
// used to filter /v2/_catalog repositories based on user rights.
|
2023-03-08 14:47:15 -05:00
|
|
|
func (ac *AccessController) getGlobPatterns(username string, groups []string, action string) map[string]bool {
|
2021-09-10 10:23:26 -05:00
|
|
|
globPatterns := make(map[string]bool)
|
2021-05-13 13:59:12 -05:00
|
|
|
|
2021-09-10 10:23:26 -05:00
|
|
|
for pattern, policyGroup := range ac.Config.Repositories {
|
2022-11-18 12:35:28 -05:00
|
|
|
if username == "" {
|
|
|
|
// check anonymous policy
|
|
|
|
if common.Contains(policyGroup.AnonymousPolicy, action) {
|
|
|
|
globPatterns[pattern] = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// check default policy (authenticated user)
|
|
|
|
if common.Contains(policyGroup.DefaultPolicy, action) {
|
|
|
|
globPatterns[pattern] = true
|
|
|
|
}
|
2021-09-10 10:23:26 -05:00
|
|
|
}
|
2022-11-18 12:35:28 -05:00
|
|
|
|
2021-09-10 10:23:26 -05:00
|
|
|
// check user based policy
|
|
|
|
for _, p := range policyGroup.Policies {
|
2022-11-18 12:35:28 -05:00
|
|
|
if common.Contains(p.Users, username) && common.Contains(p.Actions, action) {
|
2021-09-10 10:23:26 -05:00
|
|
|
globPatterns[pattern] = true
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
}
|
2021-09-10 10:23:26 -05:00
|
|
|
|
2023-03-08 14:47:15 -05:00
|
|
|
// check group based policy
|
|
|
|
for _, group := range groups {
|
|
|
|
for _, p := range policyGroup.Policies {
|
|
|
|
if common.Contains(p.Groups, group) && common.Contains(p.Actions, action) {
|
|
|
|
globPatterns[pattern] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 10:23:26 -05:00
|
|
|
// if not allowed then mark it
|
|
|
|
if _, ok := globPatterns[pattern]; !ok {
|
|
|
|
globPatterns[pattern] = false
|
|
|
|
}
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
|
2021-09-10 10:23:26 -05:00
|
|
|
return globPatterns
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// can verifies if a user can do action on repository.
|
2023-03-08 14:47:15 -05:00
|
|
|
func (ac *AccessController) can(ctx context.Context, username, action, repository string) bool {
|
2021-05-13 13:59:12 -05:00
|
|
|
can := false
|
2021-09-10 10:23:26 -05:00
|
|
|
|
|
|
|
var longestMatchedPattern string
|
|
|
|
|
|
|
|
for pattern := range ac.Config.Repositories {
|
|
|
|
matched, err := glob.Match(pattern, repository)
|
|
|
|
if err == nil {
|
|
|
|
if matched && len(pattern) > len(longestMatchedPattern) {
|
|
|
|
longestMatchedPattern = pattern
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 14:47:15 -05:00
|
|
|
acCtx, err := localCtx.GetAccessControlContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
userGroups := acCtx.Groups
|
|
|
|
|
2021-09-10 10:23:26 -05:00
|
|
|
// check matched repo based policy
|
|
|
|
pg, ok := ac.Config.Repositories[longestMatchedPattern]
|
2021-05-13 13:59:12 -05:00
|
|
|
if ok {
|
2023-03-08 14:47:15 -05:00
|
|
|
can = ac.isPermitted(userGroups, username, action, pg)
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
// check admins based policy
|
2021-05-13 13:59:12 -05:00
|
|
|
if !can {
|
2021-12-29 10:14:56 -05:00
|
|
|
if ac.isAdmin(username) && common.Contains(ac.Config.AdminPolicy.Actions, action) {
|
2021-05-13 13:59:12 -05:00
|
|
|
can = true
|
|
|
|
}
|
2023-03-08 14:47:15 -05:00
|
|
|
|
|
|
|
if ac.isAnyGroupInAdminPolicy(userGroups) && common.Contains(ac.Config.AdminPolicy.Actions, action) {
|
|
|
|
can = true
|
|
|
|
}
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return can
|
|
|
|
}
|
|
|
|
|
|
|
|
// isAdmin .
|
|
|
|
func (ac *AccessController) isAdmin(username string) bool {
|
2021-12-29 10:14:56 -05:00
|
|
|
return common.Contains(ac.Config.AdminPolicy.Users, username)
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
|
2023-03-08 14:47:15 -05:00
|
|
|
func (ac *AccessController) isAnyGroupInAdminPolicy(userGroups []string) bool {
|
|
|
|
for _, group := range userGroups {
|
|
|
|
if common.Contains(ac.Config.AdminPolicy.Groups, group) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AccessController) getUserGroups(username string) []string {
|
|
|
|
var groupNames []string
|
|
|
|
|
|
|
|
for groupName, group := range ac.Config.Groups {
|
|
|
|
for _, user := range group.Users {
|
|
|
|
// find if the user is part of any groups
|
|
|
|
if user == username {
|
|
|
|
groupNames = append(groupNames, groupName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return groupNames
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:13:06 -05:00
|
|
|
// getContext updates an AccessControlContext for a user/anonymous and returns a context.Context containing it.
|
|
|
|
func (ac *AccessController) getContext(acCtx *localCtx.AccessControlContext, request *http.Request) context.Context {
|
|
|
|
readGlobPatterns := ac.getGlobPatterns(acCtx.Username, acCtx.Groups, Read)
|
|
|
|
dmcGlobPatterns := ac.getGlobPatterns(acCtx.Username, acCtx.Groups, DetectManifestCollision)
|
2023-03-08 14:47:15 -05:00
|
|
|
|
|
|
|
acCtx.ReadGlobPatterns = readGlobPatterns
|
|
|
|
acCtx.DmcGlobPatterns = dmcGlobPatterns
|
|
|
|
|
2023-04-27 10:13:06 -05:00
|
|
|
if ac.isAdmin(acCtx.Username) {
|
2022-08-16 03:57:09 -05:00
|
|
|
acCtx.IsAdmin = true
|
2021-05-13 13:59:12 -05:00
|
|
|
} else {
|
2022-08-16 03:57:09 -05:00
|
|
|
acCtx.IsAdmin = false
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
|
2022-08-16 03:57:09 -05:00
|
|
|
authzCtxKey := localCtx.GetContextKey()
|
2023-03-08 14:47:15 -05:00
|
|
|
ctx := context.WithValue(request.Context(), authzCtxKey, *acCtx)
|
2021-05-13 13:59:12 -05:00
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
// getAuthnMiddlewareContext builds ac context(allowed to read repos and if user is admin) and returns it.
|
|
|
|
func (ac *AccessController) getAuthnMiddlewareContext(authnType string, request *http.Request) context.Context {
|
|
|
|
amwCtx := localCtx.AuthnMiddlewareContext{
|
|
|
|
AuthnType: authnType,
|
|
|
|
}
|
|
|
|
|
|
|
|
amwCtxKey := localCtx.GetAuthnMiddlewareCtxKey()
|
|
|
|
ctx := context.WithValue(request.Context(), amwCtxKey, amwCtx)
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2021-05-13 13:59:12 -05:00
|
|
|
// isPermitted returns true if username can do action on a repository policy.
|
2023-03-08 14:47:15 -05:00
|
|
|
func (ac *AccessController) isPermitted(userGroups []string, username, action string,
|
|
|
|
policyGroup config.PolicyGroup,
|
|
|
|
) bool {
|
2021-05-13 13:59:12 -05:00
|
|
|
var result bool
|
2023-03-08 14:47:15 -05:00
|
|
|
|
2021-05-13 13:59:12 -05:00
|
|
|
// check repo/system based policies
|
2021-12-13 14:23:31 -05:00
|
|
|
for _, p := range policyGroup.Policies {
|
2021-12-29 10:14:56 -05:00
|
|
|
if common.Contains(p.Users, username) && common.Contains(p.Actions, action) {
|
2021-05-13 13:59:12 -05:00
|
|
|
result = true
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2023-03-08 14:47:15 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if userGroups != nil {
|
|
|
|
for _, p := range policyGroup.Policies {
|
|
|
|
if common.Contains(p.Actions, action) {
|
|
|
|
for _, group := range p.Groups {
|
|
|
|
if common.Contains(userGroups, group) {
|
|
|
|
result = true
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check defaultPolicy
|
|
|
|
if !result {
|
2022-07-14 10:13:46 -05:00
|
|
|
if common.Contains(policyGroup.DefaultPolicy, action) && username != "" {
|
|
|
|
result = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check anonymousPolicy
|
|
|
|
if !result {
|
|
|
|
if common.Contains(policyGroup.AnonymousPolicy, action) && username == "" {
|
2021-05-13 13:59:12 -05:00
|
|
|
result = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-07-05 11:37:52 -05:00
|
|
|
func BaseAuthzHandler(ctlr *Controller) mux.MiddlewareFunc {
|
2021-05-13 13:59:12 -05:00
|
|
|
return func(next http.Handler) http.Handler {
|
2021-12-13 14:23:31 -05:00
|
|
|
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
2023-07-05 11:37:52 -05:00
|
|
|
/* NOTE:
|
|
|
|
since we only do READ actions in extensions, this middleware is enough for them because
|
|
|
|
it populates the context with user relevant data to be processed by each individual extension
|
|
|
|
*/
|
2021-05-13 13:59:12 -05:00
|
|
|
|
2023-05-10 12:09:53 -05:00
|
|
|
if request.Method == http.MethodOptions {
|
|
|
|
next.ServeHTTP(response, request)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
// request comes from bearer authn, bypass it
|
|
|
|
authnMwCtx, err := localCtx.GetAuthnMiddlewareContext(request.Context())
|
|
|
|
if err != nil || (authnMwCtx != nil && authnMwCtx.AuthnType == BEARER) {
|
|
|
|
next.ServeHTTP(response, request)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-10 10:23:26 -05:00
|
|
|
// bypass authz for /v2/ route
|
|
|
|
if request.RequestURI == "/v2/" {
|
|
|
|
next.ServeHTTP(response, request)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
acCtrlr := NewAccessController(ctlr.Config)
|
2022-03-10 07:25:15 -05:00
|
|
|
|
2022-08-12 07:18:41 -05:00
|
|
|
var identity string
|
2023-04-27 10:13:06 -05:00
|
|
|
|
|
|
|
// anonymous context
|
|
|
|
acCtx := &localCtx.AccessControlContext{}
|
2022-07-14 10:13:46 -05:00
|
|
|
|
2023-04-27 10:13:06 -05:00
|
|
|
// get username from context made in authn.go
|
2023-07-19 11:27:04 -05:00
|
|
|
if ctlr.Config.IsBasicAuthnEnabled() {
|
2023-04-27 10:13:06 -05:00
|
|
|
// get access control context made in authn.go if authn is enabled
|
|
|
|
acCtx, err = localCtx.GetAccessControlContext(request.Context())
|
|
|
|
if err != nil { // should never happen
|
2023-07-07 11:27:10 -05:00
|
|
|
authFail(response, request, ctlr.Config.HTTP.Realm, ctlr.Config.HTTP.Auth.FailDelay)
|
2023-04-27 10:13:06 -05:00
|
|
|
|
|
|
|
return
|
2022-07-14 10:13:46 -05:00
|
|
|
}
|
2023-04-27 10:13:06 -05:00
|
|
|
|
|
|
|
identity = acCtx.Username
|
2022-03-10 07:25:15 -05:00
|
|
|
}
|
|
|
|
|
2022-08-12 07:18:41 -05:00
|
|
|
if request.TLS != nil {
|
|
|
|
verifiedChains := request.TLS.VerifiedChains
|
|
|
|
// still no identity, get it from TLS certs
|
|
|
|
if identity == "" && verifiedChains != nil &&
|
|
|
|
len(verifiedChains) > 0 && len(verifiedChains[0]) > 0 {
|
|
|
|
for _, cert := range request.TLS.PeerCertificates {
|
|
|
|
identity = cert.Subject.CommonName
|
|
|
|
}
|
2023-03-09 13:43:26 -05:00
|
|
|
|
2022-08-12 07:18:41 -05:00
|
|
|
// if we still don't have an identity
|
|
|
|
if identity == "" {
|
|
|
|
acCtrlr.Log.Info().Msg("couldn't get identity from TLS certificate")
|
2023-07-07 11:27:10 -05:00
|
|
|
authFail(response, request, ctlr.Config.HTTP.Realm, ctlr.Config.HTTP.Auth.FailDelay)
|
2023-04-27 10:13:06 -05:00
|
|
|
|
|
|
|
return
|
2022-08-12 07:18:41 -05:00
|
|
|
}
|
2023-06-21 08:06:53 -05:00
|
|
|
|
|
|
|
// assign identity to authz context, needed for extensions
|
|
|
|
acCtx.Username = identity
|
2022-08-12 07:18:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:13:06 -05:00
|
|
|
ctx := acCtrlr.getContext(acCtx, request)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2023-07-05 11:37:52 -05:00
|
|
|
next.ServeHTTP(response, request.WithContext(ctx)) //nolint:contextcheck
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DistSpecAuthzHandler(ctlr *Controller) mux.MiddlewareFunc {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
|
|
if request.Method == http.MethodOptions {
|
|
|
|
next.ServeHTTP(response, request)
|
2022-08-16 03:57:09 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
// request comes from bearer authn, bypass it
|
|
|
|
authnMwCtx, err := localCtx.GetAuthnMiddlewareContext(request.Context())
|
|
|
|
if err != nil || (authnMwCtx != nil && authnMwCtx.AuthnType == BEARER) {
|
|
|
|
next.ServeHTTP(response, request)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-05 11:37:52 -05:00
|
|
|
vars := mux.Vars(request)
|
|
|
|
resource := vars["name"]
|
|
|
|
reference, ok := vars["reference"]
|
|
|
|
|
|
|
|
acCtrlr := NewAccessController(ctlr.Config)
|
|
|
|
|
|
|
|
var identity string
|
|
|
|
|
|
|
|
// get acCtx built in authn and previous authz middlewares
|
|
|
|
acCtx, err := localCtx.GetAccessControlContext(request.Context())
|
|
|
|
if err != nil { // should never happen
|
2023-07-07 11:27:10 -05:00
|
|
|
authFail(response, request, ctlr.Config.HTTP.Realm, ctlr.Config.HTTP.Auth.FailDelay)
|
2023-07-05 11:37:52 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// get username from context made in authn.go
|
|
|
|
identity = acCtx.Username
|
|
|
|
|
2021-05-13 13:59:12 -05:00
|
|
|
var action string
|
2021-12-13 14:23:31 -05:00
|
|
|
if request.Method == http.MethodGet || request.Method == http.MethodHead {
|
2022-11-18 12:35:28 -05:00
|
|
|
action = Read
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if request.Method == http.MethodPut || request.Method == http.MethodPatch || request.Method == http.MethodPost {
|
2021-05-13 13:59:12 -05:00
|
|
|
// assume user wants to create
|
2022-11-18 12:35:28 -05:00
|
|
|
action = Create
|
2021-05-13 13:59:12 -05:00
|
|
|
// if we get a reference (tag)
|
|
|
|
if ok {
|
2021-12-13 14:23:31 -05:00
|
|
|
is := ctlr.StoreController.GetImageStore(resource)
|
2021-05-13 13:59:12 -05:00
|
|
|
tags, err := is.GetImageTags(resource)
|
2022-03-10 07:25:15 -05:00
|
|
|
// if repo exists and request's tag exists then action is UPDATE
|
2021-12-29 10:14:56 -05:00
|
|
|
if err == nil && common.Contains(tags, reference) && reference != "latest" {
|
2022-11-18 12:35:28 -05:00
|
|
|
action = Update
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if request.Method == http.MethodDelete {
|
2022-11-18 12:35:28 -05:00
|
|
|
action = Delete
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
|
2023-07-05 11:37:52 -05:00
|
|
|
can := acCtrlr.can(request.Context(), identity, action, resource) //nolint:contextcheck
|
2021-05-13 13:59:12 -05:00
|
|
|
if !can {
|
2023-07-07 11:27:10 -05:00
|
|
|
common.AuthzFail(response, request, ctlr.Config.HTTP.Realm, ctlr.Config.HTTP.Auth.FailDelay)
|
2021-05-13 13:59:12 -05:00
|
|
|
} else {
|
2023-07-05 11:37:52 -05:00
|
|
|
next.ServeHTTP(response, request) //nolint:contextcheck
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|