admin api: mention admin_token and metrics_token in ListAdminTokensResponse
All checks were successful
ci/woodpecker/pr/debug Pipeline was successful
ci/woodpecker/push/debug Pipeline was successful

This commit is contained in:
Alex 2025-03-11 15:57:29 +01:00
parent 9511b20153
commit ec0da3b644
3 changed files with 33 additions and 5 deletions

View file

@ -2043,7 +2043,6 @@
"GetAdminTokenInfoResponse": { "GetAdminTokenInfoResponse": {
"type": "object", "type": "object",
"required": [ "required": [
"id",
"name", "name",
"expired", "expired",
"scope" "scope"
@ -2061,7 +2060,10 @@
"description": "Whether this admin token is expired already" "description": "Whether this admin token is expired already"
}, },
"id": { "id": {
"type": "string", "type": [
"string",
"null"
],
"description": "Identifier of the admin token (which is also a prefix of the full bearer token)" "description": "Identifier of the admin token (which is also a prefix of the full bearer token)"
}, },
"name": { "name": {

View file

@ -22,7 +22,7 @@ impl RequestHandler for ListAdminTokensRequest {
) -> Result<ListAdminTokensResponse, Error> { ) -> Result<ListAdminTokensResponse, Error> {
let now = now_msec(); let now = now_msec();
let res = garage let mut res = garage
.admin_token_table .admin_token_table
.get_range( .get_range(
&EmptyKey, &EmptyKey,
@ -36,6 +36,32 @@ impl RequestHandler for ListAdminTokensRequest {
.map(|t| admin_token_info_results(t, now)) .map(|t| admin_token_info_results(t, now))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
if garage.config.admin.admin_token.is_some() {
res.insert(
0,
GetAdminTokenInfoResponse {
id: None,
name: "admin_token (from daemon configuration)".into(),
expiration: None,
expired: false,
scope: vec!["*".into()],
},
);
}
if garage.config.admin.metrics_token.is_some() {
res.insert(
1,
GetAdminTokenInfoResponse {
id: None,
name: "metrics_token (from daemon configuration)".into(),
expiration: None,
expired: false,
scope: vec!["Metrics".into()],
},
);
}
Ok(ListAdminTokensResponse(res)) Ok(ListAdminTokensResponse(res))
} }
} }
@ -153,7 +179,7 @@ fn admin_token_info_results(token: &AdminApiToken, now: u64) -> GetAdminTokenInf
let params = token.params().unwrap(); let params = token.params().unwrap();
GetAdminTokenInfoResponse { GetAdminTokenInfoResponse {
id: token.prefix.clone(), id: Some(token.prefix.clone()),
name: params.name.get().to_string(), name: params.name.get().to_string(),
expiration: params.expiration.get().map(|x| { expiration: params.expiration.get().map(|x| {
DateTime::from_timestamp_millis(x as i64).expect("invalid timestamp stored in db") DateTime::from_timestamp_millis(x as i64).expect("invalid timestamp stored in db")

View file

@ -313,7 +313,7 @@ pub struct GetAdminTokenInfoRequest {
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct GetAdminTokenInfoResponse { pub struct GetAdminTokenInfoResponse {
/// Identifier of the admin token (which is also a prefix of the full bearer token) /// Identifier of the admin token (which is also a prefix of the full bearer token)
pub id: String, pub id: Option<String>,
/// Name of the admin API token /// Name of the admin API token
pub name: String, pub name: String,
/// Expiration time and date, formatted according to RFC 3339 /// Expiration time and date, formatted according to RFC 3339