Skip to content

Commit 8acc598

Browse files
tankyleofmar
authored andcommitted
Add option to verify JWT tokens in the HTTP Authorization header
The RSA public key against which the JWT tokens are verified can be set either via a configuration file setting, or an environment variable, with the latter having the higher priority. copy vss-server-config.toml remove ca-certificates rever removal of ca-certificates
1 parent 81d3da9 commit 8acc598

5 files changed

Lines changed: 46 additions & 32 deletions

File tree

rust/Dockerfile

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,7 @@ WORKDIR /app
3232
COPY --from=builder /build/target/release/vss-server /app/vss-server
3333

3434
# Copy default configuration file
35-
#COPY server/vss-server-config.toml /app/vss-server-config.toml
36-
37-
# Environment variables for PostgreSQL connection
38-
#ENV VSS_POSTGRESQL_USERNAME=postgres
39-
#ENV VSS_POSTGRESQL_PASSWORD=YOU_MUST_CHANGE_THIS_PASSWORD
40-
#ENV VSS_POSTGRESQL_HOST=postgres
41-
#ENV VSS_POSTGRESQL_PORT=5432
42-
#ENV VSS_POSTGRESQL_DATABASE=postgres
35+
COPY server/vss-server-config.toml /app/vss-server-config.toml
4336

4437
EXPOSE 8080
4538

rust/auth-impls/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ pub(crate) struct Claims {
4343
const BEARER_PREFIX: &str = "Bearer ";
4444

4545
impl JWTAuthorizer {
46-
/// Create new instance of [`JWTAuthorizer`]
47-
pub async fn new(jwt_issuer_key: DecodingKey) -> Self {
48-
Self { jwt_issuer_key }
46+
/// Creates a new instance of [`JWTAuthorizer`], fails on failure to parse the PEM formatted RSA public key
47+
pub async fn new(rsa_pem: &str) -> Result<Self, String> {
48+
let jwt_issuer_key =
49+
DecodingKey::from_rsa_pem(rsa_pem.as_bytes()).map_err(|e| e.to_string())?;
50+
Ok(Self { jwt_issuer_key })
4951
}
5052
}
5153

@@ -76,7 +78,7 @@ mod tests {
7678
use crate::JWTAuthorizer;
7779
use api::auth::Authorizer;
7880
use api::error::VssError;
79-
use jsonwebtoken::{encode, Algorithm, DecodingKey, EncodingKey, Header};
81+
use jsonwebtoken::{encode, Algorithm, EncodingKey, Header};
8082
use serde::{Deserialize, Serialize};
8183
use std::collections::HashMap;
8284
use std::time::SystemTime;
@@ -134,7 +136,7 @@ mod tests {
134136
)
135137
.expect("Failed to create Encoding Key.");
136138

137-
let decoding_key = DecodingKey::from_rsa_pem(
139+
let decoding_key = String::from(
138140
"-----BEGIN PUBLIC KEY-----\
139141
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAysGpKU+I9i9b+QZSANu/\
140142
ExaA6w4qiQdFZaXeReiz49r1oDfABwKIFW9gK/kNnrnL9H8P+pYfj7jqUJ/glmgq\
@@ -143,12 +145,10 @@ mod tests {
143145
8YsTa5piV8KgJpG/rwYTGXuu3lcCmnWwjmbeDq1zFFrCDDVkaIHkGJgRuFIDPXaH\
144146
yUw5H2HvKlP94ySbvTDLXWZj6TyzHEHDbstqs4DgvurB/bIhi/dQ7zK3EIXL8KRB\
145147
hwIDAQAB\
146-
-----END PUBLIC KEY-----"
147-
.as_bytes(),
148-
)
149-
.expect("Failed to create Decoding Key.");
148+
-----END PUBLIC KEY-----",
149+
);
150150

151-
let jwt_authorizer = JWTAuthorizer::new(decoding_key).await;
151+
let jwt_authorizer = JWTAuthorizer::new(&decoding_key).await.unwrap();
152152

153153
let valid_jwt_token =
154154
encode(&Header::new(Algorithm::RS256), &claims, &valid_encoding_key).unwrap();

rust/server/src/main.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@
1010
#![deny(missing_docs)]
1111

1212
use std::net::SocketAddr;
13+
use std::sync::Arc;
1314

1415
use tokio::net::TcpListener;
1516
use tokio::signal::unix::SignalKind;
1617

1718
use hyper::server::conn::http1;
1819
use hyper_util::rt::TokioIo;
1920

20-
use crate::vss_service::VssService;
2121
use api::auth::{Authorizer, NoopAuthorizer};
2222
use api::kv_store::KvStore;
2323
use auth_impls::{DecodingKey, JWTAuthorizer};
2424
use impls::postgres_store::{Certificate, PostgresPlaintextBackend, PostgresTlsBackend};
25-
use std::sync::Arc;
25+
use util::config::{Config, ServerConfig};
26+
use vss_service::VssService;
2627

2728
mod util;
2829
mod vss_service;
@@ -36,22 +37,21 @@ fn main() {
3637
std::process::exit(1);
3738
}
3839

39-
let config = match util::config::load_config(&args[1]) {
40-
Ok(cfg) => cfg,
41-
Err(e) => {
42-
eprintln!("Failed to load configuration: {}", e);
43-
std::process::exit(1);
44-
},
45-
};
46-
47-
let addr: SocketAddr =
48-
match format!("{}:{}", config.server_config.host, config.server_config.port).parse() {
49-
Ok(addr) => addr,
40+
let Config { server_config: ServerConfig { host, port }, jwt_auth_config, postgresql_config } =
41+
match util::config::load_config(&args[1]) {
42+
Ok(cfg) => cfg,
5043
Err(e) => {
51-
eprintln!("Invalid host/port configuration: {}", e);
44+
eprintln!("Failed to load configuration: {}", e);
5245
std::process::exit(1);
5346
},
5447
};
48+
let addr: SocketAddr = match format!("{}:{}", host, port).parse() {
49+
Ok(addr) => addr,
50+
Err(e) => {
51+
eprintln!("Invalid host/port configuration: {}", e);
52+
std::process::exit(1);
53+
},
54+
};
5555

5656
let runtime = match tokio::runtime::Builder::new_multi_thread().enable_all().build() {
5757
Ok(runtime) => Arc::new(runtime),

rust/server/src/util/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ 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
pub(crate) sentry_config: Option<SentryConfig>,
89
}
@@ -39,6 +40,11 @@ pub(crate) struct ServerConfig {
3940
pub(crate) rsa_pub_file_path: Option<String>,
4041
}
4142

43+
#[derive(Deserialize)]
44+
pub(crate) struct JwtAuthConfig {
45+
pub(crate) rsa_pem: String,
46+
}
47+
4248
#[derive(Deserialize)]
4349
pub(crate) struct PostgreSQLConfig {
4450
pub(crate) username: Option<String>, // Optional in TOML, can be overridden by env

rust/server/vss-server-config.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ host = "127.0.0.1"
33
port = 8080
44
# rsa_pub_file_path = "rsa_public_key.pem" # Uncomment to verify JWT tokens in the HTTP Authorization header
55

6+
# Uncomment the table below to verify JWT tokens in the HTTP Authorization header against the given RSA public key,
7+
# can be overridden by env var `VSS_JWT_RSA_PEM`
8+
# [jwt_auth_config]
9+
# rsa_pem = """
10+
# -----BEGIN PUBLIC KEY-----
11+
# MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAstPJs4ut+tFAI0qrOyGt
12+
# /3FN5jWc5gLv/j9Rc6lgr4hm7lyR05PU/G+4rfxdXGNyGTlQ6dRqcVy78CjxWz9f
13+
# 8l08EKLERPh8JhE5el6vr+ehWD5iQxSP3ejpx0Mr977fKMNKg6jlFiL+y50hOEp2
14+
# 6iN9QzZQjLxotDT3aQvbCA/DZpI+fV6WKDKWGS+pZGDVgOz5x/RcStJQXxkX3ACK
15+
# WhVdrtN3h6mHlhIt7ZIqVvQmY4NL03QPyljt13sYHoiFaoxINF/funBMCjrfSLcB
16+
# ko1rWE2BWdOrFqi27RtBs5AHOSAWXuz/2SUGpFuTQuJi7U68QUfjKeQO46JpQf+v
17+
# kQIDAQAB
18+
# -----END PUBLIC KEY-----
19+
# """
20+
621
[postgresql_config]
722
username = "postgres" # Optional in TOML, can be overridden by env var `VSS_POSTGRESQL_USERNAME`
823
password = "postgres" # Optional in TOML, can be overridden by env var `VSS_POSTGRESQL_PASSWORD`

0 commit comments

Comments
 (0)