Skip to content

Commit 6e53146

Browse files
authored
Merge pull request #533 from molnett/mk/keys_from_env
feat: allow keys from env
2 parents 2477cbd + 5c0bf80 commit 6e53146

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/pubsys-config/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ pub enum SigningKeyConfig {
163163
ssm {
164164
parameter: String,
165165
},
166+
env {
167+
var_name: String,
168+
},
166169
}
167170

168171
/// AWS region-specific configuration
@@ -202,6 +205,9 @@ impl TryFrom<SigningKeyConfig> for Url {
202205
};
203206
Url::parse(&format!("aws-ssm://{}", parameter)).map_err(|_| ())
204207
}
208+
SigningKeyConfig::env { var_name } => {
209+
Url::parse(&format!("env://{}", var_name)).map_err(|_| ())
210+
}
205211
}
206212
}
207213
}

tools/pubsys/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ publish = false
99
[dependencies]
1010
amispec.workspace = true
1111
async-stream.workspace = true
12+
async-trait.workspace = true
1213
aws-config.workspace = true
1314
aws-credential-types.workspace = true
1415
aws-sdk-ebs.workspace = true

tools/pubsys/src/repo.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ pub(crate) mod fetch_variant;
55
pub(crate) mod refresh_repo;
66
pub(crate) mod validate_repo;
77

8+
mod env_key_source;
9+
810
use crate::{friendly_version, read_stream, Args};
911
use aws_sdk_kms::{config::Region, Client as KmsClient};
1012
use chrono::{DateTime, Utc};
1113
use clap::Parser;
14+
use env_key_source::EnvKeySource;
1215
use lazy_static::lazy_static;
1316
use log::{debug, info, trace, warn};
1417
use parse_datetime::parse_datetime;
@@ -429,6 +432,9 @@ fn get_signing_key_source(signing_key_config: &SigningKeyConfig) -> Result<Box<d
429432
parameter_name: parameter.clone(),
430433
key_id: None,
431434
})),
435+
SigningKeyConfig::env { var_name } => Ok(Box::new(EnvKeySource {
436+
var_name: var_name.clone(),
437+
})),
432438
}
433439
}
434440

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use async_trait::async_trait;
2+
use log::{debug, warn};
3+
use snafu::Snafu;
4+
use std::env;
5+
use tough::key_source::KeySource;
6+
use tough::sign::{parse_keypair, Sign};
7+
8+
#[derive(Debug)]
9+
pub struct EnvKeySource {
10+
pub var_name: String,
11+
}
12+
13+
#[derive(Debug, Snafu)]
14+
pub enum Error {
15+
#[snafu(display("Environment variable '{}' not found", var_name))]
16+
EnvVarNotFound { var_name: String },
17+
18+
#[snafu(display(
19+
"Failed to parse key from environment variable '{}': {}",
20+
var_name,
21+
source
22+
))]
23+
KeyParse {
24+
var_name: String,
25+
source: Box<dyn std::error::Error + Send + Sync>,
26+
},
27+
}
28+
29+
#[async_trait]
30+
impl KeySource for EnvKeySource {
31+
async fn as_sign(
32+
&self,
33+
) -> std::result::Result<Box<dyn Sign>, Box<dyn std::error::Error + Send + Sync>> {
34+
debug!("Reading key from environment variable: {}", self.var_name);
35+
36+
// Get the key data from the environment variable
37+
let key_data = env::var(&self.var_name).map_err(|_| Error::EnvVarNotFound {
38+
var_name: self.var_name.clone(),
39+
})?;
40+
41+
// Parse the key data into a signer
42+
let key = parse_keypair(key_data.as_bytes()).map_err(|e| Error::KeyParse {
43+
var_name: self.var_name.clone(),
44+
source: Box::new(e),
45+
})?;
46+
47+
Ok(Box::new(key))
48+
}
49+
50+
async fn write(
51+
&self,
52+
_value: &str,
53+
_key_id_hex: &str,
54+
) -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
55+
// We don't support writing keys back to environment variables
56+
// as this wouldn't persist beyond the current process
57+
warn!("Writing keys back to environment variables is not supported");
58+
Ok(())
59+
}
60+
}

0 commit comments

Comments
 (0)