Skip to content

Commit

Permalink
Updated STS code examples in Rust to use alpha 0.0.13 bits (awsdocs#1981
Browse files Browse the repository at this point in the history
)

* Updated STS code examples in Rust to use alpha 0.0.13 bits

* Added newline to manifest file for STS data code examples in Rust

* Updated STS data code examples in Rust based on feedback
  • Loading branch information
Doug-AWS authored Aug 16, 2021
1 parent 3b53b9a commit c4344d4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 21 deletions.
10 changes: 5 additions & 5 deletions .rust_alpha/sts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sts = { git = "https://github.com/awslabs/aws-sdk-rust", tag = "v0.0.9-alpha", package = "aws-sdk-sts" }
dynamodb = { git = "https://github.com/awslabs/aws-sdk-rust", tag = "v0.0.9-alpha", package = "aws-sdk-dynamodb"}
aws-auth = { git = "https://github.com/awslabs/aws-sdk-rust", tag = "v0.0.9-alpha", package = "aws-auth" }

aws-sdk-sts = { git = "https://github.com/awslabs/aws-sdk-rust", tag = "v0.0.13-alpha", package = "aws-sdk-sts" }
aws-sdk-dynamodb = { git = "https://github.com/awslabs/aws-sdk-rust", tag = "v0.0.13-alpha", package = "aws-sdk-dynamodb" }
aws-auth = { git = "https://github.com/awslabs/aws-sdk-rust", tag = "v0.0.13-alpha", package = "aws-auth" }
aws-types = { git = "https://github.com/awslabs/aws-sdk-rust", tag = "v0.0.13-alpha", package = "aws-types" }
tokio = { version = "1", features = ["full"] }

structopt = { version = "0.3", default-features = false }
tracing-subscriber = "0.2.18"
8 changes: 4 additions & 4 deletions .rust_alpha/sts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ You must have an AWS account, and have configured your default credentials and A

### credentials-providers

This example implements a basic version of ProvideCredentials with AWS STS and lists the tables in the region based on those credentials.
This example implements a basic version of ProvideCredentials with AWS STS and lists the tables in the Region based on those credentials.

`cargo run --bin create-bucket -- -b BUCKET [-d DEFAULT-REGION] [-v]`
`cargo run --bin create-bucket -- -b BUCKET [-r REGION] [-v]`

- _BUCKET_ is the name of the bucket to create.
- _DEFAULT-REGION_ is the name of the AWS Region, such as __us-east-1__, where the table is located.
If not supplied, uses the value of the __AWS_DEFAULT_REGION__ or __AWS_REGION__ environment variable.
- _REGION_ is the Region in which the client is created.
If not supplied, uses the value of the __AWS_REGION__ environment variable.
If the environment variable is not set, defaults to __us-west-2__.
- __-v__ displays additional information.

Expand Down
68 changes: 56 additions & 12 deletions .rust_alpha/sts/src/bin/credentials-provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,81 @@
* SPDX-License-Identifier: Apache-2.0.
*/

use aws_auth::{CredentialsError, ProvideCredentials};
use aws_auth::provider::{CredentialsError, ProvideCredentials};
use aws_sdk_dynamodb::Error;
use aws_sdk_sts::{Credentials, Region};
use aws_types::region;
use aws_types::region::ProvideRegion;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};
use sts::Credentials;
use structopt::StructOpt;

/// Implements a basic version of ProvideCredentials with AWS STS
#[derive(Debug, StructOpt)]
struct Opt {
/// The AWS Region.
#[structopt(short, long)]
region: Option<String>,

/// Whether to display additional information.
#[structopt(short, long)]
verbose: bool,
}

/// Implements a basic version of ProvideCredentials with AWS Security Token Service (AWS STS)
/// and lists the tables in the region based on those credentials.
/// # Arguments
///
/// * `[-r REGION]` - The Region in which the client is created.
/// If not supplied, uses the value of the **AWS_REGION** environment variable.
/// If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), dynamodb::Error> {
async fn main() -> Result<(), Error> {
tracing_subscriber::fmt::init();
let client = sts::Client::from_env();

let Opt { region, verbose } = Opt::from_args();

let region = region::ChainProvider::first_try(region.map(Region::new))
.or_default_provider()
.or_else(Region::new("us-west-2"));

println!();

if verbose {
println!("STS client version: {}", aws_sdk_sts::PKG_VERSION);
println!("DynamoDB client version: {}", aws_sdk_dynamodb::PKG_VERSION);
println!(
"Region: {}",
region.region().unwrap().as_ref()
);
println!();
}

let config = aws_sdk_sts::Config::builder().region(region).build();
let client = aws_sdk_sts::Client::from_conf(config);

let sts_provider = StsCredentialsProvider {
client,
credentials: Arc::new(Mutex::new(None)),
};

sts_provider.spawn_refresh_loop().await;

let dynamodb_conf = dynamodb::Config::builder()
let dynamodb_conf = aws_sdk_dynamodb::Config::builder()
.credentials_provider(sts_provider)
.build();
let client = dynamodb::Client::from_conf(dynamodb_conf);

let client = aws_sdk_dynamodb::Client::from_conf(dynamodb_conf);

println!("tables: {:?}", client.list_tables().send().await?);
Ok(())
}

/// This is a rough example of how you could implement ProvideCredentials with Amazon STS.
///
/// Do not use this in production! A high quality implementation is in the roadmap.
/// The following code implements ProvideCredentials with Amazon STS.
/// We recommend that you do not use this code in production.
#[derive(Clone)]
struct StsCredentialsProvider {
client: sts::Client,
client: aws_sdk_sts::Client,
credentials: Arc<Mutex<Option<Credentials>>>,
}

Expand Down Expand Up @@ -74,7 +118,7 @@ impl StsCredentialsProvider {
}
});
}
pub async fn refresh(&self) -> Result<(), sts::Error> {
pub async fn refresh(&self) -> Result<(), aws_sdk_sts::Error> {
let session_token = self.client.get_session_token().send().await?;
let sts_credentials = session_token
.credentials
Expand Down

0 comments on commit c4344d4

Please sign in to comment.