Skip to content

Commit af19987

Browse files
author
Jakub Konka
committed
feat(server): add JWT-based authorization mode
This mode is an alternative to whitelist authorization mode. It extracts the JWT from the authorization header (bearer token), validates token's signature, claimed expiry times and additional (user-configurable) claims.
1 parent 33c4b9d commit af19987

File tree

20 files changed

+731
-241
lines changed

20 files changed

+731
-241
lines changed

Cargo.lock

Lines changed: 23 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/notary/client/src/client.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use http_body_util::{BodyExt as _, Either, Empty, Full};
77
use hyper::{
88
body::{Bytes, Incoming},
99
client::conn::http1::Parts,
10+
header::AUTHORIZATION,
1011
Request, Response, StatusCode,
1112
};
1213
use hyper_util::rt::TokioIo;
@@ -137,6 +138,10 @@ pub struct NotaryClient {
137138
/// in notary server.
138139
#[builder(setter(into, strip_option), default)]
139140
api_key: Option<String>,
141+
/// JWT token used to callnotary server endpoints if JWT authorization is enabled
142+
/// in notary server.
143+
#[builder(setter(into, strip_option), default)]
144+
jwt: Option<String>,
140145
/// The duration of notarization request timeout in seconds.
141146
#[builder(default = "60")]
142147
request_timeout: usize,
@@ -291,6 +296,11 @@ impl NotaryClient {
291296
configuration_request_builder.header(X_API_KEY_HEADER, api_key);
292297
}
293298

299+
if let Some(jwt) = &self.jwt {
300+
configuration_request_builder =
301+
configuration_request_builder.header(AUTHORIZATION, format!("Bearer {jwt}"));
302+
}
303+
294304
let configuration_request = configuration_request_builder
295305
.body(Either::Left(Full::new(Bytes::from(
296306
configuration_request_payload,

crates/notary/server/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ http = { workspace = true }
3131
http-body-util = { workspace = true }
3232
hyper = { workspace = true, features = ["client", "http1", "server"] }
3333
hyper-util = { workspace = true, features = ["full"] }
34+
jsonwebtoken = { version = "9.3.1", features = ["use_pem"] }
3435
k256 = { workspace = true }
3536
notify = { version = "6.1.1", default-features = false, features = [
3637
"macos_kqueue",
@@ -42,6 +43,7 @@ rand06-compat = { workspace = true }
4243
rustls = { workspace = true }
4344
rustls-pemfile = { workspace = true }
4445
serde = { workspace = true, features = ["derive"] }
46+
serde_json = { workspace = true }
4547
serde_yaml = { version = "0.9" }
4648
sha1 = { version = "0.10" }
4749
structopt = { version = "0.3" }

crates/notary/server/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,34 @@ After calling the configuration endpoint above, the prover can proceed to start
145145
Currently, both the private key (and cert) used to establish a TLS connection with the prover, and the private key used by the notary server to sign the notarized transcript, are hardcoded PEM keys stored in this repository. Though the paths of these keys can be changed in the config (`notary-key` field) to use different keys instead.
146146

147147
#### Authorization
148-
An optional authorization module is available to only allow requests with a valid API key attached in the custom HTTP header `X-API-Key`. The API key whitelist path (as well as the flag to enable/disable this module) can be changed in the config (`authorization` field).
148+
An optional authorization module is available to only allow requests with a valid credential attached. Currently, two modes are supported: whitelist and JWT.
149+
150+
Please note that only *one* mode can be active at any one time.
151+
152+
##### Whitelist mode
153+
In whitelist mode, an API key is attached in the custom HTTP header `X-API-Key`. The API key whitelist path (as well as the flag to enable/disable this module) can be changed in the config (`authorization` field).
149154

150155
Hot reloading of the whitelist is supported, i.e. modification of the whitelist file will be automatically applied without needing to restart the server. Please take note of the following
151156
- Avoid using auto save mode when editing the whitelist to prevent spamming hot reloads
152157
- Once the edit is saved, ensure that it has been reloaded successfully by checking the server log
153158

159+
##### JWT mode
160+
In JWT mode, JSON Web Token is attached in the standard `Authorization` HTTP header as a bearer token. The path to decoding key as well as custom user claims can be changed in the
161+
config (`authorization` field).
162+
163+
An example JWT config may look something like this:
164+
165+
```yaml
166+
authorization:
167+
enabled: true
168+
jwt:
169+
algorithm: "RS256"
170+
public_key_path: "./fixture/auth/jwt.key.pub"
171+
claims:
172+
- name: sub
173+
values: ["tlsnotary"]
174+
```
175+
154176
#### Optional TLS
155177
TLS between the prover and the notary is currently manually handled in this server, though it can be turned off if any of the following is true
156178
- This server is run locally

crates/notary/server/openapi.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ paths:
1515
security:
1616
- {} # make security optional
1717
- ApiKeyAuth: []
18+
- BearerAuth: []
1819
responses:
1920
'200':
2021
description: Ok response from server
@@ -38,6 +39,7 @@ paths:
3839
security:
3940
- {} # make security optional
4041
- ApiKeyAuth: []
42+
- BearerAuth: []
4143
responses:
4244
'200':
4345
description: Info response from server
@@ -60,6 +62,7 @@ paths:
6062
security:
6163
- {} # make security optional
6264
- ApiKeyAuth: []
65+
- BearerAuth: []
6366
parameters:
6467
- in: header
6568
name: Content-Type
@@ -212,4 +215,9 @@ components:
212215
type: apiKey
213216
in: header
214217
name: X-API-Key
215-
description: Whitelisted API key if auth module is turned on
218+
description: Whitelisted API key if auth module is turned on and in whitelist mode
219+
BearerAuth:
220+
type: http
221+
scheme: bearer
222+
bearerFormat: JWT
223+
description: JSON Web Token if auth module is turned on and in JWT mode

0 commit comments

Comments
 (0)