From f7452cc2334014061b02960452cea6859d1a57ca Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Wed, 6 Sep 2023 10:25:26 -0700 Subject: [PATCH] Update the workload identity credential sample to work with required environment variables that need to be set. (#4924) * Update the workload identity credential sample to work with required environment variables that need to be set. * Fix clang format. --- .../samples/workload_identity_credential.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/sdk/identity/azure-identity/samples/workload_identity_credential.cpp b/sdk/identity/azure-identity/samples/workload_identity_credential.cpp index 21e906d8e..c46683f90 100644 --- a/sdk/identity/azure-identity/samples/workload_identity_credential.cpp +++ b/sdk/identity/azure-identity/samples/workload_identity_credential.cpp @@ -9,10 +9,19 @@ // 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_CLIENT_CERTIFICATE_PATH: The path to a client certificate. +// * AZURE_FEDERATED_TOKEN_FILE: The path of a file containing a Kubernetes service account token. std::string GetTenantId() { return std::getenv("AZURE_TENANT_ID"); } std::string GetClientId() { return std::getenv("AZURE_CLIENT_ID"); } -std::string GetTokenFilePath() { return std::getenv("AZURE_FEDERATED_TOKEN_FILE"); } +std::string GetTokenFilePath() +{ + char const* const tokenFilePath{std::getenv("AZURE_FEDERATED_TOKEN_FILE")}; + if (tokenFilePath == nullptr) + { + std::cerr << "Missing environment variable AZURE_FEDERATED_TOKEN_FILE" << std::endl; + return std::string(); + } + return tokenFilePath; +} int main() {