Skip to content

Commit 7dc8a58

Browse files
committed
Add secrets.keys_dir config option
1 parent dc4854f commit 7dc8a58

File tree

3 files changed

+237
-17
lines changed

3 files changed

+237
-17
lines changed

crates/config/src/sections/secrets.rs

Lines changed: 211 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,86 @@ impl From<Encryption> for EncryptionRaw {
256256
}
257257
}
258258

259+
/// Description of signing keys.
260+
///
261+
/// It either holds the key config values directly or references a directory
262+
/// where each file contains a key.
263+
#[derive(Debug, Clone)]
264+
pub enum Keys {
265+
Values(Vec<KeyConfig>),
266+
Directory(Utf8PathBuf),
267+
}
268+
269+
impl Keys {
270+
/// Returns a list of key configs.
271+
///
272+
/// If `keys_dir` was given, the keys are read from file.
273+
async fn key_configs(&self) -> anyhow::Result<Vec<KeyConfig>> {
274+
match self {
275+
Keys::Values(key_configs) => Ok(key_configs.clone()),
276+
Keys::Directory(path) => key_configs_from_path(path).await,
277+
}
278+
}
279+
}
280+
281+
/// Reads all keys from the given directory.
282+
async fn key_configs_from_path(path: &Utf8PathBuf) -> anyhow::Result<Vec<KeyConfig>> {
283+
let mut result = vec![];
284+
let mut read_dir = tokio::fs::read_dir(path).await?;
285+
while let Some(dir_entry) = read_dir.next_entry().await? {
286+
if !dir_entry.path().is_file() {
287+
continue;
288+
}
289+
result.push(KeyConfig {
290+
kid: None,
291+
password: None,
292+
key: Key::File(dir_entry.path().try_into()?),
293+
});
294+
}
295+
Ok(result)
296+
}
297+
298+
#[serde_as]
299+
#[derive(JsonSchema, Serialize, Deserialize, Debug, Clone)]
300+
struct KeysRaw {
301+
/// List of private keys to use for signing and encrypting payloads.
302+
#[serde(skip_serializing_if = "Option::is_none")]
303+
keys: Option<Vec<KeyConfig>>,
304+
305+
/// Directory of private keys to use for signing and encrypting payloads.
306+
#[schemars(with = "Option<String>")]
307+
#[serde(skip_serializing_if = "Option::is_none")]
308+
keys_dir: Option<Utf8PathBuf>,
309+
}
310+
311+
impl TryFrom<KeysRaw> for Keys {
312+
type Error = anyhow::Error;
313+
314+
fn try_from(value: KeysRaw) -> Result<Keys, Self::Error> {
315+
match (value.keys, value.keys_dir) {
316+
(None, None) => bail!("Missing `keys` or `keys_dir`"),
317+
(None, Some(path)) => Ok(Keys::Directory(path)),
318+
(Some(keys), None) => Ok(Keys::Values(keys)),
319+
(Some(_), Some(_)) => bail!("Cannot specify both `keys` and `keys_dir`"),
320+
}
321+
}
322+
}
323+
324+
impl From<Keys> for KeysRaw {
325+
fn from(value: Keys) -> Self {
326+
match value {
327+
Keys::Directory(path) => KeysRaw {
328+
keys_dir: Some(path),
329+
keys: None,
330+
},
331+
Keys::Values(keys) => KeysRaw {
332+
keys_dir: None,
333+
keys: Some(keys),
334+
},
335+
}
336+
}
337+
}
338+
259339
/// Application secrets
260340
#[serde_as]
261341
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
@@ -267,8 +347,10 @@ pub struct SecretsConfig {
267347
encryption: Encryption,
268348

269349
/// List of private keys to use for signing and encrypting payloads
270-
#[serde(default)]
271-
keys: Vec<KeyConfig>,
350+
#[schemars(with = "KeysRaw")]
351+
#[serde_as(as = "serde_with::TryFromInto<KeysRaw>")]
352+
#[serde(flatten)]
353+
keys: Keys,
272354
}
273355

274356
impl SecretsConfig {
@@ -279,7 +361,8 @@ impl SecretsConfig {
279361
/// Returns an error when a key could not be imported
280362
#[tracing::instrument(name = "secrets.load", skip_all)]
281363
pub async fn key_store(&self) -> anyhow::Result<Keystore> {
282-
let web_keys = try_join_all(self.keys.iter().map(KeyConfig::json_web_key)).await?;
364+
let key_configs = self.keys.key_configs().await?;
365+
let web_keys = try_join_all(key_configs.iter().map(KeyConfig::json_web_key)).await?;
283366

284367
Ok(Keystore::new(JsonWebKeySet::new(web_keys)))
285368
}
@@ -394,7 +477,7 @@ impl SecretsConfig {
394477

395478
Ok(Self {
396479
encryption: Encryption::Value(Standard.sample(&mut rng)),
397-
keys: vec![rsa_key, ec_p256_key, ec_p384_key, ec_k256_key],
480+
keys: Keys::Values(vec![rsa_key, ec_p256_key, ec_p384_key, ec_k256_key]),
398481
})
399482
}
400483

@@ -435,7 +518,7 @@ impl SecretsConfig {
435518

436519
Self {
437520
encryption: Encryption::Value([0xEA; 32]),
438-
keys: vec![rsa_key, ecdsa_key],
521+
keys: Keys::Values(vec![rsa_key, ecdsa_key]),
439522
}
440523
}
441524
}
@@ -451,6 +534,129 @@ mod tests {
451534

452535
use super::*;
453536

537+
#[tokio::test]
538+
async fn load_config() {
539+
task::spawn_blocking(|| {
540+
Jail::expect_with(|jail| {
541+
jail.create_file(
542+
"config.yaml",
543+
indoc::indoc! {r"
544+
secrets:
545+
encryption_file: encryption
546+
keys_dir: keys
547+
"},
548+
)?;
549+
jail.create_file("encryption", example_secret())?;
550+
jail.create_dir("keys")?;
551+
jail.create_file(
552+
"keys/key1",
553+
indoc::indoc! {r"
554+
-----BEGIN RSA PRIVATE KEY-----
555+
MIIJKQIBAAKCAgEA6oR6LXzJOziUxcRryonLTM5Xkfr9cYPCKvnwsWoAHfd2MC6Q
556+
OCAWSQnNcNz5RTeQUcLEaA8sxQi64zpCwO9iH8y8COCaO8u9qGkOOuJwWnmPfeLs
557+
cEwALEp0LZ67eSUPsMaz533bs4C8p+2UPMd+v7Td8TkkYoqgUrfYuT0bDTMYVsSe
558+
wcNB5qsI7hDLf1t5FX6KU79/Asn1K3UYHTdN83mghOlM4zh1l1CJdtgaE1jAg4Ml
559+
1X8yG+cT+Ks8gCSGQfIAlVFV4fvvzmpokNKfwAI/b3LS2/ft4ZrK+RCTsWsjUu38
560+
Zr8jbQMtDznzBHMw1LoaHpwRNjbJZ7uA6x5ikbwz5NAlfCITTta6xYn8qvaBfiYJ
561+
YyUFl0kIHm9Kh9V9p54WPMCFCcQx12deovKV82S6zxTeMflDdosJDB/uG9dT2qPt
562+
wkpTD6xAOx5h59IhfiY0j4ScTl725GygVzyK378soP3LQ/vBixQLpheALViotodH
563+
fJknsrelaISNkrnapZL3QE5C1SUoaUtM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vdxSH/cN+QYs2Pr1X5Pneu+622KnbHQphfq0fqg7Upchwdb4Faw
596+
5Z6wthVMvK0YMcppUMgEzOOz0w6xGEbowGAkA5cj1KTG+jjzs02ivNM9V5Utb5nF
597+
3D4iphAYK3rNMfTlKsejciIlCX+TMVyb9EdSjU+uM7ZJ2xtgWx+i4NA+10GCT42V
598+
EZct4TORbN0ukK2+yH2m8yoAiOks0gJemwKCAQAMGROGt8O4HfhpUdOq01J2qvQL
599+
m5oUXX8w1I95XcoAwCqb+dIan8UbCyl/79lbqNpQlHbRy3wlXzWwH9aHKsfPlCvk
600+
5dE1qrdMdQhLXwP109bRmTiScuU4zfFgHw3XgQhMFXxNp9pze197amLws0TyuBW3
601+
fupS4kM5u6HKCeBYcw2WP5ukxf8jtn29tohLBiA2A7NYtml9xTer6BBP0DTh+QUn
602+
IJL6jSpuCNxBPKIK7p6tZZ0nMBEdAWMxglYm0bmHpTSd3pgu3ltCkYtDlDcTIaF0
603+
Q4k44lxUTZQYwtKUVQXBe4ZvaT/jIEMS7K5bsAy7URv/toaTaiEh1hguwSmf
604+
-----END RSA PRIVATE KEY-----
605+
"},
606+
)?;
607+
jail.create_file(
608+
"keys/key2",
609+
indoc::indoc! {r"
610+
-----BEGIN EC PRIVATE KEY-----
611+
MHcCAQEEIKlZz/GnH0idVH1PnAF4HQNwRafgBaE2tmyN1wjfdOQqoAoGCCqGSM49
612+
AwEHoUQDQgAEHrgPeG+Mt8eahih1h4qaPjhl7jT25cdzBkg3dbVks6gBR2Rx4ug9
613+
h27LAir5RqxByHvua2XsP46rSTChof78uw==
614+
-----END EC PRIVATE KEY-----
615+
"},
616+
)?;
617+
618+
let config = Figment::new()
619+
.merge(Yaml::file("config.yaml"))
620+
.extract_inner::<SecretsConfig>("secrets")?;
621+
622+
Handle::current().block_on(async move {
623+
assert!(
624+
matches!(config.encryption, Encryption::File(ref p) if p == "encryption")
625+
);
626+
assert_eq!(
627+
config.encryption().await.unwrap(),
628+
[
629+
0, 0, 17, 17, 34, 34, 51, 51, 68, 68, 85, 85, 102, 102, 119, 119, 136,
630+
136, 153, 153, 170, 170, 187, 187, 204, 204, 221, 221, 238, 238, 255,
631+
255
632+
]
633+
);
634+
635+
let mut key_config = config.keys.key_configs().await.unwrap();
636+
key_config.sort_by_key(|a| {
637+
if let Key::File(p) = &a.key {
638+
Some(p.clone())
639+
} else {
640+
None
641+
}
642+
});
643+
let key_store = config.key_store().await.unwrap();
644+
645+
assert!(key_config[0].kid.is_none());
646+
assert!(matches!(&key_config[0].key, Key::File(p) if p == "keys/key1"));
647+
assert!(key_store.iter().any(|k| k.kid() == Some("040b0ab8")));
648+
assert!(key_config[1].kid.is_none());
649+
assert!(matches!(&key_config[1].key, Key::File(p) if p == "keys/key2"));
650+
assert!(key_store.iter().any(|k| k.kid() == Some("7a0a3fc2")));
651+
});
652+
653+
Ok(())
654+
});
655+
})
656+
.await
657+
.unwrap();
658+
}
659+
454660
#[tokio::test]
455661
async fn load_config_inline_secrets() {
456662
task::spawn_blocking(|| {

docs/config.schema.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,14 +1528,6 @@
15281528
"description": "Application secrets",
15291529
"type": "object",
15301530
"properties": {
1531-
"keys": {
1532-
"description": "List of private keys to use for signing and encrypting payloads",
1533-
"default": [],
1534-
"type": "array",
1535-
"items": {
1536-
"$ref": "#/definitions/KeyConfig"
1537-
}
1538-
},
15391531
"encryption_file": {
15401532
"description": "File containing the encryption key for secure cookies.",
15411533
"type": "string"
@@ -1547,6 +1539,17 @@
15471539
],
15481540
"type": "string",
15491541
"pattern": "[0-9a-fA-F]{64}"
1542+
},
1543+
"keys": {
1544+
"description": "List of private keys to use for signing and encrypting payloads.",
1545+
"type": "array",
1546+
"items": {
1547+
"$ref": "#/definitions/KeyConfig"
1548+
}
1549+
},
1550+
"keys_dir": {
1551+
"description": "Directory of private keys to use for signing and encrypting payloads.",
1552+
"type": "string"
15501553
}
15511554
}
15521555
},

docs/reference/configuration.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ The secret is not updated when the content of the file changes.
222222
> Changing the encryption secret afterwards will lead to a loss of all encrypted
223223
> information in the database.
224224

225-
### `secrets.keys`
225+
### Singing Keys
226226

227227
The service can use a number of key types for signing.
228228
The following key types are supported:
@@ -232,15 +232,26 @@ The following key types are supported:
232232
- ECDSA with the P-384 (`secp384r1`) curve
233233
- ECDSA with the K-256 (`secp256k1`) curve
234234

235-
Each entry in the list corresponds to one signing key used by MAS.
236-
The key can either be specified inline (with the `key` property),
237-
or loaded from a file (with the `key_file` property).
238235
The following key formats are supported:
239236

240237
- PKCS#1 PEM or DER-encoded RSA private key
241238
- PKCS#8 PEM or DER-encoded RSA or ECDSA private key, encrypted or not
242239
- SEC1 PEM or DER-encoded ECDSA private key
243240

241+
The keys can be given as a directory path via `secrets.keys_dir`
242+
or, alternatively, as an inline configuration list via `secrets.keys`.
243+
244+
#### `secrets.keys_dir`
245+
246+
Path to the directory containing MAS signing key files.
247+
Only keys that don’t require a password are supported.
248+
249+
#### `secrets.keys`
250+
251+
Each entry in the list corresponds to one signing key used by MAS.
252+
The key can either be specified inline (with the `key` property),
253+
or loaded from a file (with the `key_file` property).
254+
244255
A [JWK Key ID] is automatically derived from each key.
245256
To override this default, set `kid` to a custom value.
246257
The `kid` can be any case-sensitive string value as long as it is unique to this list;

0 commit comments

Comments
 (0)