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

Create REST endpoint for POSTing job models #2031

Merged
merged 6 commits into from
Jun 19, 2017

Conversation

kotfic
Copy link
Contributor

@kotfic kotfic commented May 25, 2017

Does not allow posting to /job unless jobs.rest.create_job is set as a scope
on the current token - regardless of whether TokenScope.USER_AUTH is set.

@kotfic
Copy link
Contributor Author

kotfic commented May 25, 2017

@zachmullen can you comment on whether or not this is the right/best approach? thanks!

@kotfic
Copy link
Contributor Author

kotfic commented May 25, 2017

See: #2018

@kotfic
Copy link
Contributor Author

kotfic commented May 25, 2017

btw, I found this to be a little bit confusing - i expected when setting @access.token(scope='FOOBAR') that the endpoint would throw a 403 if the token didn't have scope 'FOOBAR' set on it.

However, because ensureTokenScopes returns immediately if the user is logged in, it effectively means a logged in user has ALL custom scopes defined. I didn't see a built-in way to handle a situation where i wanted to protect an endpoint with a custom scope regardless of user authentication status.

@filtermodel(model='job', plugin='jobs')
@access.token(scope=constants.REST_CREATE_JOB_TOKEN_SCOPE)
@autoDescribeRoute(
Description('Create a job model via a RESTful endpoint')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to put "via a RESTful endpoint" :)


def load(info):
info['apiRoot'].job = job_rest.Job()
events.bind('rest.post.job.before', 'jobs', _authorizeRestJobCreation)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is all in the same plugin, let's just move this logic into the REST route handler itself. That will improve readability via lexical proximity of the logic, and won't preclude behavioral modification by downstreams.

if not tokenModel.hasScope(token, constants.REST_CREATE_JOB_TOKEN_SCOPE):
raise AccessException(
'Invalid token scope.\n'
'Required: %s.\n' % (constants.REST_CREATE_JOB_TOKEN_SCOPE))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern appears in a few places, it's probably worth adding a standardized requireScope method into the token model that raises an exception like this.

Copy link
Contributor

@mgrauer mgrauer Jun 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this also help with the recent issue we found where invalidly scoped tokens were throwing 401s and saying "not logged in" when really it was the case that the token didn't have the correct scope? If we can improve that error handling as a part of this PR that would be rad.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wouldn't help with that particular case. I'm not actually sure how to engineer a solution for that at this time.

from girder.plugins.jobs.constants import JobStatus, JOB_HANDLER_LOCAL
from girder.plugins.jobs.constants import (JobStatus,
JOB_HANDLER_LOCAL,
REST_CREATE_JOB_TOKEN_SCOPE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't seem necessary.

@zachmullen
Copy link
Member

btw, I found this to be a little bit confusing - i expected when setting @access.token(scope='FOOBAR') that the endpoint would throw a 403 if the token didn't have scope 'FOOBAR' set on it.

Right, that's the behavior we use to support OAuth-provider-like authorization control. One way to support these sorts of use cases is to add another flag to the access decorators, e.g.

@access.token(scope=FOO, required=True)

The required flag (for lack of a more expressive name) would default to False, but if True would actually require the listed scopes. This PR would be a good place to add that, and also the token model requireScope method that would underlie it.

Does not allow posting unless jobs.rest.create_job is set as a scope
on the token.  This is set automatically when using createJobToken()
@dorukozturk dorukozturk force-pushed the create-worker-job-model branch 2 times, most recently from db04b3c to 0287f0a Compare June 15, 2017 15:35
@dorukozturk dorukozturk changed the title [WIP] Create REST endpoint for POSTing job models Create REST endpoint for POSTing job models Jun 15, 2017
@dorukozturk
Copy link
Contributor

@kotfic, @zachmullen This is ready for a review.


if not self.hasScope(token, scope):
raise AccessException('Invalid token scope.'
'Required: %s.' % (scope))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can fit this all on one line, but if not, we'll need to add a space between the sentences.

Description('Create a job model')
.param('title', '', required=True)
.param('type', '', required=True)
.modelParam('parentId', 'Id of the parent job.', model='job',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In user-facing text, we should use "ID" rather than "Id".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zachmullen I applied those changes.

@dorukozturk dorukozturk merged commit db04886 into master Jun 19, 2017
@dorukozturk dorukozturk deleted the create-worker-job-model branch June 19, 2017 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants