Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
rust-gitlab
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Geoffrey Arthaud
rust-gitlab
Commits
cdd5830d
Unverified
Commit
cdd5830d
authored
1 year ago
by
Geoffrey Arthaud
Browse files
Options
Downloads
Patches
Plain Diff
api: add specific connection checking with job token
parent
3657f7c9
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#390397
passed
1 year ago
Stage: prep
Stage: build
Stage: test
Stage: advice
Stage: external
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CHANGELOG.md
+6
-0
6 additions, 0 deletions
CHANGELOG.md
src/auth.rs
+69
-14
69 additions, 14 deletions
src/auth.rs
with
75 additions
and
14 deletions
CHANGELOG.md
+
6
−
0
View file @
cdd5830d
# v0.1609.2 (unreleased)
## Fixes
*
Job token authentication is now compliant with its limited permissions.
# v0.1609.1
# v0.1609.1
## Fixes
## Fixes
...
...
This diff is collapsed.
Click to expand it.
src/auth.rs
+
69
−
14
View file @
cdd5830d
...
@@ -7,8 +7,10 @@
...
@@ -7,8 +7,10 @@
use
http
::{
HeaderMap
,
HeaderValue
};
use
http
::{
HeaderMap
,
HeaderValue
};
use
log
::
error
;
use
log
::
error
;
use
serde
::
Deserialize
;
use
serde
::
Deserialize
;
use
std
::
env
;
use
thiserror
::
Error
;
use
thiserror
::
Error
;
use
crate
::
api
::
projects
::
packages
::
Packages
;
use
crate
::
api
::
users
::
CurrentUser
;
use
crate
::
api
::
users
::
CurrentUser
;
use
crate
::
api
::{
self
,
AsyncQuery
,
Query
};
use
crate
::
api
::{
self
,
AsyncQuery
,
Query
};
...
@@ -20,6 +22,22 @@ pub enum AuthError {
...
@@ -20,6 +22,22 @@ pub enum AuthError {
#[from]
#[from]
source
:
http
::
header
::
InvalidHeaderValue
,
source
:
http
::
header
::
InvalidHeaderValue
,
},
},
/// The CI environment is misconfigured
#[error(
"CI environment is missing `CI_PROJECT_ID`"
)]
CiEnvironmentMissing
,
/// The CI environment is misconfigured
#[error(
"CI environment has a non-integer `CI_PROJECT_ID`"
)]
CiEnvironmentInvalid
,
}
impl
AuthError
{
fn
ci_environment_missing
()
->
Self
{
Self
::
CiEnvironmentMissing
}
fn
ci_environment_invalid
()
->
Self
{
Self
::
CiEnvironmentInvalid
}
}
}
#[derive(Deserialize,
Debug)]
#[derive(Deserialize,
Debug)]
...
@@ -29,6 +47,13 @@ struct UserPublic {
...
@@ -29,6 +47,13 @@ struct UserPublic {
_username
:
String
,
_username
:
String
,
}
}
#[derive(Deserialize,
Debug)]
struct
ProjectPackage
{
/// The username.
#[serde(rename
=
"id"
)]
_id
:
u64
,
}
type
AuthResult
<
T
>
=
Result
<
T
,
AuthError
>
;
type
AuthResult
<
T
>
=
Result
<
T
,
AuthError
>
;
/// A Gitlab API token
/// A Gitlab API token
...
@@ -85,11 +110,22 @@ impl Auth {
...
@@ -85,11 +110,22 @@ impl Auth {
where
where
C
:
api
::
Client
,
C
:
api
::
Client
,
{
{
if
let
Self
::
None
=
self
{
match
self
{
// There does not seem to be an unparameterized endpoint that can be used to reliably
Self
::
None
=>
{
// detect whether the connection will work or not.
// There does not seem to be an unparameterized endpoint that can be used to reliably
}
else
{
// detect whether the connection will work or not.
let
_
:
UserPublic
=
CurrentUser
::
builder
()
.build
()
.unwrap
()
.query
(
api
)
?
;
},
Self
::
JobToken
(
_
)
=>
{
let
project_id
=
Self
::
_ci_project_id
()
?
;
let
_
:
Vec
<
ProjectPackage
>
=
Packages
::
builder
()
.project
(
project_id
)
.build
()
.unwrap
()
.query
(
api
)
?
;
},
Self
::
Token
(
_
)
|
Self
::
OAuth2
(
_
)
=>
{
let
_
:
UserPublic
=
CurrentUser
::
builder
()
.build
()
.unwrap
()
.query
(
api
)
?
;
},
}
}
Ok
(())
Ok
(())
...
@@ -99,17 +135,36 @@ impl Auth {
...
@@ -99,17 +135,36 @@ impl Auth {
where
where
C
:
api
::
AsyncClient
+
Sync
,
C
:
api
::
AsyncClient
+
Sync
,
{
{
if
let
Self
::
None
=
self
{
match
self
{
// There does not seem to be an unparameterized endpoint that can be used to reliably
Self
::
None
=>
{
// detect whether the connection will work or not.
// There does not seem to be an unparameterized endpoint that can be used to reliably
}
else
{
// detect whether the connection will work or not.
let
_
:
UserPublic
=
CurrentUser
::
builder
()
},
.build
()
Self
::
JobToken
(
_
)
=>
{
.unwrap
()
let
project_id
=
Self
::
_ci_project_id
()
?
;
.query_async
(
api
)
let
_
:
Vec
<
ProjectPackage
>
=
Packages
::
builder
()
.await
?
;
.project
(
project_id
)
.build
()
.unwrap
()
.query_async
(
api
)
.await
?
;
},
Self
::
Token
(
_
)
|
Self
::
OAuth2
(
_
)
=>
{
let
_
:
UserPublic
=
CurrentUser
::
builder
()
.build
()
.unwrap
()
.query_async
(
api
)
.await
?
;
},
}
}
Ok
(())
Ok
(())
}
}
fn
_ci_project_id
()
->
Result
<
u64
,
AuthError
>
{
env
::
var
(
"CI_PROJECT_ID"
)
.map_err
(|
_
|
AuthError
::
ci_environment_missing
())
?
.parse
::
<
u64
>
()
.map_err
(|
_
|
AuthError
::
ci_environment_invalid
())
}
}
}
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