Skip to content

Commit 274b226

Browse files
committed
fixup: Take the rsa public key as a string, not as a file
Also allow the setting to be overridden with `VSS_JWT_RSA_PEM`
1 parent 748532c commit 274b226

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

rust/server/src/main.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn main() {
3636
std::process::exit(1);
3737
}
3838

39-
let Config { server_config: ServerConfig { host, port, rsa_pub_file_path }, postgresql_config } =
39+
let Config { server_config: ServerConfig { host, port }, jwt_auth_config, postgresql_config } =
4040
match util::config::load_config(&args[1]) {
4141
Ok(cfg) => cfg,
4242
Err(e) => {
@@ -69,23 +69,27 @@ fn main() {
6969
},
7070
};
7171

72-
let authorizer: Arc<dyn Authorizer> = if let Some(file_path) = rsa_pub_file_path {
73-
let rsa_pub_file = match std::fs::read(file_path) {
74-
Ok(pem) => pem,
75-
Err(e) => {
76-
println!("Failed to read RSA public key file: {}", e);
77-
std::process::exit(-1);
78-
},
79-
};
80-
let rsa_public_key = match DecodingKey::from_rsa_pem(&rsa_pub_file) {
81-
Ok(pem) => pem,
72+
let rsa_pem_env = match std::env::var("VSS_JWT_RSA_PEM") {
73+
Ok(env) => Some(env),
74+
Err(std::env::VarError::NotPresent) => None,
75+
Err(e) => {
76+
println!("Failed to load the VSS_JWT_RSA_PEM env var: {}", e);
77+
std::process::exit(-1);
78+
},
79+
};
80+
let rsa_pem = rsa_pem_env.or(jwt_auth_config.map(|config| config.rsa_pem));
81+
let authorizer: Arc<dyn Authorizer> = if let Some(pem) = rsa_pem {
82+
let rsa_public_key = match DecodingKey::from_rsa_pem(pem.as_bytes()) {
83+
Ok(p) => p,
8284
Err(e) => {
83-
println!("Failed to parse RSA public key file: {}", e);
85+
println!("Failed to parse the PEM formatted RSA public key: {}", e);
8486
std::process::exit(-1);
8587
},
8688
};
89+
println!("Configured JWT authorizer with RSA public key");
8790
Arc::new(JWTAuthorizer::new(rsa_public_key).await)
8891
} else {
92+
println!("No JWT authentication method configured");
8993
Arc::new(NoopAuthorizer {})
9094
};
9195

rust/server/src/util/config.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ use serde::Deserialize;
33
#[derive(Deserialize)]
44
pub(crate) struct Config {
55
pub(crate) server_config: ServerConfig,
6+
pub(crate) jwt_auth_config: Option<JwtAuthConfig>,
67
pub(crate) postgresql_config: Option<PostgreSQLConfig>,
78
}
89

910
#[derive(Deserialize)]
1011
pub(crate) struct ServerConfig {
1112
pub(crate) host: String,
1213
pub(crate) port: u16,
13-
pub(crate) rsa_pub_file_path: Option<String>,
14+
}
15+
16+
#[derive(Deserialize)]
17+
pub(crate) struct JwtAuthConfig {
18+
pub(crate) rsa_pem: String,
1419
}
1520

1621
#[derive(Deserialize)]

rust/server/vss-server-config.toml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
[server_config]
22
host = "127.0.0.1"
33
port = 8080
4-
# rsa_pub_file_path = "rsa_public_key.pem" # Uncomment to verify JWT tokens in the HTTP Authorization header
4+
5+
# Uncomment the table below to verify JWT tokens in the HTTP Authorization header against the given RSA public key,
6+
# can be overridden by env var `VSS_JWT_RSA_PEM`
7+
# [jwt_auth_config]
8+
# rsa_pem = """
9+
# -----BEGIN PUBLIC KEY-----
10+
# MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAstPJs4ut+tFAI0qrOyGt
11+
# /3FN5jWc5gLv/j9Rc6lgr4hm7lyR05PU/G+4rfxdXGNyGTlQ6dRqcVy78CjxWz9f
12+
# 8l08EKLERPh8JhE5el6vr+ehWD5iQxSP3ejpx0Mr977fKMNKg6jlFiL+y50hOEp2
13+
# 6iN9QzZQjLxotDT3aQvbCA/DZpI+fV6WKDKWGS+pZGDVgOz5x/RcStJQXxkX3ACK
14+
# WhVdrtN3h6mHlhIt7ZIqVvQmY4NL03QPyljt13sYHoiFaoxINF/funBMCjrfSLcB
15+
# ko1rWE2BWdOrFqi27RtBs5AHOSAWXuz/2SUGpFuTQuJi7U68QUfjKeQO46JpQf+v
16+
# kQIDAQAB
17+
# -----END PUBLIC KEY-----
18+
# """
519

620
[postgresql_config]
721
username = "postgres" # Optional in TOML, can be overridden by env var `VSS_POSTGRESQL_USERNAME`

0 commit comments

Comments
 (0)