use conductor::{
client::ConductorClient,
configuration::Configuration,
error::Result,
models::{
AccessType, CreateOrUpdateApplicationRequest, MetadataTag, SubjectRef, SubjectType,
TargetRef, TargetType, UpsertGroupRequest, UpsertUserRequest,
},
};
use tracing::info;
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::from_default_env()
.add_directive("conductor=info".parse().unwrap()),
)
.init();
let config = Configuration::default();
info!("Connecting to Conductor at {}", config.server_api_url);
let client = ConductorClient::new(config)?;
let auth = client.authorization_client();
info!("\n=== Current User Info ===");
match auth.get_user_info_from_token().await {
Ok(user_info) => {
info!("Current user info: {:?}", user_info);
}
Err(e) => {
info!("Could not get user info (may need authentication): {}", e);
}
}
info!("\n=== Available Roles ===");
match auth.list_all_roles().await {
Ok(roles) => {
info!("Available roles: {}", roles.len());
for role in roles.iter().take(5) {
info!(" - {:?}", role);
}
}
Err(e) => {
info!("Could not list roles: {}", e);
}
}
info!("\n=== User Management ===");
let user_id = format!("rust_demo_user_{}", &uuid::Uuid::new_v4().to_string()[..8]);
let user_request = UpsertUserRequest::new(format!("Demo User {}", &user_id[..8]))
.with_roles(vec!["USER".to_owned()]);
match auth.upsert_user(&user_request, &user_id).await {
Ok(user) => {
info!("Created user: {:?}", user.id);
match auth.get_user(&user_id).await {
Ok(retrieved) => {
info!("Retrieved user: {:?}", retrieved.name);
}
Err(e) => info!("Could not retrieve user: {}", e),
}
match auth.list_users(false).await {
Ok(users) => {
info!("Total users: {}", users.len());
}
Err(e) => info!("Could not list users: {}", e),
}
}
Err(e) => {
info!("Could not create user (may need admin permissions): {}", e);
}
}
info!("\n=== Group Management ===");
let group_id = format!("rust_demo_group_{}", &uuid::Uuid::new_v4().to_string()[..8]);
let group_request = UpsertGroupRequest::new(&group_id)
.with_description("Demo group created by Rust SDK")
.with_roles(vec!["USER".to_owned()]);
match auth.upsert_group(&group_request, &group_id).await {
Ok(group) => {
info!("Created group: {:?}", group.id);
match auth.add_user_to_group(&group_id, &user_id).await {
Ok(()) => info!("Added user to group"),
Err(e) => info!("Could not add user to group: {}", e),
}
match auth.get_users_in_group(&group_id).await {
Ok(users) => {
info!("Users in group: {}", users.len());
}
Err(e) => info!("Could not get users in group: {}", e),
}
match auth.list_groups().await {
Ok(groups) => {
info!("Total groups: {}", groups.len());
}
Err(e) => info!("Could not list groups: {}", e),
}
}
Err(e) => {
info!("Could not create group: {}", e);
}
}
info!("\n=== Application Management ===");
let app_name = format!("rust_demo_app_{}", &uuid::Uuid::new_v4().to_string()[..8]);
let app_request = CreateOrUpdateApplicationRequest {
name: app_name.clone(),
};
match auth.create_application(&app_request).await {
Ok(app) => {
info!("Created application: {:?}", app.id);
let app_id = &app.id;
match auth.create_access_key(app_id).await {
Ok(key) => {
info!("Created access key: {:?}", key.id);
match auth.get_access_keys(app_id).await {
Ok(keys) => {
info!("Total access keys: {}", keys.len());
}
Err(e) => info!("Could not list access keys: {}", e),
}
let key_id = &key.id;
match auth.toggle_access_key_status(app_id, key_id).await {
Ok(toggled) => {
info!("Toggled key status: {:?}", toggled.status);
}
Err(e) => info!("Could not toggle key: {}", e),
}
match auth.delete_access_key(app_id, key_id).await {
Ok(()) => info!("Deleted access key"),
Err(e) => info!("Could not delete access key: {}", e),
}
}
Err(e) => info!("Could not create access key: {}", e),
}
let tags = vec![
MetadataTag::with_value("environment", "demo"),
MetadataTag::with_value("created_by", "rust-sdk"),
];
match auth.set_application_tags(&tags, app_id).await {
Ok(()) => info!("Added tags to application"),
Err(e) => info!("Could not add tags: {}", e),
}
match auth.delete_application(app_id).await {
Ok(()) => info!("Deleted application"),
Err(e) => info!("Could not delete application: {}", e),
}
}
Err(e) => {
info!("Could not create application: {}", e);
}
}
info!("\n=== Permission Management ===");
let subject = SubjectRef {
id: user_id.clone(),
subject_type: SubjectType::User,
};
let target = TargetRef {
id: "test_workflow".to_owned(),
target_type: TargetType::WorkflowDef,
};
let access = vec![AccessType::Execute, AccessType::Read];
match auth.grant_permissions(&subject, &target, &access).await {
Ok(()) => {
info!("Granted permissions to user over workflow");
match auth.get_permissions(&target).await {
Ok(perms) => {
info!("Permissions for target: {:?}", perms);
}
Err(e) => info!("Could not get permissions: {}", e),
}
match auth
.check_permissions(&user_id, "WORKFLOW", "test_workflow")
.await
{
Ok(result) => {
info!("User permission check: {:?}", result);
}
Err(e) => info!("Could not check permissions: {}", e),
}
match auth.remove_permissions(&subject, &target, &access).await {
Ok(()) => info!("Removed permissions"),
Err(e) => info!("Could not remove permissions: {}", e),
}
}
Err(e) => {
info!("Could not grant permissions: {}", e);
}
}
info!("\n=== Cleanup ===");
match auth.remove_user_from_group(&group_id, &user_id).await {
Ok(()) => info!("Removed user from group"),
Err(e) => info!("Could not remove user from group: {}", e),
}
match auth.delete_group(&group_id).await {
Ok(()) => info!("Deleted group"),
Err(e) => info!("Could not delete group: {}", e),
}
match auth.delete_user(&user_id).await {
Ok(()) => info!("Deleted user"),
Err(e) => info!("Could not delete user: {}", e),
}
info!("\nAuthorization example completed!");
Ok(())
}