Skip to content

Commit

Permalink
Github Build Failure fixes
Browse files Browse the repository at this point in the history
* Fixed Formatting
* Fixed cargo clippy
  • Loading branch information
srgothi92 committed Aug 10, 2020
1 parent 82accb4 commit 133ee24
Show file tree
Hide file tree
Showing 16 changed files with 357 additions and 242 deletions.
1 change: 0 additions & 1 deletion tough-kms/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ pub(crate) fn build_client_kms(profile: Option<&str>) -> Result<KmsClient> {
KmsClient::new(Region::default())
})
}

13 changes: 6 additions & 7 deletions tough-kms/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Snafu)]
#[snafu(visibility = "pub(crate)")]
pub enum Error {

#[snafu(display(
"Failed to create customer managed key for aws-kms://{}/{}: {}",
profile.as_deref().unwrap_or(""),
Expand All @@ -20,7 +19,7 @@ pub enum Error {
profile: Option<String>,
key_id: String,
source: rusoto_core::RusotoError<rusoto_kms::CreateKeyError>,
backtrace: Backtrace
backtrace: Backtrace,
},

#[snafu(display(
Expand All @@ -37,7 +36,7 @@ pub enum Error {
profile: Option<String>,
key_id: String,
source: rusoto_core::RusotoError<rusoto_kms::CreateAliasError>,
backtrace: Backtrace
backtrace: Backtrace,
},

#[snafu(display("Error creating AWS credentials provider: {}", source))]
Expand Down Expand Up @@ -82,7 +81,7 @@ pub enum Error {
profile: Option<String>,
key_id: String,
source: rusoto_core::RusotoError<rusoto_kms::GetPublicKeyError>,
backtrace: Backtrace
backtrace: Backtrace,
},

#[snafu(display(
Expand All @@ -93,7 +92,7 @@ pub enum Error {
))]
KmsAliasCheck {
profile: Option<String>,
key_id: String
key_id: String,
},

#[snafu(display("Public key is none"))]
Expand All @@ -106,7 +105,7 @@ pub enum Error {
))]
WriteNotDefined {
profile: Option<String>,
key_id: String
key_id: String,
},

#[snafu(display(
Expand All @@ -117,6 +116,6 @@ pub enum Error {
KeyMatadata {
profile: Option<String>,
key_id: String,
backtrace: Backtrace
backtrace: Backtrace,
},
}
44 changes: 24 additions & 20 deletions tough-kms/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT OR Apache-2.0


pub mod error;
mod client;
pub mod error;
use rusoto_kms::{Kms, KmsClient};
use snafu::{OptionExt, ResultExt};
use std::fmt;
use tough::key_source::KeySource;
use tough::sign::{KmsRsaKey, Sign};
use tough::schema::decoded::{Decoded, RsaPem};
use std::fmt;
use tough::schema::key::KmsSigningAlgorithms;
use tough::sign::{KmsRsaKey, Sign};

/// Implements the KeySource trait for keys that live in AWS KMS
pub struct KmsKeySource {
pub profile: Option<String>,
pub key_id: String,
pub client: Option<KmsClient>
pub client: Option<KmsClient>,
}

impl fmt::Debug for KmsKeySource {
Expand All @@ -36,7 +35,7 @@ impl KeySource for KmsKeySource {
{
let kms_client = match self.client.clone() {
Some(value) => value,
None => client::build_client_kms(self.profile.as_deref())?
None => client::build_client_kms(self.profile.as_deref())?,
};
// Get the public key from aws kms
let fut = kms_client.get_public_key(rusoto_kms::GetPublicKeyRequest {
Expand All @@ -48,38 +47,42 @@ impl KeySource for KmsKeySource {
.block_on(fut)
.context(error::KmsGetPublicKey {
profile: self.profile.clone(),
key_id : self.key_id.clone()
key_id: self.key_id.clone(),
})?;
let pb_key :Decoded<RsaPem> = response.public_key.context(error::PublicKeyNone)?.to_vec().into();
let pb_key: Decoded<RsaPem> = response
.public_key
.context(error::PublicKeyNone)?
.to_vec()
.into();
Ok(Box::new(KmsRsaKey {
kms_client: kms_client.clone(),
key_id: self.key_id.clone(),
public_key: pb_key,
signing_algorithm: KmsSigningAlgorithms::Rsa(String::from("RSASSA_PSS_SHA_256"))
signing_algorithm: KmsSigningAlgorithms::Rsa(String::from("RSASSA_PSS_SHA_256")),
}))
}

fn write(
&self,
value: &str
value: &str,
) -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let kms_client = match self.client.clone() {
Some(value) => value,
None => client::build_client_kms(self.profile.as_deref())?
None => client::build_client_kms(self.profile.as_deref())?,
};
// Assign an alias to the Key
let fut = kms_client.create_alias(rusoto_kms::CreateAliasRequest {
alias_name: self.key_id.clone(),
target_key_id: value.to_string().clone()
target_key_id: value.to_string(),
});
let _response = tokio::runtime::Runtime::new()
.context(error::RuntimeCreation)?
.block_on(fut)
.context(error::KmsCreateAlias {
alias : self.key_id.clone(),
target_key_id : value.to_string().clone(),
alias: self.key_id.clone(),
target_key_id: value.to_string(),
profile: self.profile.clone(),
key_id : self.key_id.clone()
key_id: self.key_id.clone(),
})?;
Ok(())
}
Expand All @@ -91,7 +94,7 @@ impl KeySource for KmsKeySource {
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let kms_client = match self.client.clone() {
Some(value) => value,
None => client::build_client_kms(self.profile.as_deref())?
None => client::build_client_kms(self.profile.as_deref())?,
};
// Kms currently does not have an API to validate if the alias exist. As a Hack,
// or temporary resolution, we are using the get_public_key API, if it succeeds than
Expand All @@ -107,8 +110,9 @@ impl KeySource for KmsKeySource {
if response.is_ok() {
error::KmsAliasCheck {
profile: self.profile.clone(),
key_id : self.key_id.clone()
}.fail()?;
key_id: self.key_id.clone(),
}
.fail()?;
}
// Create a new Customer managed Key in KMS
let fut = kms_client.create_key(rusoto_kms::CreateKeyRequest {
Expand All @@ -122,12 +126,12 @@ impl KeySource for KmsKeySource {
.block_on(fut)
.context(error::KmsCreateKey {
profile: self.profile.clone(),
key_id : self.key_id.clone()
key_id: self.key_id.clone(),
})?
.key_metadata
.context(error::KeyMatadata {
profile: self.profile.clone(),
key_id : self.key_id.clone()
key_id: self.key_id.clone(),
})?
.key_id;
self.write(&key_id)
Expand Down
Loading

0 comments on commit 133ee24

Please sign in to comment.