azure-sdk-for-cpp/sdk/identity/azure-identity/samples/workload_identity_credential.cpp
Ahson Khan 79737f1473
Make several params to WorkloadIdentityCredential optional and read them from the environment instead. (#4893)
* Make several params to WorkloadIdentityCredential optional and read them from the environment instead.

* Fix typo in calling the ClientCredentialCore ctor

* Add changelog entry.

* Address PR feedback, avoid creating many WIC options instances.

* Update ctor impl and add options test.

* Set locals if the options are non-empty, and void unused variable in tests.

* Fixup the sample since the customer no longer needs to explicitly pass in those params.
2023-09-12 23:18:05 -07:00

40 lines
1.3 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include <azure/identity/workload_identity_credential.hpp>
#include <azure/service/client.hpp>
#include <iostream>
// The following environment variables must be set before running the sample.
// * AZURE_TENANT_ID: Tenant ID for the Azure account.
// * AZURE_CLIENT_ID: The Client ID to authenticate the request.
// * AZURE_FEDERATED_TOKEN_FILE: The path of a file containing a Kubernetes service account token.
int main()
{
try
{
// Step 1: Initialize Workload Identity Credential.
auto workloadIdentityCredential
= std::make_shared<Azure::Identity::WorkloadIdentityCredential>();
// Step 2: Pass the credential to an Azure Service Client.
Azure::Service::Client azureServiceClient("serviceUrl", workloadIdentityCredential);
// Step 3: Start using the Azure Service Client.
azureServiceClient.DoSomething(Azure::Core::Context::ApplicationContext);
std::cout << "Success!" << std::endl;
}
catch (const Azure::Core::Credentials::AuthenticationException& exception)
{
// Step 4: Handle authentication errors, if needed
// (invalid credential parameters, insufficient permissions).
std::cout << "Authentication error: " << exception.what() << std::endl;
return 1;
}
return 0;
}