Skip to content

Commit f752263

Browse files
authored
feat: CORS support on gateway (#1714)
Allow the console to call gw directly by adding CORS support to gw and setting the origin as the console.
1 parent 0525f33 commit f752263

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ services:
122122
- "--provisioner-host=provisioner"
123123
- "--proxy-fqdn=${APPS_FQDN}"
124124
- "--use-tls=${USE_TLS}"
125+
- "--cors-origin=https://console.shuttle.rs"
125126
- "--admin-key=${GATEWAY_ADMIN_KEY}"
126127
- "--permit-api-uri=https://api.eu-central-1.permit.io"
127128
- "--permit-pdp-uri=http://permit-pdp:7000"

gateway/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ strum = { workspace = true }
4444
tokio = { workspace = true, features = ["full"] }
4545
tonic = { workspace = true }
4646
tower = { workspace = true, features = ["steer"] }
47-
tower-http = { workspace = true }
47+
tower-http = { workspace = true, features = ["cors"] }
4848
tower-sanitize-path = "0.2.0"
4949
tracing = { workspace = true, features = ["default"] }
5050
tracing-opentelemetry = { workspace = true }

gateway/src/api/latest.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use axum::routing::{any, delete, get, post};
1414
use axum::{Json as AxumJson, Router};
1515
use fqdn::FQDN;
1616
use futures::Future;
17-
use http::{StatusCode, Uri};
17+
use http::header::AUTHORIZATION;
18+
use http::{HeaderValue, Method, StatusCode, Uri};
1819
use instant_acme::{AccountCredentials, ChallengeType};
1920
use serde::{Deserialize, Serialize};
2021
use shuttle_backends::auth::{AuthPublicKey, JwtAuthenticationLayer, ScopedLayer};
@@ -34,6 +35,7 @@ use shuttle_proto::provisioner::Ping;
3435
use tokio::sync::mpsc::Sender;
3536
use tokio::sync::{Mutex, MutexGuard};
3637
use tower::ServiceBuilder;
38+
use tower_http::cors::CorsLayer;
3739
use tracing::{error, field, instrument, trace};
3840
use ttl_cache::TtlCache;
3941
use ulid::Ulid;
@@ -973,6 +975,22 @@ impl ApiBuilder {
973975
self
974976
}
975977

978+
pub fn with_cors(mut self, cors_origin: &str) -> Self {
979+
let cors_layer = CorsLayer::new()
980+
.allow_methods(vec![Method::GET, Method::POST, Method::DELETE])
981+
.allow_headers(vec![AUTHORIZATION])
982+
.max_age(Duration::from_secs(60) * 10)
983+
.allow_origin(
984+
cors_origin
985+
.parse::<HeaderValue>()
986+
.expect("to be able to parse the CORS origin"),
987+
);
988+
989+
self.router = self.router.layer(cors_layer);
990+
991+
self
992+
}
993+
976994
pub fn into_router(self) -> Router {
977995
let service = self.service.expect("a GatewayService is required");
978996
let sender = self.sender.expect("a task Sender is required");

gateway/src/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ pub struct StartArgs {
4040
/// Allows to disable the use of TLS in the user proxy service (DANGEROUS)
4141
#[arg(long, default_value = "enable")]
4242
pub use_tls: UseTls,
43+
/// The origin to allow CORS requests from
44+
#[arg(long, default_value = "https://console.shuttle.rs")]
45+
pub cors_origin: String,
4346
#[command(flatten)]
4447
pub context: ServiceArgs,
4548
#[command(flatten)]

gateway/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ pub mod tests {
540540
user,
541541
bouncer,
542542
use_tls: UseTls::Disable,
543+
cors_origin: "http://localhost:3001".to_string(),
543544
context: ServiceArgs {
544545
docker_host,
545546
image,

gateway/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ async fn start(
250250
.with_default_routes()
251251
.with_auth_service(args.context.auth_uri, args.context.admin_key)
252252
.with_default_traces()
253+
.with_cors(&args.cors_origin)
253254
.serve();
254255

255256
let user_handle = user_builder.serve();

0 commit comments

Comments
 (0)