-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In this commit I add support for sending login notifications to Google Pub/Sub: These command line arguments were added: ``` --ntf-pubsub-base-url <URL> The base url of Google Cloud Storage API [env: UNFTP_NTF_PUBSUB_BASE_URL=] [default: https://pubsub.googleapis.com] --ntf-pubsub-project <PROJECT_ID> The ID of the GCP project where the topic exists [env: UNFTP_NTF_PUBSUB_PROJECT=] --ntf-pubsub-topic <TOPIC_NAME> The name of the Google PubSub topic to publish to [env: UNFTP_NTF_PUBSUB_TOPIC=] ``` that can be used to enable this. Currently only workload identity is supported as authentication mechanism #102
- Loading branch information
1 parent
c4e72c7
commit 210712d
Showing
9 changed files
with
752 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,59 @@ | ||
use async_trait::async_trait; | ||
use serde::__private::fmt::Debug; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
// EventDispatcher can send events to the outside world. | ||
#[async_trait] | ||
pub trait EventDispatcher<T>: Send + Sync + Debug { | ||
async fn dispatch(&self, event: T); | ||
} | ||
|
||
// An EventDispatcher that dispatches to the void of nothingness. | ||
#[derive(Debug)] | ||
pub struct NullEventDispatcher {} | ||
|
||
#[async_trait] | ||
impl EventDispatcher<FTPEvent> for NullEventDispatcher { | ||
async fn dispatch(&self, _event: FTPEvent) { | ||
// Do Nothing | ||
} | ||
} | ||
|
||
// The event that will be sent | ||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct FTPEvent { | ||
pub source_instance: String, | ||
pub hostname: String, | ||
pub payload: FTPEventPayload, | ||
} | ||
|
||
// The event variant | ||
#[derive(Serialize, Deserialize, Debug)] | ||
pub enum FTPEventPayload { | ||
Startup { | ||
libunftp_version: String, | ||
unftp_version: String, | ||
}, | ||
Login { | ||
username: String, | ||
}, | ||
Get { | ||
path: String, | ||
}, | ||
Put { | ||
path: String, | ||
}, | ||
Delete { | ||
path: String, | ||
}, | ||
MakeDir { | ||
path: String, | ||
}, | ||
Rename { | ||
from: String, | ||
to: String, | ||
}, | ||
RemoveDir { | ||
path: String, | ||
}, | ||
} |
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,4 @@ | ||
mod pubsub; | ||
mod workload_identity; | ||
|
||
pub use pubsub::PubsubEventDispatcher; |
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,137 @@ | ||
use crate::domain::{EventDispatcher, FTPEvent}; | ||
use crate::infra::workload_identity; | ||
use async_trait::async_trait; | ||
use http::{header, Method, Request, StatusCode, Uri}; | ||
use hyper::client::connect::dns::GaiResolver; | ||
use hyper::client::HttpConnector; | ||
use hyper::{Body, Client, Response}; | ||
use hyper_rustls::HttpsConnector; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
// Notes: | ||
// - Emulator: https://cloud.google.com/pubsub/docs/emulator | ||
// - virtualenv -p python3 mypython | ||
// - API Docs for publishing: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/publish | ||
// | ||
|
||
/// An [EventDispatcher](crate::domain::EventDispatcher) that dispatches to Google Pub/sub | ||
#[derive(Debug)] | ||
pub struct PubsubEventDispatcher { | ||
log: Arc<slog::Logger>, | ||
api_base_url: String, | ||
project: String, | ||
topic: String, | ||
client: Client<HttpsConnector<HttpConnector>>, | ||
} | ||
|
||
const DEFAULT_SERVICE_ENDPOINT: &str = "https://pubsub.googleapis.com"; | ||
|
||
impl PubsubEventDispatcher { | ||
#[allow(dead_code)] | ||
pub fn new<Str>(log: Arc<slog::Logger>, project: Str, topic: Str) -> Self | ||
where | ||
Str: Into<String>, | ||
{ | ||
Self::with_api_base(log, project.into(), topic.into(), DEFAULT_SERVICE_ENDPOINT.to_owned()) | ||
} | ||
|
||
pub fn with_api_base<Str>(log: Arc<slog::Logger>, project: Str, topic: Str, api_base: Str) -> Self | ||
where | ||
Str: Into<String>, | ||
{ | ||
let client: Client<HttpsConnector<HttpConnector<GaiResolver>>, Body> = | ||
Client::builder().build(HttpsConnector::with_native_roots()); | ||
PubsubEventDispatcher { | ||
log, | ||
api_base_url: api_base.into(), | ||
project: project.into(), | ||
topic: topic.into(), | ||
client, | ||
} | ||
} | ||
|
||
// Gets the authentication token through workload identity mechanisms | ||
async fn get_token(&self) -> Result<String, workload_identity::Error> { | ||
Ok(workload_identity::request_token(None, self.client.clone()) | ||
.await? | ||
.access_token) | ||
} | ||
|
||
// publishes to Google pub/sub | ||
async fn publish(&self, event: FTPEvent) -> Result<(), String> { | ||
let msg = base64::encode(serde_json::to_string(&event).unwrap()); | ||
let b = PubSubRequest { | ||
messages: vec![PubSubMsg { | ||
data: msg.to_owned(), | ||
attributes: HashMap::new(), // TODO Set attribute based on the event type so subscribers can filter. | ||
}], | ||
}; | ||
let body_string = serde_json::to_string(&b).map_err(|e| format!("error marshalling message: {}", e))?; | ||
|
||
// TODO: Implement other auth methods | ||
// FIXME: When testing locally there won't be a token, we might want to handle this better. | ||
let token = self.get_token().await.unwrap_or_else(|_| "".to_owned()); | ||
|
||
let request: Request<Body> = Request::builder() | ||
.uri( | ||
Uri::from_maybe_shared(format!( | ||
"{}/v1/projects/{}/topics/{}:publish", | ||
self.api_base_url, self.project, self.topic | ||
)) | ||
.map_err(|e| format!("invalid request URI: {}", e))?, | ||
) | ||
.header(header::AUTHORIZATION, format!("Bearer {}", token)) | ||
.method(Method::POST) | ||
.body(body_string.into()) | ||
.map_err(|e| format!("error with publish request: {}", e))?; | ||
|
||
let response: Response<Body> = self.client.request(request).await.unwrap(); | ||
if response.status() != StatusCode::OK { | ||
Err(format!("bad HTTP status code received: {}", response.status())) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl EventDispatcher<FTPEvent> for PubsubEventDispatcher { | ||
async fn dispatch(&self, event: FTPEvent) { | ||
let r = self.publish(event).await; | ||
if r.is_err() { | ||
slog::error!(self.log, "Could not dispatch event to pub/sub: {}", r.unwrap_err()); | ||
} | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct PubSubRequest { | ||
messages: Vec<PubSubMsg>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct PubSubMsg { | ||
data: String, | ||
attributes: HashMap<String, String>, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::infra::pubsub::{PubSubMsg, PubSubRequest}; | ||
use std::collections::HashMap; | ||
|
||
#[test] | ||
fn pubub_request_serializes_correctly() { | ||
let payload = base64::encode("123"); | ||
let r = PubSubRequest { | ||
messages: vec![PubSubMsg { | ||
data: payload.to_owned(), | ||
attributes: HashMap::new(), | ||
}], | ||
}; | ||
let json = serde_json::to_string(&r).unwrap(); | ||
assert_eq!(json, "{\"messages\":[{\"data\":\"MTIz\",\"attributes\":{}}]}") | ||
} | ||
} |
Oops, something went wrong.