-
Notifications
You must be signed in to change notification settings - Fork 393
refactoring object-operation endpoints to use permission descriptors #9416
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,10 @@ type Controller struct { | |
licenseManager license.Manager | ||
} | ||
|
||
type AuthOpts struct { | ||
callback func(w http.ResponseWriter, r *http.Request, code int, v interface{}) | ||
} | ||
|
||
var usageCounter = stats.NewUsageCounter() | ||
|
||
func NewController(cfg config.Config, catalog *catalog.Catalog, authenticator auth.Authenticator, authService auth.Service, authenticationService authentication.Service, blockAdapter block.Adapter, metadataManager auth.MetadataManager, migrator Migrator, collector stats.Collector, actions actionsHandler, auditChecker AuditChecker, logger logging.Logger, sessionStore sessions.Store, pathProvider upload.PathProvider, usageReporter stats.UsageReporterOperations, licenseManager license.Manager) *Controller { | ||
|
@@ -3377,12 +3381,7 @@ func (c *Controller) DeleteObject(w http.ResponseWriter, r *http.Request, reposi | |
} | ||
|
||
func (c *Controller) UploadObjectPreflight(w http.ResponseWriter, r *http.Request, repository, branch string, params apigen.UploadObjectPreflightParams) { | ||
if !c.authorize(w, r, permissions.Node{ | ||
Permission: permissions.Permission{ | ||
Action: permissions.WriteObjectAction, | ||
Resource: permissions.ObjectArn(repository, params.Path), | ||
}, | ||
}) { | ||
if !c.authorizeReq(w, r, "UploadObjectPreflight", permissions.PermissionParams{Repository: &repository, Path: ¶ms.Path}, nil) { | ||
return | ||
} | ||
|
||
|
@@ -3393,12 +3392,7 @@ func (c *Controller) UploadObjectPreflight(w http.ResponseWriter, r *http.Reques | |
} | ||
|
||
func (c *Controller) UploadObject(w http.ResponseWriter, r *http.Request, repository, branch string, params apigen.UploadObjectParams) { | ||
if !c.authorize(w, r, permissions.Node{ | ||
Permission: permissions.Permission{ | ||
Action: permissions.WriteObjectAction, | ||
Resource: permissions.ObjectArn(repository, params.Path), | ||
}, | ||
}) { | ||
if !c.authorizeReq(w, r, "UploadObject", permissions.PermissionParams{Repository: &repository, Path: ¶ms.Path}, nil) { | ||
return | ||
} | ||
ctx := r.Context() | ||
|
@@ -3563,12 +3557,7 @@ func (c *Controller) UploadObject(w http.ResponseWriter, r *http.Request, reposi | |
} | ||
|
||
func (c *Controller) StageObject(w http.ResponseWriter, r *http.Request, body apigen.StageObjectJSONRequestBody, repository, branch string, params apigen.StageObjectParams) { | ||
if !c.authorize(w, r, permissions.Node{ | ||
Permission: permissions.Permission{ | ||
Action: permissions.WriteObjectAction, | ||
Resource: permissions.ObjectArn(repository, params.Path), | ||
}, | ||
}) { | ||
if !c.authorizeReq(w, r, "StageObject", permissions.PermissionParams{Repository: &repository, Path: ¶ms.Path}, nil) { | ||
return | ||
} | ||
ctx := r.Context() | ||
|
@@ -4383,12 +4372,7 @@ func (c *Controller) RestoreStatus(w http.ResponseWriter, r *http.Request, repos | |
} | ||
|
||
func (c *Controller) CreateSymlinkFile(w http.ResponseWriter, r *http.Request, repository, branch string, params apigen.CreateSymlinkFileParams) { | ||
if !c.authorize(w, r, permissions.Node{ | ||
Permission: permissions.Permission{ | ||
Action: permissions.WriteObjectAction, | ||
Resource: permissions.ObjectArn(repository, branch), | ||
}, | ||
}) { | ||
if !c.authorizeReq(w, r, "CreateSymlinkFile", permissions.PermissionParams{Repository: &repository, Path: &branch}, nil) { | ||
return | ||
} | ||
ctx := r.Context() | ||
|
@@ -4582,14 +4566,10 @@ func (c *Controller) LogCommits(w http.ResponseWriter, r *http.Request, reposito | |
} | ||
|
||
func (c *Controller) HeadObject(w http.ResponseWriter, r *http.Request, repository, ref string, params apigen.HeadObjectParams) { | ||
if !c.authorizeCallback(w, r, permissions.Node{ | ||
Permission: permissions.Permission{ | ||
Action: permissions.ReadObjectAction, | ||
Resource: permissions.ObjectArn(repository, params.Path), | ||
}, | ||
}, func(w http.ResponseWriter, r *http.Request, code int, v interface{}) { | ||
writeResponse(w, r, code, nil) | ||
}) { | ||
if !c.authorizeReq(w, r, "HeadObject", permissions.PermissionParams{Repository: &repository, Path: ¶ms.Path}, | ||
&AuthOpts{callback: func(w http.ResponseWriter, r *http.Request, code int, v interface{}) { | ||
writeResponse(w, r, code, nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the different callback here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be clear, I didn't change the callback that is used here. but I believe the reason is that authorize() uses writeError as callback, which adds an error message to the response body when authorizeCallback fails. HeadObject is a Head endpoint, so it shouldn't have a body in the response. |
||
}}) { | ||
return | ||
} | ||
ctx := r.Context() | ||
|
@@ -4724,12 +4704,7 @@ func (c *Controller) GetMetadataObject(w http.ResponseWriter, r *http.Request, r | |
} | ||
|
||
func (c *Controller) GetObject(w http.ResponseWriter, r *http.Request, repository, ref string, params apigen.GetObjectParams) { | ||
if !c.authorize(w, r, permissions.Node{ | ||
Permission: permissions.Permission{ | ||
Action: permissions.ReadObjectAction, | ||
Resource: permissions.ObjectArn(repository, params.Path), | ||
}, | ||
}) { | ||
if !c.authorizeReq(w, r, "GetObject", permissions.PermissionParams{Repository: &repository, Path: ¶ms.Path}, nil) { | ||
return | ||
} | ||
ctx := r.Context() | ||
|
@@ -4947,12 +4922,7 @@ func (c *Controller) ListObjects(w http.ResponseWriter, r *http.Request, reposit | |
} | ||
|
||
func (c *Controller) StatObject(w http.ResponseWriter, r *http.Request, repository, ref string, params apigen.StatObjectParams) { | ||
if !c.authorize(w, r, permissions.Node{ | ||
Permission: permissions.Permission{ | ||
Action: permissions.ReadObjectAction, | ||
Resource: permissions.ObjectArn(repository, params.Path), | ||
}, | ||
}) { | ||
if !c.authorizeReq(w, r, "StatObject", permissions.PermissionParams{Repository: &repository, Path: ¶ms.Path}, nil) { | ||
return | ||
} | ||
ctx := r.Context() | ||
|
@@ -5015,12 +4985,7 @@ func (c *Controller) StatObject(w http.ResponseWriter, r *http.Request, reposito | |
} | ||
|
||
func (c *Controller) UpdateObjectUserMetadata(w http.ResponseWriter, r *http.Request, body apigen.UpdateObjectUserMetadataJSONRequestBody, repository, branch string, params apigen.UpdateObjectUserMetadataParams) { | ||
if !c.authorize(w, r, permissions.Node{ | ||
Permission: permissions.Permission{ | ||
Action: permissions.WriteObjectAction, | ||
Resource: permissions.ObjectArn(repository, params.Path), | ||
}, | ||
}) { | ||
if !c.authorizeReq(w, r, "UpdateObjectUserMetadata", permissions.PermissionParams{Repository: &repository, Path: ¶ms.Path}, nil) { | ||
return | ||
} | ||
ctx := r.Context() | ||
|
@@ -5036,12 +5001,7 @@ func (c *Controller) UpdateObjectUserMetadata(w http.ResponseWriter, r *http.Req | |
} | ||
|
||
func (c *Controller) GetUnderlyingProperties(w http.ResponseWriter, r *http.Request, repository, ref string, params apigen.GetUnderlyingPropertiesParams) { | ||
if !c.authorize(w, r, permissions.Node{ | ||
Permission: permissions.Permission{ | ||
Action: permissions.ReadObjectAction, | ||
Resource: permissions.ObjectArn(repository, params.Path), | ||
}, | ||
}) { | ||
if !c.authorizeReq(w, r, "GetUnderlyingProperties", permissions.PermissionParams{Repository: &repository, Path: ¶ms.Path}, nil) { | ||
return | ||
} | ||
ctx := r.Context() | ||
|
@@ -5944,6 +5904,19 @@ func (c *Controller) authorize(w http.ResponseWriter, r *http.Request, perms per | |
return c.authorizeCallback(w, r, perms, writeError) | ||
} | ||
|
||
func (c *Controller) authorizeReq(w http.ResponseWriter, r *http.Request, operationId string, params permissions.PermissionParams, opts *AuthOpts) bool { | ||
desc := permissions.GetPermissionDescriptor(operationId) | ||
if desc == nil { | ||
c.Logger.Error(fmt.Sprintf("missing permission descriptor for %s", operationId)) | ||
return false | ||
} | ||
callback := writeError | ||
if opts != nil && opts.callback != nil { | ||
callback = opts.callback | ||
} | ||
return c.authorizeCallback(w, r, desc.Permission(params), callback) | ||
} | ||
|
||
func (c *Controller) isNameValid(name, nameType string) (bool, string) { | ||
// URLs are % encoded. Allowing % signs in entity names would | ||
// limit the ability to use these entity names in the URL for both | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add at least one, and possibly all, of these:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently all endpoints except 2 call authorize() which calls authorizeCallback() with writeError as callback. There are 2 endpoints which call authorizeCallback directly with a different callback.
So when a function is supplied here it is used as callback instead of writeError.
I used the same signature with the same parameter names as in authorizeCallback, because I think it's clearer if they match. after your comment I changed the variable names in both places to be more informative, tell me what you think.