Skip to content

Commit

Permalink
credentials: use impl Into<String>
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobarbolini committed Nov 10, 2021
1 parent bb4ff05 commit f851adc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
30 changes: 18 additions & 12 deletions src/credentials/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,26 @@ pub struct Credentials {
impl Credentials {
/// Construct a new `Credentials` using the provided key and secret
#[inline]
pub fn new<S: Into<String>>(key: S, secret: S) -> Self {
Self::new_(key, secret, None)
pub fn new(key: impl Into<String>, secret: impl Into<String>) -> Self {
Self::new_with_maybe_token(key.into(), secret.into(), None)
}

/// Construct a new `Credentials` using the provided key, secret and token
///
/// For backwards compatibility this method was named `new_`, and will replace
/// the current `new` implementation in the 0.2.0 release.
#[inline]
pub fn new_<S: Into<String>>(key: S, secret: S, token: Option<S>) -> Self {
pub fn new_with_token(
key: impl Into<String>,
secret: impl Into<String>,
token: impl Into<String>,
) -> Self {
Self::new_with_maybe_token(key.into(), secret.into(), Some(token.into()))
}

#[inline]
pub(super) fn new_with_maybe_token(key: String, secret: String, token: Option<String>) -> Self {
Self {
key: key.into(),
secret: Zeroizing::new(secret.into()),
token: token.map(|s| s.into()),
key,
secret: Zeroizing::new(secret),
token,
}
}

Expand All @@ -59,7 +65,7 @@ impl Credentials {
let key = env::var("AWS_ACCESS_KEY_ID").ok()?;
let secret = env::var("AWS_SECRET_ACCESS_KEY").ok()?;
let token = env::var("AWS_SESSION_TOKEN").ok();
Some(Self::new_(key, secret, token))
Some(Self::new_with_maybe_token(key, secret, token))
}

/// Get the key of this `Credentials`
Expand Down Expand Up @@ -105,7 +111,7 @@ mod tests {

#[test]
fn key_secret_token() {
let credentials = Credentials::new_("abcd", "1234", Some("xyz"));
let credentials = Credentials::new_with_token("abcd", "1234", "xyz");
assert_eq!(credentials.key(), "abcd");
assert_eq!(credentials.secret(), "1234");
assert_eq!(credentials.token(), Some("xyz"));
Expand All @@ -120,7 +126,7 @@ mod tests {

#[test]
fn debug_token() {
let credentials = Credentials::new_("abcd", "1234", Some("xyz"));
let credentials = Credentials::new_with_token("abcd", "1234", "xyz");
let debug_output = format!("{:?}", credentials);
assert_eq!(debug_output, "Credentials { key: \"abcd\" }");
}
Expand Down
4 changes: 2 additions & 2 deletions src/credentials/rotating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct RotatingCredentials {
impl RotatingCredentials {
/// Construct a new `RotatingCredentials` using the provided key, secret and token
pub fn new(key: String, secret: String, token: Option<String>) -> Self {
let credentials = Credentials::new_(key, secret, token);
let credentials = Credentials::new_with_maybe_token(key, secret, token);

Self {
inner: Arc::new(RwLock::new(Arc::new(credentials))),
Expand All @@ -32,7 +32,7 @@ impl RotatingCredentials {

/// Update the credentials inside this `RotatingCredentials`
pub fn update(&self, key: String, secret: String, token: Option<String>) {
let credentials = Credentials::new_(key, secret, token);
let credentials = Credentials::new_with_maybe_token(key, secret, token);

let mut lock = self.inner.write().expect("can't be poisoned");
match Arc::get_mut(&mut lock) {
Expand Down
2 changes: 1 addition & 1 deletion src/credentials/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Ec2SecurityCredentialsMetadataResponse {
let key = mem::take(&mut self.key);
let secret = mem::take(&mut self.secret);
let token = mem::take(&mut self.token);
Credentials::new_(key, secret, Some(token))
Credentials::new_with_token(key, secret, token)
}

/// Update a [`RotatingCredentials`] with the credentials of this `Ec2SecurityCredentialsMetadataResponse`
Expand Down

0 comments on commit f851adc

Please sign in to comment.