|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/anuvu/zot/pkg/log" |
| 11 | + "github.com/gorilla/mux" |
| 12 | +) |
| 13 | + |
| 14 | +type contextKey int |
| 15 | + |
| 16 | +const ( |
| 17 | + // actions |
| 18 | + CREATE = "create" |
| 19 | + READ = "read" |
| 20 | + UPDATE = "update" |
| 21 | + DELETE = "delete" |
| 22 | + |
| 23 | + // request-local context key |
| 24 | + authzCtxKey contextKey = 0 |
| 25 | +) |
| 26 | + |
| 27 | +type AccessControlConfig struct { |
| 28 | + Repositories Repositories |
| 29 | + AdminPolicy Policy |
| 30 | +} |
| 31 | + |
| 32 | +type Repositories map[string]PolicyGroup |
| 33 | + |
| 34 | +type PolicyGroup struct { |
| 35 | + Policies []Policy |
| 36 | + DefaultPolicy []string |
| 37 | +} |
| 38 | + |
| 39 | +type Policy struct { |
| 40 | + Users []string |
| 41 | + Actions []string |
| 42 | +} |
| 43 | + |
| 44 | +// AccessController authorizes users to act on resources. |
| 45 | +type AccessController struct { |
| 46 | + Config *AccessControlConfig |
| 47 | + Log log.Logger |
| 48 | +} |
| 49 | + |
| 50 | +// AccessControlContext context passed down to http.Handlers. |
| 51 | +type AccessControlContext struct { |
| 52 | + userAllowedRepos []string |
| 53 | + isAdmin bool |
| 54 | +} |
| 55 | + |
| 56 | +func NewAccessController(config *Config) *AccessController { |
| 57 | + return &AccessController{ |
| 58 | + Config: config.AccessControl, |
| 59 | + Log: log.NewLogger(config.Log.Level, config.Log.Output), |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// getReadRepos get repositories from config file that the user has READ perms. |
| 64 | +func (ac *AccessController) getReadRepos(username string) []string { |
| 65 | + var repos []string |
| 66 | + |
| 67 | + for r, pg := range ac.Config.Repositories { |
| 68 | + for _, p := range pg.Policies { |
| 69 | + if (contains(p.Users, username) && contains(p.Actions, READ)) || |
| 70 | + contains(pg.DefaultPolicy, READ) { |
| 71 | + repos = append(repos, r) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return repos |
| 77 | +} |
| 78 | + |
| 79 | +// can verifies if a user can do action on repository. |
| 80 | +func (ac *AccessController) can(username, action, repository string) bool { |
| 81 | + can := false |
| 82 | + // check repo based policy |
| 83 | + pg, ok := ac.Config.Repositories[repository] |
| 84 | + if ok { |
| 85 | + can = isPermitted(username, action, pg) |
| 86 | + } |
| 87 | + |
| 88 | + //check admins based policy |
| 89 | + if !can { |
| 90 | + if ac.isAdmin(username) && contains(ac.Config.AdminPolicy.Actions, action) { |
| 91 | + can = true |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return can |
| 96 | +} |
| 97 | + |
| 98 | +// isAdmin . |
| 99 | +func (ac *AccessController) isAdmin(username string) bool { |
| 100 | + return contains(ac.Config.AdminPolicy.Users, username) |
| 101 | +} |
| 102 | + |
| 103 | +// getContext builds ac context(allowed to read repos and if user is admin) and returns it. |
| 104 | +func (ac *AccessController) getContext(username string, r *http.Request) context.Context { |
| 105 | + userAllowedRepos := ac.getReadRepos(username) |
| 106 | + acCtx := AccessControlContext{userAllowedRepos: userAllowedRepos} |
| 107 | + |
| 108 | + if ac.isAdmin(username) { |
| 109 | + acCtx.isAdmin = true |
| 110 | + } else { |
| 111 | + acCtx.isAdmin = false |
| 112 | + } |
| 113 | + |
| 114 | + ctx := context.WithValue(r.Context(), authzCtxKey, acCtx) |
| 115 | + |
| 116 | + return ctx |
| 117 | +} |
| 118 | + |
| 119 | +// isPermitted returns true if username can do action on a repository policy. |
| 120 | +func isPermitted(username, action string, pg PolicyGroup) bool { |
| 121 | + var result bool |
| 122 | + // check repo/system based policies |
| 123 | + for _, p := range pg.Policies { |
| 124 | + if contains(p.Users, username) && contains(p.Actions, action) { |
| 125 | + result = true |
| 126 | + break |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // check defaultPolicy |
| 131 | + if !result { |
| 132 | + if contains(pg.DefaultPolicy, action) { |
| 133 | + result = true |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return result |
| 138 | +} |
| 139 | + |
| 140 | +func contains(slice []string, item string) bool { |
| 141 | + for _, v := range slice { |
| 142 | + if item == v { |
| 143 | + return true |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return false |
| 148 | +} |
| 149 | + |
| 150 | +func containsRepo(slice []string, item string) bool { |
| 151 | + for _, v := range slice { |
| 152 | + if strings.HasPrefix(item, v) { |
| 153 | + return true |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return false |
| 158 | +} |
| 159 | + |
| 160 | +func AuthzHandler(c *Controller) mux.MiddlewareFunc { |
| 161 | + return func(next http.Handler) http.Handler { |
| 162 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 163 | + vars := mux.Vars(r) |
| 164 | + resource := vars["name"] |
| 165 | + reference, ok := vars["reference"] |
| 166 | + |
| 167 | + ac := NewAccessController(c.Config) |
| 168 | + username := getUsername(r) |
| 169 | + ctx := ac.getContext(username, r) |
| 170 | + |
| 171 | + if r.RequestURI == "/v2/_catalog" || r.RequestURI == "/v2/" { |
| 172 | + next.ServeHTTP(w, r.WithContext(ctx)) |
| 173 | + return |
| 174 | + } |
| 175 | + |
| 176 | + var action string |
| 177 | + if r.Method == http.MethodGet || r.Method == http.MethodHead { |
| 178 | + action = READ |
| 179 | + } |
| 180 | + |
| 181 | + if r.Method == http.MethodPut || r.Method == http.MethodPatch || r.Method == http.MethodPost { |
| 182 | + // assume user wants to create |
| 183 | + action = CREATE |
| 184 | + // if we get a reference (tag) |
| 185 | + if ok { |
| 186 | + is := c.StoreController.GetImageStore(resource) |
| 187 | + tags, err := is.GetImageTags(resource) |
| 188 | + // if repo exists and request's tag doesn't exist yet then action is UPDATE |
| 189 | + if err == nil && contains(tags, reference) && reference != "latest" { |
| 190 | + action = UPDATE |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + if r.Method == http.MethodDelete { |
| 196 | + action = DELETE |
| 197 | + } |
| 198 | + |
| 199 | + can := ac.can(username, action, resource) |
| 200 | + if !can { |
| 201 | + authzFail(w, c.Config.HTTP.Realm, c.Config.HTTP.Auth.FailDelay) |
| 202 | + } else { |
| 203 | + next.ServeHTTP(w, r.WithContext(ctx)) |
| 204 | + } |
| 205 | + }) |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +func getUsername(r *http.Request) string { |
| 210 | + // this should work because it worked in auth middleware |
| 211 | + basicAuth := r.Header.Get("Authorization") |
| 212 | + s := strings.SplitN(basicAuth, " ", 2) |
| 213 | + b, _ := base64.StdEncoding.DecodeString(s[1]) |
| 214 | + pair := strings.SplitN(string(b), ":", 2) |
| 215 | + |
| 216 | + return pair[0] |
| 217 | +} |
| 218 | + |
| 219 | +func isBearerAuthEnabled(config *Config) bool { |
| 220 | + if config.HTTP.Auth != nil && |
| 221 | + config.HTTP.Auth.Bearer != nil && |
| 222 | + config.HTTP.Auth.Bearer.Cert != "" && |
| 223 | + config.HTTP.Auth.Bearer.Realm != "" && |
| 224 | + config.HTTP.Auth.Bearer.Service != "" { |
| 225 | + return true |
| 226 | + } |
| 227 | + |
| 228 | + return false |
| 229 | +} |
| 230 | + |
| 231 | +func authzFail(w http.ResponseWriter, realm string, delay int) { |
| 232 | + time.Sleep(time.Duration(delay) * time.Second) |
| 233 | + w.Header().Set("WWW-Authenticate", realm) |
| 234 | + w.Header().Set("Content-Type", "application/json") |
| 235 | + WriteJSON(w, http.StatusForbidden, NewErrorList(NewError(DENIED))) |
| 236 | +} |
0 commit comments