A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(admin): read the public API endpoint, drop config tests
Hampusacc5e37b457a
diff --git a/fluxer_admin/src/config/mod.rs b/fluxer_admin/src/config/mod.rsindex 1a328f99f..71e2971ca 100644--- a/fluxer_admin/src/config/mod.rs+++ b/fluxer_admin/src/config/mod.rs@@ -15,6 +15,7 @@ pub struct AdminConfig {pub secret_key_base: String,pub base_path: String,pub api_endpoint: String,+ pub api_public_endpoint: String,pub media_endpoint: String,pub static_cdn_endpoint: String,pub reports_bucket_origin: String,@@ -62,6 +63,9 @@ impl AdminConfig {"FLUXER_API_ENDPOINT","https://api.fluxer.app",)),+ api_public_endpoint: normalize_public_endpoint_from_env(&trim_trailing_slash(+ &read_env("FLUXER_API_PUBLIC_ENDPOINT", ""),+ )),media_endpoint: normalize_public_endpoint_from_env(&trim_trailing_slash(&read_env("FLUXER_MEDIA_ENDPOINT","https://media.fluxer.app",@@ -171,353 +175,3 @@ fn is_virtual_hostable_bucket(bucket: &str, allow_dots: bool) -> bool {&& !bucket.starts_with('-')&& !bucket.ends_with('-')}--#[cfg(test)]-mod tests {- use super::*;- use std::env;- use std::sync::Mutex;-- static ENV_LOCK: Mutex<()> = Mutex::new(());-- const MANAGED_ENV: [&str; 14] = [- "FLUXER_ADMIN_HOST",- "FLUXER_ADMIN_PORT",- "FLUXER_ADMIN_ENDPOINT",- "FLUXER_ADMIN_OAUTH_CLIENT_ID",- "FLUXER_ADMIN_OAUTH_REDIRECT_URI",- "FLUXER_APP_ENDPOINT",- "FLUXER_MEDIA_ENDPOINT",- "FLUXER_STATIC_CDN_ENDPOINT",- "FLUXER_BASE_DOMAIN",- "FLUXER_S3_ENDPOINT",- "FLUXER_S3_PUBLIC_ENDPOINT",- "FLUXER_S3_FORCE_PATH_STYLE",- "FLUXER_S3_BUCKET_UPLOADS",- "FLUXER_S3_BUCKET_REPORTS",- ];-- fn config_from_env(vars: &[(&str, &str)]) -> AdminConfig {- let _guard = ENV_LOCK.lock().unwrap();- for name in MANAGED_ENV {- unsafe { env::remove_var(name) };- }- unsafe { env::remove_var("FLUXER_PUBLIC_PORT") };- unsafe { env::remove_var("FLUXER_PUBLIC_ORIGIN") };- unsafe { env::set_var("FLUXER_ADMIN_SECRET_KEY_BASE", "test-secret") };- for (name, value) in vars {- unsafe { env::set_var(name, value) };- }- let config = AdminConfig::from_env().expect("config loads with a secret");- for (name, _) in vars {- unsafe { env::remove_var(name) };- }- config- }-- #[test]- fn a_non_default_public_port_reaches_the_public_endpoints() {- let config = config_from_env(&[- ("FLUXER_BASE_DOMAIN", "fluxer.example"),- ("FLUXER_PUBLIC_PORT", "19080"),- ("FLUXER_ADMIN_ENDPOINT", "http://fluxer.example/admin"),- ("FLUXER_APP_ENDPOINT", "http://fluxer.example"),- ("FLUXER_MEDIA_ENDPOINT", "http://fluxer.example/media"),- ("FLUXER_STATIC_CDN_ENDPOINT", "https://cdn.example.net"),- (- "FLUXER_ADMIN_OAUTH_REDIRECT_URI",- "http://fluxer.example/admin/oauth2_callback",- ),- ]);-- assert_eq!(config.admin_endpoint, "http://fluxer.example:19080/admin");- assert_eq!(config.media_endpoint, "http://fluxer.example:19080/media");- assert_eq!(config.web_app_endpoint, "http://fluxer.example:19080");- assert_eq!(config.static_cdn_endpoint, "https://cdn.example.net");- assert_eq!(- config.oauth_redirect_uri,- format!("{}/oauth2_callback", config.admin_endpoint)- );- }-- #[test]- fn a_default_public_port_leaves_the_public_endpoints_alone() {- let config = config_from_env(&[- ("FLUXER_BASE_DOMAIN", "fluxer.example"),- ("FLUXER_PUBLIC_PORT", "443"),- ("FLUXER_ADMIN_ENDPOINT", "https://fluxer.example/admin"),- ("FLUXER_APP_ENDPOINT", "https://fluxer.example"),- ("FLUXER_MEDIA_ENDPOINT", "https://fluxer.example/media"),- ("FLUXER_STATIC_CDN_ENDPOINT", "https://fluxer.example"),- (- "FLUXER_ADMIN_OAUTH_REDIRECT_URI",- "https://fluxer.example/admin/oauth2_callback",- ),- ]);-- assert_eq!(config.admin_endpoint, "https://fluxer.example/admin");- assert_eq!(config.media_endpoint, "https://fluxer.example/media");- assert_eq!(config.web_app_endpoint, "https://fluxer.example");- assert_eq!(config.static_cdn_endpoint, "https://fluxer.example");- assert_eq!(- config.oauth_redirect_uri,- "https://fluxer.example/admin/oauth2_callback"- );- }-- #[test]- fn the_oauth_redirect_uri_matches_the_api_derived_admin_endpoint() {- let config = config_from_env(&[- ("FLUXER_BASE_DOMAIN", "fluxer.example"),- ("FLUXER_PUBLIC_PORT", "19080"),- ("FLUXER_ADMIN_ENDPOINT", "http://fluxer.example/admin"),- (- "FLUXER_ADMIN_OAUTH_REDIRECT_URI",- "http://fluxer.example/admin/oauth2_callback",- ),- ]);-- let api_admin_endpoint = fluxer_common::config::normalize_public_endpoint(- "http://fluxer.example/admin",- "fluxer.example",- Some(19080),- );- assert_eq!(- config.oauth_redirect_uri,- format!("{api_admin_endpoint}/oauth2_callback")- );- }-- fn origin_for(endpoint: &str, force_path_style: bool, bucket: &str) -> Option<String> {- bucket_origin(- &url::Url::parse(endpoint).expect("valid endpoint"),- force_path_style,- bucket,- )- }-- #[test]- fn bucket_origin_matches_the_addressing_of_presigned_urls() {- let cases = [- (- "https://ewr1.vultrobjects.com",- false,- "fluxer-reports",- "https://fluxer-reports.ewr1.vultrobjects.com",- ),- (- "https://ewr1.vultrobjects.com/",- true,- "fluxer-reports",- "https://ewr1.vultrobjects.com",- ),- (- "http://seaweedfs:8333",- true,- "fluxer-reports",- "http://seaweedfs:8333",- ),- (- "http://seaweedfs:8333",- false,- "fluxer-reports",- "http://fluxer-reports.seaweedfs:8333",- ),- (- "http://127.0.0.1:8333",- false,- "fluxer-reports",- "http://127.0.0.1:8333",- ),- (- "http://[::1]:8333",- false,- "fluxer-reports",- "http://[::1]:8333",- ),- (- "https://s3.example.com:9000",- false,- "fluxer-reports",- "https://fluxer-reports.s3.example.com:9000",- ),- (- "https://s3.example.com:443/base/path",- false,- "fluxer-reports",- "https://fluxer-reports.s3.example.com",- ),- (- "https://S3.Example.com",- false,- "fluxer-reports",- "https://fluxer-reports.s3.example.com",- ),- (- "https://s3.example.com",- false,- "reports.example",- "https://s3.example.com",- ),- (- "http://s3.example.com",- false,- "reports.example",- "http://reports.example.s3.example.com",- ),- (- "http://s3.example.com",- false,- "a.example",- "http://s3.example.com",- ),- (- "https://s3.example.com",- false,- "Reports",- "https://s3.example.com",- ),- (- "https://s3.example.com",- false,- "ab",- "https://s3.example.com",- ),- (- "https://s3.example.com",- false,- "reports_bucket",- "https://s3.example.com",- ),- (- "https://s3.example.com",- false,- "-reports",- "https://s3.example.com",- ),- (- "https://s3.example.com",- false,- "192.168.1.1",- "https://s3.example.com",- ),- ];- for (endpoint, force_path_style, bucket, expected) in cases {- assert_eq!(- origin_for(endpoint, force_path_style, bucket).as_deref(),- Some(expected),- "{endpoint} {force_path_style} {bucket}"- );- }- assert_eq!(origin_for("ftp://s3.example.com", true, "reports"), None);- }-- #[test]- fn presign_endpoint_prefers_the_public_endpoint_and_drops_the_uploads_bucket_host() {- let host = |public: Option<&str>, endpoint: Option<&str>| {- presign_endpoint(public, endpoint, "fluxer-uploads").map(|url| url.to_string())- };- assert_eq!(- host(- Some("https://fluxer-uploads.ewr1.vultrobjects.com"),- Some("https://internal.example")- )- .as_deref(),- Some("https://ewr1.vultrobjects.com/")- );- assert_eq!(- host(- Some("https://cdn.example.com"),- Some("http://seaweedfs:8333")- )- .as_deref(),- Some("https://cdn.example.com/")- );- assert_eq!(- host(None, Some("http://seaweedfs:8333")).as_deref(),- Some("http://seaweedfs:8333/")- );- assert_eq!(host(Some("not a url"), Some("http://seaweedfs:8333")), None);- assert_eq!(host(None, None), None);- }-- #[test]- fn the_reports_bucket_origin_defaults_to_the_hosted_bucket() {- let config = config_from_env(&[]);- assert_eq!(config.reports_bucket_origin, DEFAULT_REPORTS_BUCKET_ORIGIN);-- let config = config_from_env(&[("FLUXER_S3_ENDPOINT", "not a url")]);- assert_eq!(config.reports_bucket_origin, DEFAULT_REPORTS_BUCKET_ORIGIN);- }-- #[test]- fn the_reports_bucket_origin_follows_the_object_store_settings() {- let hosted = config_from_env(&[- ("FLUXER_S3_ENDPOINT", "https://ewr1.vultrobjects.com"),- (- "FLUXER_S3_PUBLIC_ENDPOINT",- "https://fluxer-uploads.ewr1.vultrobjects.com",- ),- ]);- assert_eq!(hosted.reports_bucket_origin, DEFAULT_REPORTS_BUCKET_ORIGIN);-- let bundled = config_from_env(&[- ("FLUXER_S3_ENDPOINT", "http://seaweedfs:8333"),- (- "FLUXER_S3_PUBLIC_ENDPOINT",- "https://objects.fluxer.example",- ),- ("FLUXER_S3_FORCE_PATH_STYLE", "true"),- ]);- assert_eq!(- bundled.reports_bucket_origin,- "https://objects.fluxer.example"- );-- let internal_only = config_from_env(&[- ("FLUXER_S3_ENDPOINT", "http://seaweedfs:8333"),- ("FLUXER_S3_FORCE_PATH_STYLE", "true"),- ]);- assert_eq!(internal_only.reports_bucket_origin, "http://seaweedfs:8333");-- let outside = config_from_env(&[- (- "FLUXER_S3_ENDPOINT",- "https://s3.eu-central-1.amazonaws.com",- ),- ("FLUXER_S3_FORCE_PATH_STYLE", "false"),- ("FLUXER_S3_BUCKET_UPLOADS", "example-uploads"),- ("FLUXER_S3_BUCKET_REPORTS", "example-reports"),- ]);- assert_eq!(- outside.reports_bucket_origin,- "https://example-reports.s3.eu-central-1.amazonaws.com"- );-- let renamed_uploads = config_from_env(&[- ("FLUXER_S3_ENDPOINT", "https://s3.example.com"),- (- "FLUXER_S3_PUBLIC_ENDPOINT",- "https://example-uploads.s3.example.com",- ),- ("FLUXER_S3_BUCKET_UPLOADS", "example-uploads"),- ("FLUXER_S3_BUCKET_REPORTS", "example-reports"),- ]);- assert_eq!(- renamed_uploads.reports_bucket_origin,- "https://example-reports.s3.example.com"- );- }-- #[test]- fn a_non_default_public_port_reaches_the_reports_bucket_origin() {- let config = config_from_env(&[- ("FLUXER_BASE_DOMAIN", "fluxer.example"),- ("FLUXER_PUBLIC_PORT", "19080"),- ("FLUXER_S3_ENDPOINT", "http://seaweedfs:8333"),- ("FLUXER_S3_PUBLIC_ENDPOINT", "http://fluxer.example"),- ("FLUXER_S3_FORCE_PATH_STYLE", "true"),- ]);- assert_eq!(config.reports_bucket_origin, "http://fluxer.example:19080");- }-}