1010#![ deny( missing_docs) ]
1111
1212use std:: net:: SocketAddr ;
13+ use std:: sync:: Arc ;
1314
1415use tokio:: net:: TcpListener ;
1516use tokio:: signal:: unix:: SignalKind ;
1617
1718use hyper:: server:: conn:: http1;
1819use hyper_util:: rt:: TokioIo ;
1920
20- use crate :: vss_service:: VssService ;
2121use api:: auth:: { Authorizer , NoopAuthorizer } ;
2222use api:: kv_store:: KvStore ;
23+ use auth_impls:: JWTAuthorizer ;
2324use impls:: postgres_store:: { Certificate , PostgresPlaintextBackend , PostgresTlsBackend } ;
24- use std:: sync:: Arc ;
25+ use util:: config:: { Config , ServerConfig } ;
26+ use vss_service:: VssService ;
2527
26- pub ( crate ) mod util;
27- pub ( crate ) mod vss_service;
28+ mod util;
29+ mod vss_service;
2830
2931fn main ( ) {
3032 let args: Vec < String > = std:: env:: args ( ) . collect ( ) ;
@@ -33,22 +35,21 @@ fn main() {
3335 std:: process:: exit ( 1 ) ;
3436 }
3537
36- let config = match util:: config:: load_config ( & args[ 1 ] ) {
37- Ok ( cfg) => cfg,
38- Err ( e) => {
39- eprintln ! ( "Failed to load configuration: {}" , e) ;
40- std:: process:: exit ( 1 ) ;
41- } ,
42- } ;
43-
44- let addr: SocketAddr =
45- match format ! ( "{}:{}" , config. server_config. host, config. server_config. port) . parse ( ) {
46- Ok ( addr) => addr,
38+ let Config { server_config : ServerConfig { host, port } , jwt_auth_config, postgresql_config } =
39+ match util:: config:: load_config ( & args[ 1 ] ) {
40+ Ok ( cfg) => cfg,
4741 Err ( e) => {
48- eprintln ! ( "Invalid host/port configuration: {}" , e) ;
42+ eprintln ! ( "Failed to load configuration: {}" , e) ;
4943 std:: process:: exit ( 1 ) ;
5044 } ,
5145 } ;
46+ let addr: SocketAddr = match format ! ( "{}:{}" , host, port) . parse ( ) {
47+ Ok ( addr) => addr,
48+ Err ( e) => {
49+ eprintln ! ( "Invalid host/port configuration: {}" , e) ;
50+ std:: process:: exit ( 1 ) ;
51+ } ,
52+ } ;
5253
5354 let runtime = match tokio:: runtime:: Builder :: new_multi_thread ( ) . enable_all ( ) . build ( ) {
5455 Ok ( runtime) => Arc :: new ( runtime) ,
@@ -66,9 +67,33 @@ fn main() {
6667 std:: process:: exit ( -1 ) ;
6768 } ,
6869 } ;
69- let authorizer: Arc < dyn Authorizer > = Arc :: new ( NoopAuthorizer { } ) ;
70+
71+ let rsa_pem_env = match std:: env:: var ( "VSS_JWT_RSA_PEM" ) {
72+ Ok ( env) => Some ( env) ,
73+ Err ( std:: env:: VarError :: NotPresent ) => None ,
74+ Err ( e) => {
75+ println ! ( "Failed to load the VSS_JWT_RSA_PEM env var: {}" , e) ;
76+ std:: process:: exit ( -1 ) ;
77+ } ,
78+ } ;
79+ let rsa_pem = rsa_pem_env. or ( jwt_auth_config. map ( |config| config. rsa_pem ) ) ;
80+ let authorizer: Arc < dyn Authorizer > = if let Some ( pem) = rsa_pem {
81+ let authorizer = match JWTAuthorizer :: new ( pem. as_str ( ) ) . await {
82+ Ok ( auth) => auth,
83+ Err ( e) => {
84+ println ! ( "Failed to parse the PEM formatted RSA public key: {}" , e) ;
85+ std:: process:: exit ( -1 ) ;
86+ } ,
87+ } ;
88+ println ! ( "Configured JWT authorizer with RSA public key" ) ;
89+ Arc :: new ( authorizer)
90+ } else {
91+ println ! ( "No JWT authentication method configured" ) ;
92+ Arc :: new ( NoopAuthorizer { } )
93+ } ;
94+
7095 let postgresql_config =
71- config . postgresql_config . expect ( "PostgreSQLConfig must be defined in config file." ) ;
96+ postgresql_config. expect ( "PostgreSQLConfig must be defined in config file." ) ;
7297 let endpoint = postgresql_config. to_postgresql_endpoint ( ) ;
7398 let db_name = postgresql_config. database ;
7499 let store: Arc < dyn KvStore > = if let Some ( tls_config) = postgresql_config. tls {
@@ -109,6 +134,7 @@ fn main() {
109134 Arc :: new ( postgres_plaintext_backend)
110135 } ;
111136 println ! ( "Connected to PostgreSQL backend with DSN: {}/{}" , endpoint, db_name) ;
137+
112138 let rest_svc_listener =
113139 TcpListener :: bind ( & addr) . await . expect ( "Failed to bind listening port" ) ;
114140 println ! ( "Listening for incoming connections on {}" , addr) ;
0 commit comments