Skip to content

Commit

Permalink
cleanup azure_identity examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Caswell committed Sep 9, 2023
1 parent e19ca9f commit 0209d17
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 88 deletions.
4 changes: 2 additions & 2 deletions sdk/identity/examples/azure_cli_credentials.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use azure_core::auth::TokenCredential;
use azure_identity::*;
use azure_identity::AzureCliCredential;
use std::error::Error;
use url::Url;

Expand Down Expand Up @@ -27,6 +27,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
.text()
.await?;

println!("\n\nresp {resp:?}");
println!("{resp}");
Ok(())
}
27 changes: 10 additions & 17 deletions sdk/identity/examples/client_certificate_credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ use azure_identity::{
CertificateCredentialOptions, ClientCertificateCredential, DefaultAzureCredential,
};
use azure_security_keyvault::KeyvaultClient;
use oauth2::ClientId;
use std::env;
use std::error::Error;
use std::env::var;
use url::Url;

async fn get_certficate(
vault_name: &str,
certificate_name: &str,
) -> Result<Vec<u8>, Box<dyn Error>> {
async fn get_certficate(vault_name: &str, certificate_name: &str) -> azure_core::Result<Vec<u8>> {
let creds = DefaultAzureCredential::default();
let client = KeyvaultClient::new(
format!("https://{}.vault.azure.net", vault_name).as_str(),
Expand All @@ -29,16 +24,14 @@ async fn get_certficate(
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client_id =
ClientId::new(env::var("CLIENT_ID").expect("Missing CLIENT_ID environment variable."));
let tenant_id = env::var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
async fn main() -> azure_core::Result<()> {
let client_id = var("CLIENT_ID").expect("Missing CLIENT_ID environment variable.");
let tenant_id = var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
let subscription_id =
env::var("SUBSCRIPTION_ID").expect("Missing SUBSCRIPTION_ID environment variable.");
var("SUBSCRIPTION_ID").expect("Missing SUBSCRIPTION_ID environment variable.");

let keyvault_uri =
env::var("KEYVAULT_URI").expect("Missing KEYVAULT_URI environment variable.");
let cert_name = env::var("CERT_NAME").expect("Missing CERT_NAME environment variable.");
let keyvault_uri = var("KEYVAULT_URI").expect("Missing KEYVAULT_URI environment variable.");
let cert_name = var("CERT_NAME").expect("Missing CERT_NAME environment variable.");
let cert = get_certficate(&keyvault_uri, &cert_name).await?;

let mut options = CertificateCredentialOptions::default();
Expand All @@ -47,8 +40,8 @@ async fn main() -> Result<(), Box<dyn Error>> {

// pass is empty by default when certificate is fetched from keyvault
let creds = ClientCertificateCredential::new(
tenant_id.to_string(),
client_id.to_string(),
tenant_id,
client_id,
base64::encode(cert),
"".to_string(),
options,
Expand Down
21 changes: 9 additions & 12 deletions sdk/identity/examples/client_credentials_flow.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use azure_identity::client_credentials_flow;
use std::{env::var, error::Error};
use url::Url;

use std::env;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client_id = env::var("CLIENT_ID").expect("Missing CLIENT_ID environment variable.");
let client_secret =
env::var("CLIENT_SECRET").expect("Missing CLIENT_SECRET environment variable.");
let tenant_id = env::var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
let scope = env::var("SCOPE").expect("Missing SCOPE environment variable.");
let client_id = var("CLIENT_ID").expect("Missing CLIENT_ID environment variable.");
let client_secret = var("CLIENT_SECRET").expect("Missing CLIENT_SECRET environment variable.");
let tenant_id = var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
let scope = var("SCOPE").expect("Missing SCOPE environment variable.");
let subscription_id =
var("SUBSCRIPTION_ID").expect("Missing SUBSCRIPTION_ID environment variable.");

let http_client = azure_core::new_http_client();
// This will give you the final token to use in authorization.
Expand All @@ -22,10 +21,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
&tenant_id,
)
.await?;
println!("Non interactive authorization == {token:?}");

let subscription_id =
env::var("SUBSCRIPTION_ID").expect("Missing SUBSCRIPTION_ID environment variable.");
eprintln!("Non interactive authorization == {token:?}");

// Let's enumerate the Azure SQL Databases instances
// in the subscription. Note: this way of calling the REST API
Expand All @@ -46,6 +43,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
.text()
.await?;

println!("\n\nresp {resp:?}");
println!("{resp}");
Ok(())
}
24 changes: 12 additions & 12 deletions sdk/identity/examples/client_credentials_flow_blob.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use azure_core::date;
use azure_core::{date, new_http_client};
use azure_identity::client_credentials_flow;
use std::{
env::{args, var},
error::Error,
};
use time::OffsetDateTime;

use std::env;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client_id = env::var("CLIENT_ID").expect("Missing CLIENT_ID environment variable.");
let client_secret =
env::var("CLIENT_SECRET").expect("Missing CLIENT_SECRET environment variable.");
let tenant_id = env::var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
let client_id = var("CLIENT_ID").expect("Missing CLIENT_ID environment variable.");
let client_secret = var("CLIENT_SECRET").expect("Missing CLIENT_SECRET environment variable.");
let tenant_id = var("TENANT_ID").expect("Missing TENANT_ID environment variable.");

let storage_account_name = std::env::args()
let storage_account_name = args()
.nth(1)
.expect("please specify the storage account name as first command line parameter");
let container_name = std::env::args()
let container_name = args()
.nth(2)
.expect("please specify the container name as second command line parameter");

let http_client = azure_core::new_http_client();
let http_client = new_http_client();

let token = client_credentials_flow::perform(
http_client.clone(),
http_client,
&client_id,
&client_secret,
&[&format!(
Expand Down
27 changes: 12 additions & 15 deletions sdk/identity/examples/code_flow.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
use azure_identity::*;
use azure_core::new_http_client;
use azure_identity::{authorization_code_flow, development::naive_redirect_server};
use oauth2::{ClientId, ClientSecret, TokenResponse};
use std::env;
use std::error::Error;
use std::{env::var, error::Error};
use url::Url;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client_id =
ClientId::new(env::var("CLIENT_ID").expect("Missing CLIENT_ID environment variable."));
ClientId::new(var("CLIENT_ID").expect("Missing CLIENT_ID environment variable."));
let client_secret = ClientSecret::new(
env::var("CLIENT_SECRET").expect("Missing CLIENT_SECRET environment variable."),
var("CLIENT_SECRET").expect("Missing CLIENT_SECRET environment variable."),
);
let tenant_id = env::var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
let tenant_id = var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
let subscription_id =
env::var("SUBSCRIPTION_ID").expect("Missing SUBSCRIPTION_ID environment variable.");
var("SUBSCRIPTION_ID").expect("Missing SUBSCRIPTION_ID environment variable.");

// Create URL to browse for initial authorization
let c = authorization_code_flow::start(
let code_flow = authorization_code_flow::start(
client_id,
Some(client_secret),
&tenant_id,
Url::parse("http://localhost:3003/redirect").unwrap(),
"https://management.azure.com/user_impersonation",
);

println!("c == {c:?}");
println!("\nbrowse this url:\n{}", c.authorize_url);
println!("c == {code_flow:?}");
println!("\nbrowse this url:\n{}", code_flow.authorize_url);

// Start a naive server to receive the redirect with the token. This naive server is blocking
// so you should use something better.
let code = development::naive_redirect_server(&c, 3003).unwrap();
let code = naive_redirect_server(&code_flow, 3003).unwrap();

println!("code received: {code:?}");

// Exchange the token with one that can be used for authorization
let token = c
.exchange(azure_core::new_http_client(), code)
.await
.unwrap();
let token = code_flow.exchange(new_http_client(), code).await.unwrap();

println!("token received: {token:?}");

Expand Down
20 changes: 11 additions & 9 deletions sdk/identity/examples/code_flow_blob.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
use azure_core::date;
use azure_identity::*;
use azure_identity::{authorization_code_flow, development::naive_redirect_server};
use oauth2::{ClientId, ClientSecret, TokenResponse};
use std::env;
use std::error::Error;
use std::{
env::{args, var},
error::Error,
};
use time::OffsetDateTime;
use url::Url;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client_id =
ClientId::new(env::var("CLIENT_ID").expect("Missing CLIENT_ID environment variable."));
ClientId::new(var("CLIENT_ID").expect("Missing CLIENT_ID environment variable."));
let client_secret = ClientSecret::new(
env::var("CLIENT_SECRET").expect("Missing CLIENT_SECRET environment variable."),
var("CLIENT_SECRET").expect("Missing CLIENT_SECRET environment variable."),
);
let tenant_id = env::var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
let tenant_id = var("TENANT_ID").expect("Missing TENANT_ID environment variable.");

let storage_account_name = std::env::args()
let storage_account_name = args()
.nth(1)
.expect("please specify the storage account name as first command line parameter");
let container_name = std::env::args()
let container_name = args()
.nth(2)
.expect("please specify the container name as second command line parameter");

Expand All @@ -36,7 +38,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

// Start a naive redirect server to receive the redirect with the token.
// This naive server is blocking so you should use something better.
let code = development::naive_redirect_server(&c, 3003).unwrap();
let code = naive_redirect_server(&c, 3003).unwrap();

println!("code received: {code:?}");

Expand Down
11 changes: 6 additions & 5 deletions sdk/identity/examples/default_credentials.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use azure_core::auth::TokenCredential;
use azure_identity::*;
use azure_identity::DefaultAzureCredentialBuilder;
use std::{env::var, error::Error};
use url::Url;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();

let sub_id = std::env::var("AZURE_SUBSCRIPTION_ID")?;
let sub_id = var("AZURE_SUBSCRIPTION_ID")?;
let creds = DefaultAzureCredentialBuilder::new()
.exclude_azure_cli_credential() // disable using CLI for credentials (just as an example)
.build();
Expand All @@ -15,7 +16,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.get_token("https://management.azure.com/")
.await
.unwrap();
println!("Azure token response == {res:?}");
eprintln!("Azure token response == {res:?}");
// Let's enumerate the Azure storage accounts
// in the subscription. Note: this way of calling the REST API
// will be different (and easier) using other Azure Rust SDK
Expand All @@ -32,6 +33,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.text()
.await?;

println!("\n\n{resp:?}");
println!("{resp}");
Ok(())
}
13 changes: 5 additions & 8 deletions sdk/identity/examples/environment_credentials.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use azure_core::auth::TokenCredential;
use azure_identity::*;
use std::error::Error;
use std::{env::var, error::Error};
use url::Url;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let sub_id = std::env::var("AZURE_SUBSCRIPTION_ID")?;
let sub_id = var("AZURE_SUBSCRIPTION_ID")?;
let creds = EnvironmentCredential::default();
let res = creds
.get_token("https://management.azure.com/")
.await
.unwrap();
println!("Azure cli response == {res:?}");
let res = creds.get_token("https://management.azure.com/").await?;
eprintln!("Azure cli response == {res:?}");
// Let's enumerate the Azure storage accounts
// in the subscription. Note: this way of calling the REST API
// will be different (and easier) using other Azure Rust SDK
Expand All @@ -28,6 +25,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
.text()
.await?;

println!("\n\nresp {resp:?}");
println!("{resp}");
Ok(())
}
17 changes: 9 additions & 8 deletions sdk/identity/examples/federated_credential.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
use azure_identity::{authority_hosts, federated_credentials_flow};
use std::{
env::{args, var},
error::Error,
};
use url::Url;

use std::env;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client_id = env::var("CLIENT_ID").expect("Missing CLIENT_ID environment variable.");
let token = env::var("FEDERATED_TOKEN").expect("Missing FEDERATED_TOKEN environment variable.");
let tenant_id = env::var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
let client_id = var("CLIENT_ID").expect("Missing CLIENT_ID environment variable.");
let token = var("FEDERATED_TOKEN").expect("Missing FEDERATED_TOKEN environment variable.");
let tenant_id = var("TENANT_ID").expect("Missing TENANT_ID environment variable.");

let vault_name = std::env::args()
let vault_name = args()
.nth(1)
.expect("please specify the vault name as first command line parameter");

let http_client = azure_core::new_http_client();
// This will give you the final token to use in authorization.
let token = federated_credentials_flow::perform(
http_client.clone(),
http_client,
&client_id,
&token,
&["https://vault.azure.net/.default"],
Expand Down

0 comments on commit 0209d17

Please sign in to comment.