Skip to content

add durable sse subscriptions #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rand = "0.9"
serde = "1"
serde_json = "1"
sha1 = "0.10"
thiserror = "2"
time = "0.3"

[lints.rust]
Expand Down
4 changes: 2 additions & 2 deletions src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ fn text_response(status: StatusCode, text: &str) -> Response {
Response::from_status(status).with_body_text_plain(&format!("{text}\n"))
}

pub fn post_keys(req: Request) -> Response {
if !req.fastly_key_is_valid() {
pub fn post_keys(fastly_authed: bool, _req: Request) -> Response {
if !fastly_authed {
return text_response(
StatusCode::UNAUTHORIZED,
"Fastly-Key header invalid or not specified",
Expand Down
72 changes: 56 additions & 16 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ fn validate_token(token: &str, key: &[u8]) -> Result<Capabilities, TokenError> {
Ok(caps)
}

pub fn create_token(
readable: Vec<String>,
writable: Vec<String>,
key_id: &str,
key: &[u8],
) -> String {
let key = HS256Key::from_bytes(key).with_key_id(key_id);

let claims = Claims::with_custom_claims(
CustomClaims {
x_fastly_read: readable,
x_fastly_write: writable,
},
Duration::from_hours(1),
);

key.authenticate(claims)
.expect("token creation should always succeed")
}

#[derive(Debug)]
pub enum AuthorizationError {
Token(TokenError),
Expand All @@ -92,7 +112,11 @@ impl From<TokenError> for AuthorizationError {
}

pub trait Authorizor {
fn validate_token(&self, token: &str) -> Result<Capabilities, AuthorizationError>;
fn validate_token(
&self,
token: &str,
internal_key: Option<&[u8]>,
) -> Result<Capabilities, AuthorizationError>;
}

pub struct KVStoreAuthorizor {
Expand All @@ -108,7 +132,11 @@ impl KVStoreAuthorizor {
}

impl Authorizor for KVStoreAuthorizor {
fn validate_token(&self, token: &str) -> Result<Capabilities, AuthorizationError> {
fn validate_token(
&self,
token: &str,
internal_key: Option<&[u8]>,
) -> Result<Capabilities, AuthorizationError> {
let Ok(metadata) = Token::decode_metadata(token) else {
return Err(AuthorizationError::Token(TokenError::Invalid));
};
Expand All @@ -117,28 +145,40 @@ impl Authorizor for KVStoreAuthorizor {
return Err(AuthorizationError::Token(TokenError::NoKeyId));
};

let store = match kv_store::KVStore::open(&self.store_name) {
Ok(Some(store)) => store,
Ok(None) => return Err(AuthorizationError::StoreNotFound),
Err(_) => return Err(AuthorizationError::StoreError),
};

let v = match store.lookup(key_id) {
Ok(mut lookup) => lookup.take_body_bytes(),
Err(kv_store::KVStoreError::ItemNotFound) => {
return Err(AuthorizationError::KeyNotFound)
let key = if key_id == "internal" {
let Some(internal_key) = internal_key else {
return Err(AuthorizationError::KeyNotFound);
};

internal_key.to_vec()
} else {
let store = match kv_store::KVStore::open(&self.store_name) {
Ok(Some(store)) => store,
Ok(None) => return Err(AuthorizationError::StoreNotFound),
Err(_) => return Err(AuthorizationError::StoreError),
};

match store.lookup(key_id) {
Ok(mut lookup) => lookup.take_body_bytes(),
Err(kv_store::KVStoreError::ItemNotFound) => {
return Err(AuthorizationError::KeyNotFound)
}
Err(_) => return Err(AuthorizationError::StoreError),
}
Err(_) => return Err(AuthorizationError::StoreError),
};

Ok(validate_token(token, &v)?)
Ok(validate_token(token, &key)?)
}
}

pub struct TestAuthorizor;

impl Authorizor for TestAuthorizor {
fn validate_token(&self, token: &str) -> Result<Capabilities, AuthorizationError> {
fn validate_token(
&self,
token: &str,
_internal_key: Option<&[u8]>,
) -> Result<Capabilities, AuthorizationError> {
Ok(validate_token(token, b"notasecret")?)
}
}
Expand All @@ -160,7 +200,7 @@ mod tests {
let key = HS256Key::from_bytes(b"notasecret");
let token = key.authenticate(claims).unwrap();

let caps = TestAuthorizor.validate_token(&token).unwrap();
let caps = TestAuthorizor.validate_token(&token, None).unwrap();
assert!(caps.can_subscribe("readable"));
assert!(!caps.can_subscribe("foo"));
assert!(caps.can_publish("writable"));
Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Config {
pub mqtt_enabled: bool,
pub admin_enabled: bool,
pub publish_token: String,
pub internal_key: Vec<u8>,
}

impl Default for Config {
Expand All @@ -17,6 +18,7 @@ impl Default for Config {
mqtt_enabled: true,
admin_enabled: true,
publish_token: String::new(),
internal_key: Vec::new(),
}
}
}
Expand Down Expand Up @@ -101,6 +103,12 @@ impl Source for ConfigAndSecretStoreSource {
Ok(None) => {}
Err(_) => return Err(ConfigError::StoreError),
}

match store.try_get("internal-key") {
Ok(Some(v)) => config.internal_key = v.plaintext().to_vec(),
Ok(None) => {}
Err(_) => return Err(ConfigError::StoreError),
}
}

Ok(config)
Expand Down
Loading
Loading