-
-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): wip add lessor processor to automatically revoke expired …
…tokens
- Loading branch information
1 parent
8b5013f
commit 4166fba
Showing
7 changed files
with
319 additions
and
176 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
implementations/rust/ockam/ockam_api/src/influxdb/influxdb_models.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use crate::influxdb::lease_token::{LeaseToken, TokenStatus}; | ||
use crate::ApiError; | ||
use ockam::identity::Identifier; | ||
use std::str::FromStr; | ||
use time::format_description::well_known::Rfc3339; | ||
use time::OffsetDateTime; | ||
|
||
/// Token returned by InfluxDB API | ||
#[derive(serde::Deserialize, Debug, PartialEq, Eq)] | ||
pub struct InfluxDBTokenResponse { | ||
pub id: String, | ||
pub description: String, | ||
pub token: String, | ||
pub status: String, | ||
#[serde(rename = "createdAt")] | ||
pub created_at: String, | ||
} | ||
|
||
/// Return a `LeaseToken` if it's an Ockam token (i.e., if the `description` contains a valid Ockam metadata). | ||
/// If the metadata is not found, the token will be ignored. | ||
impl TryFrom<InfluxDBTokenResponse> for Option<LeaseToken> { | ||
type Error = ockam_core::Error; | ||
|
||
fn try_from(token: InfluxDBTokenResponse) -> Result<Self, Self::Error> { | ||
match token.unpack_metadata()? { | ||
Some((issued_for, expires)) => Ok(Some(LeaseToken { | ||
id: token.id, | ||
issued_for, | ||
created_at: OffsetDateTime::parse(&token.created_at, &Rfc3339) | ||
.map_err(|_| { | ||
ApiError::core(format!( | ||
"Expected Rfc3339 format for 'created_at' with value {}", | ||
token.created_at | ||
)) | ||
})? | ||
.unix_timestamp(), | ||
expires_at: expires.unix_timestamp(), | ||
status: TokenStatus::from_str(&token.status)?, | ||
token: token.token, | ||
})), | ||
None => Ok(None), | ||
} | ||
} | ||
} | ||
|
||
impl InfluxDBTokenResponse { | ||
/// The InfluxDB tokens only have a description field that can be used to store metadata. | ||
/// The Ockam `LeaseToken` will pack in the description field the identifier that created the token, | ||
/// and its expiration time. | ||
pub fn pack_metadata(requester: &Identifier, expires: OffsetDateTime) -> String { | ||
format!("OCKAM:{}:{}", requester, expires.unix_timestamp()).to_string() | ||
} | ||
|
||
/// Unpack the metadata from the description field. | ||
pub fn unpack_metadata(&self) -> ockam_core::Result<Option<(Identifier, OffsetDateTime)>> { | ||
let segments = self.description.split(':').collect::<Vec<_>>(); | ||
match segments[..] { | ||
["OCKAM", identifier, expires] => { | ||
let identifier = Identifier::try_from(identifier)?; | ||
let expires_timestamp: i64 = expires | ||
.parse() | ||
.map_err(|_| ApiError::core("Invalid 'expires' timestamp"))?; | ||
let expires = OffsetDateTime::from_unix_timestamp(expires_timestamp) | ||
.map_err(|_| ApiError::core("Invalid 'expires' timestamp"))?; | ||
Ok(Some((identifier, expires))) | ||
} | ||
_ => Ok(None), | ||
} | ||
} | ||
} | ||
|
||
#[derive(serde::Deserialize, Debug, PartialEq, Eq)] | ||
pub struct InfluxDBListTokensResponse { | ||
#[serde(rename = "authorizations")] | ||
pub tokens: Vec<InfluxDBTokenResponse>, | ||
#[serde(rename = "links")] | ||
pub pagination: InfluxDBPagination, | ||
} | ||
|
||
#[derive(serde::Deserialize, Debug, PartialEq, Eq)] | ||
pub struct InfluxDBPagination { | ||
pub next: Option<String>, | ||
pub prev: Option<String>, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::influxdb::lease_token::{LeaseToken, TokenStatus}; | ||
use std::str::FromStr; | ||
use time::OffsetDateTime; | ||
|
||
#[test] | ||
fn lease_token_from_influxdb_token() { | ||
let identifier = "I0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; | ||
let expires_at = OffsetDateTime::now_utc() + core::time::Duration::from_secs(60); | ||
let expires_at_timestamp = expires_at.unix_timestamp(); | ||
let created_at = "2024-09-12T16:23:54Z"; | ||
let created_at_timestamp = 1726158234; | ||
let token = InfluxDBTokenResponse { | ||
id: "token_id".to_string(), | ||
description: format!("OCKAM:{identifier}:{expires_at_timestamp}"), | ||
token: "token".to_string(), | ||
status: "active".to_string(), | ||
created_at: created_at.to_string(), | ||
}; | ||
let expected = LeaseToken { | ||
id: "token_id".to_string(), | ||
issued_for: Identifier::from_str(identifier).unwrap(), | ||
created_at: created_at_timestamp, | ||
expires_at: expires_at_timestamp, | ||
token: "token".to_string(), | ||
status: TokenStatus::Active, | ||
}; | ||
let got = { | ||
let got: Option<LeaseToken> = token.try_into().unwrap(); | ||
got.unwrap() | ||
}; | ||
assert_eq!(got, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod influxdb_models; | ||
mod lease_token; | ||
pub mod token_lessor_node_service; | ||
mod token_lessor_processor; | ||
mod token_lessor_worker; | ||
|
||
pub use token_lessor_node_service::StartInfluxDBLeaseManagerRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.