Skip to content

Commit cdff534

Browse files
authored
Allow KMS IAM auth via default config (#57)
* Allow KMS IAM auth via default config * Fix comment * Update error
1 parent 1993699 commit cdff534

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

rustica/src/signing/amazon_kms.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use std::time::Duration;
22

33
use aws_config::timeout::TimeoutConfig;
4-
use aws_credential_types::provider::future;
5-
use aws_credential_types::provider::ProvideCredentials;
4+
use aws_credential_types::Credentials;
65
use aws_sdk_kms::types::SigningAlgorithmSpec;
76
use aws_sdk_kms::{
8-
config::{Credentials, Region},
7+
config::Region,
98
primitives::Blob,
109
Client,
1110
};
@@ -39,9 +38,9 @@ pub struct KmsKeyDefinition {
3938
#[derive(Clone, Debug, Deserialize)]
4039
pub struct Config {
4140
/// The AWS access key that can access the KMS keys
42-
aws_access_key_id: String,
41+
aws_access_key_id: Option<String>,
4342
/// The secret corresponding to the AWS access key
44-
aws_secret_access_key: String,
43+
aws_secret_access_key: Option<String>,
4544
/// The region to be used
4645
aws_region: String,
4746

@@ -91,21 +90,6 @@ pub struct AmazonKMSSigner {
9190
client: Client,
9291
}
9392

94-
impl ProvideCredentials for Config {
95-
fn provide_credentials<'a>(&'a self) -> future::ProvideCredentials<'a>
96-
where
97-
Self: 'a,
98-
{
99-
future::ProvideCredentials::ready(Ok(Credentials::new(
100-
self.aws_access_key_id.clone(),
101-
self.aws_secret_access_key.clone(),
102-
None,
103-
None,
104-
"AmazonKMSSigner",
105-
)))
106-
}
107-
}
108-
10993
pub struct KmsRcgenRemoteSigner {
11094
x509_public_key: Vec<u8>,
11195
/// The id to lookup the private portion of the X509 key in Amazon KMS
@@ -275,12 +259,28 @@ impl SignerConfig for Config {
275259
.operation_attempt_timeout(Duration::from_secs(10))
276260
.operation_timeout(Duration::from_secs(10))
277261
.build();
278-
let aws_config = aws_config::from_env()
279-
.timeout_config(timeout_config)
280-
.region(Region::new(self.aws_region.clone()))
281-
.credentials_provider(self.clone())
282-
.load()
283-
.await;
262+
let aws_config = match (self.aws_access_key_id, self.aws_secret_access_key) {
263+
(Some(_), None) => return Err(
264+
SigningError::InvalidAwsConfig("aws_access_key_id is defined but aws_secret_access_key is not defined".to_string())
265+
),
266+
(None, Some(_)) => return Err(
267+
SigningError::InvalidAwsConfig("aws_secret_access_key is defined but aws_access_key_id is not defined".to_string())
268+
),
269+
(Some(access_key_id), Some(secret_access_key)) => aws_config::from_env()
270+
.region(Region::new(self.aws_region.clone()))
271+
.credentials_provider(
272+
Credentials::new(access_key_id, secret_access_key, None, None, "AmazonKMSSigner")
273+
)
274+
.load()
275+
.await,
276+
// If access key is not defined, use the default config
277+
(None, None) => aws_config::from_env()
278+
.timeout_config(timeout_config)
279+
.region(Region::new(self.aws_region.clone()))
280+
.load()
281+
.await,
282+
};
283+
284284
let client = Client::new(&aws_config);
285285

286286
let ssh_keys = match (self.user_key, self.host_key) {

rustica/src/signing/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pub enum SigningError {
125125
IdenticalUserAndHostKey(String),
126126
SignerDoesNotHaveSSHKeys,
127127
SignerDoesNotAllRequiredSSHKeys,
128+
InvalidAwsConfig(String),
128129
}
129130

130131
impl std::fmt::Display for SigningError {
@@ -137,7 +138,8 @@ impl std::fmt::Display for SigningError {
137138
Self::DuplicatedKey(a1, a2) => write!(f, "Authorities {a1} and {a2} share at least one key. This is not allowed as it almost always a misconfiguration leading to access that is not correctly restricted"),
138139
Self::IdenticalUserAndHostKey(authority) => write!(f, "Authority {authority} has an identical key for both user and host certificates. This is not allowed as it's much safer to use separate keys for both."),
139140
Self::SignerDoesNotHaveSSHKeys => write!(f, "Signer was not configured with SSH keys so it cannot create an SSH certificate"),
140-
Self::SignerDoesNotAllRequiredSSHKeys => write!(f, "Signer did not have both user and host keys defined")
141+
Self::SignerDoesNotAllRequiredSSHKeys => write!(f, "Signer did not have both user and host keys defined"),
142+
Self::InvalidAwsConfig(e) => write!(f, "Invalid AWS config: {e}"),
141143
}
142144
}
143145
}

0 commit comments

Comments
 (0)