Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to JiHu GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Chenhao Ma
core
Commits
80a31d55
Commit
80a31d55
authored
7 years ago
by
Nathaniel Kofalt
Browse files
Options
Downloads
Patches
Plain Diff
Provide api keys to inputs of type api-key
parent
724c5141
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
api/auth/apikeys.py
+17
-6
17 additions, 6 deletions
api/auth/apikeys.py
api/jobs/handlers.py
+32
-1
32 additions, 1 deletion
api/jobs/handlers.py
with
49 additions
and
7 deletions
api/auth/apikeys.py
+
17
−
6
View file @
80a31d55
...
...
@@ -91,14 +91,25 @@ class JobApiKey(APIKey):
@classmethod
def
generate
(
cls
,
uid
,
job_id
):
"""
Generates an API key for user for use by a specific job
Returns an API key for user for use by a specific job.
Re-uses such a key if it already exists.
"""
api_key
=
cls
.
generate_api_key
(
cls
.
key_type
)
api_key
[
'
uid
'
]
=
uid
api_key
[
'
job
'
]
=
str
(
job_id
)
config
.
db
.
apikeys
.
insert_one
(
api_key
)
return
api_key
[
'
_id
'
]
existing_key
=
config
.
db
.
apikeys
.
find_one
({
'
uid
'
:
uid
,
'
job
'
:
job_id
,
})
if
existing_key
is
not
None
:
return
existing_key
[
'
_id
'
]
else
:
api_key
=
cls
.
generate_api_key
(
cls
.
key_type
)
api_key
[
'
uid
'
]
=
uid
api_key
[
'
job
'
]
=
str
(
job_id
)
config
.
db
.
apikeys
.
insert_one
(
api_key
)
return
api_key
[
'
_id
'
]
@classmethod
def
remove
(
cls
,
job_id
):
...
...
This diff is collapsed.
Click to expand it.
api/jobs/handlers.py
+
32
−
1
View file @
80a31d55
...
...
@@ -5,6 +5,7 @@ import bson
import
os
import
StringIO
from
jsonschema
import
ValidationError
from
urlparse
import
urlparse
from
..
import
upload
from
..
import
util
...
...
@@ -19,6 +20,8 @@ from .. import config
from
.
import
batch
from
..validators
import
validate_data
,
verify_payload_exists
from
..auth.apikeys
import
JobApiKey
from
.gears
import
validate_gear_config
,
get_gears
,
get_gear
,
get_invocation_schema
,
remove_gear
,
upsert_gear
,
suggest_container
,
get_gear_by_name
,
check_for_gear_insertion
from
.jobs
import
Job
,
Logs
from
.batch
import
check_state
,
update
...
...
@@ -403,7 +406,6 @@ class JobHandler(base.RequestHandler):
if
not
self
.
superuser_request
and
not
self
.
user_is_admin
:
self
.
abort
(
403
,
'
Request requires admin
'
)
return
Job
.
get
(
_id
)
def
get_config
(
self
,
_id
):
...
...
@@ -425,6 +427,35 @@ class JobHandler(base.RequestHandler):
if
c
.
get
(
'
config
'
)
is
not
None
and
c
.
get
(
'
inputs
'
)
is
not
None
:
# New behavior
# API keys are only returned in-flight, when the job is running, and not persisted to the job object.
if
j
.
state
==
'
running
'
:
gear
=
get_gear
(
j
.
gear_id
)
for
key
in
gear
[
'
gear
'
][
'
inputs
'
]:
input
=
gear
[
'
gear
'
][
'
inputs
'
][
key
]
if
input
[
'
base
'
]
==
'
api-key
'
:
if
j
.
origin
[
'
type
'
]
!=
'
user
'
:
raise
Exception
(
'
Cannot provide an API key to a job not launched by a user
'
)
uid
=
j
.
origin
[
'
id
'
]
api_key
=
JobApiKey
.
generate
(
uid
,
j
.
id_
)
parsed_url
=
urlparse
(
config
.
get_item
(
'
site
'
,
'
api_url
'
))
if
parsed_url
.
port
!=
443
:
api_key
=
parsed_url
.
hostname
+
'
:
'
+
str
(
parsed_url
.
port
)
+
'
:
'
+
api_key
else
:
api_key
=
parsed_url
.
hostname
+
'
:
'
+
api_key
if
c
.
get
(
'
inputs
'
)
is
None
:
c
[
'
inputs
'
]
=
{}
c
[
'
inputs
'
][
key
]
=
{
'
base
'
:
'
api-key
'
,
'
key
'
:
api_key
}
encoded
=
pseudo_consistent_json_encode
(
c
)
self
.
response
.
app_iter
=
StringIO
.
StringIO
(
encoded
)
else
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment