Skip to content

Commit 9a1a944

Browse files
authored
add location header to post machine identity response (#195)
* add location header to post machine identity response * bump up nebula version into 0.0.4
1 parent 16e67e8 commit 9a1a944

File tree

4 files changed

+56
-23
lines changed

4 files changed

+56
-23
lines changed

Cargo.lock

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resolver = "2"
33
members = ["crates/*"]
44

55
[workspace.package]
6-
version = "0.0.3"
6+
version = "0.0.4"
77
edition = "2021"
88
authors = ["John Choi <john@cremit.io>", "Boris Im <boris@cremit.io>"]
99
repository = "https://github.com/cremithq/nebula"

crates/nebula-authorization/src/domain/machine_identity/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ pub struct MachineIdentity {
2222
deleted: bool,
2323
}
2424

25+
pub struct MachineIdentityCreationResult {
26+
pub id: Ulid,
27+
}
28+
2529
impl MachineIdentity {
2630
pub(crate) fn update_attributes(&mut self, attributes: &[(&str, &str)]) {
2731
let current_attributes: HashSet<(&str, &str)> =
@@ -139,11 +143,11 @@ impl MachineIdentityService {
139143
transaction: &DatabaseTransaction,
140144
owner_claim: &NebulaClaim,
141145
label: &str,
142-
) -> Result<()> {
143-
let machine_identity_id = UlidId::new(Ulid::new());
146+
) -> Result<MachineIdentityCreationResult> {
147+
let machine_identity_id = Ulid::new();
144148
let now = Utc::now();
145149
machine_identity::ActiveModel {
146-
id: Set(machine_identity_id),
150+
id: Set(UlidId::new(machine_identity_id)),
147151
owner_gid: Set(owner_claim.gid.to_owned()),
148152
label: Set(label.to_owned()),
149153
created_at: Set(now),
@@ -152,7 +156,7 @@ impl MachineIdentityService {
152156
.insert(transaction)
153157
.await?;
154158

155-
Ok(())
159+
Ok(MachineIdentityCreationResult { id: machine_identity_id })
156160
}
157161

158162
pub async fn get_machine_identities(&self, transaction: &DatabaseTransaction) -> Result<Vec<MachineIdentity>> {

crates/nebula-authorization/src/server/router/mod.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::sync::Arc;
1+
use std::{str::FromStr, sync::Arc};
22

33
use axum::{
4-
extract::{Path, Query, Request, State},
5-
http::{HeaderMap, StatusCode},
4+
extract::{OriginalUri, Path, Query, Request, State},
5+
http::{header, uri::PathAndQuery, HeaderMap, HeaderValue, StatusCode, Uri},
66
middleware::{self, Next},
77
response::{IntoResponse, Redirect, Response},
88
routing::{delete, get, patch, post},
@@ -18,6 +18,7 @@ use nebula_token::{
1818
use sea_orm::{DatabaseTransaction, DbErr, TransactionTrait};
1919
use serde::{Deserialize, Serialize};
2020
use thiserror::Error;
21+
use tracing::error;
2122
use ulid::Ulid;
2223

2324
use crate::{
@@ -371,6 +372,9 @@ enum MachineIdentityError {
371372
#[error("Error occurrred by database")]
372373
#[status(StatusCode::INTERNAL_SERVER_ERROR)]
373374
DatabaseError(#[from] DbErr),
375+
#[error("failed to create location header")]
376+
#[status(StatusCode::INTERNAL_SERVER_ERROR)]
377+
FailedToCreateLocationHeader,
374378
}
375379

376380
impl From<machine_identity::Error> for MachineIdentityError {
@@ -382,18 +386,43 @@ impl From<machine_identity::Error> for MachineIdentityError {
382386
}
383387

384388
async fn handle_post_machine_identity(
389+
OriginalUri(uri): OriginalUri,
385390
Path(workspace_name): Path<String>,
386391
State(application): State<Arc<Application>>,
387392
Extension(claim): Extension<NebulaClaim>,
388393
Json(payload): Json<PostMachineIdentityRequest>,
389394
) -> Result<impl IntoResponse, MachineIdentityError> {
390395
let transaction = application.database_connection.begin_with_workspace_scope(&workspace_name).await?;
391396

392-
application.machine_identity_service.register_machine_identity(&transaction, &claim, &payload.label).await?;
397+
let result =
398+
application.machine_identity_service.register_machine_identity(&transaction, &claim, &payload.label).await?;
399+
400+
let mut parts = uri.into_parts();
401+
402+
let new_machine_identity_path = PathAndQuery::from_str(&format!(
403+
"{}/{}",
404+
parts.path_and_query.ok_or_else(|| {
405+
error!("failed to get request path while creating location url");
406+
MachineIdentityError::FailedToCreateLocationHeader
407+
})?,
408+
result.id
409+
))
410+
.inspect_err(|e| error!(error = %e, "failed to create uri path from new machine identity resource"))
411+
.map_err(|_| MachineIdentityError::FailedToCreateLocationHeader)?;
412+
413+
parts.path_and_query = Some(new_machine_identity_path);
414+
let location_header_uri = Uri::from_parts(parts)
415+
.inspect_err(|e| error!(error = %e, "failed to create location uri from new machine identity resource"))
416+
.map_err(|_| MachineIdentityError::FailedToCreateLocationHeader)?;
417+
let location_header_value = HeaderValue::from_str(&location_header_uri.to_string())
418+
.inspect_err(
419+
|e| error!(error = %e, "failed to create location header value from new machine identity resource"),
420+
)
421+
.map_err(|_| MachineIdentityError::FailedToCreateLocationHeader)?;
393422

394423
transaction.commit().await?;
395424

396-
Ok(StatusCode::CREATED)
425+
Ok((StatusCode::CREATED, [(header::LOCATION, location_header_value)]))
397426
}
398427

399428
#[derive(Serialize)]

0 commit comments

Comments
 (0)