From 2aae6be7a36de2135a11f8c2552eb2d4143f0536 Mon Sep 17 00:00:00 2001 From: Anton Kolesnyk <41349689+antkmsft@users.noreply.github.com> Date: Sat, 29 Jan 2022 00:22:33 -0800 Subject: [PATCH] Encapsulate getenv(), and make it work on UWP (#3275) It all started with UWP. The [docs](https://docs.microsoft.com/en-us/cpp/cppcx/crt-functions-not-supported-in-universal-windows-platform-apps?view=msvc-170) say: "`Environment variables are not available to UWP apps.`". And it truly won't work, I tried: linker error, the function is simply not present. So, for a year or so, we were `ifdef`ing everything enivoronment-related: console logger, environment credential, managed identity credential. And then just recently we wanted to enable our CI for UWP, including tests and samples. And it required to do more ifdefs (in vcpkg, we don't build samples or tests, so that problem did not exist). It just became more messy. Especially in samples - you can see how we would disable warning with `#pragma warning(disable : 4996)` or defining `_CRT_SECURE_NO_WARNINGS` already, but now came UWP, so we would have to add comment that `getenv()` is not available and make the sample compilation to either fail with clear message, or throw an exception. Plus we would have to detect that we are being compiled on UWP, which also adds visual clutter for reader. You can see how such an irrelevant (for a sample) thing as `getenv` was consuming more and more lines of sample code and reader's attention. But then! I read docs on more APIs for UWP. And I noticed that on .NET you can read environment variables. So I went and checked Win32 API docs for [GetEnvironmentVariable()](https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getenvironmentvariable) - it says: "`Minimum supported client: ... UWP apps`". **GetEnvironmentVariable() works on UWP!** And so does `SetEnvironmentVariable()` (our tests use it, which means we can make all of them work and execute for UWP). That's good news, but now it would probably be more code: it usually takes more lines to invoke WinAPI, it is no more an one-liner to call `getenv()/setenv()`. So, I encapsulated that into `Azure::Core::_internal::Environment::GetVariable()` and `SetVariable()`. You can see how much less ifdefs is in our code now. Not to mention it works on UWP! Per team request, that API is SDK-internal. Samples use their own mini-helper project, `get-env-helper` that makes is so that `getenv()` works naturally on Linux and macOS, compiles without warnings and works on Win32, and compiles and works on UWP (using `GetEnvironmentStringsA()`) If it was for me, I would just make `Azure::Core::Environment::GetVariable()` public and simplify even further, I think it would be beneficial for sample readers (you can see that extra `get-env-helper` stuff adds just a little more visual clutter, compared to nothing). But I can see reasons against that, why team did not want to do it. --- CMakeLists.txt | 9 ++- samples/helpers/get-env/CMakeLists.txt | 18 +++++ samples/helpers/get-env/LICENSE | 21 ++++++ samples/helpers/get-env/README.md | 5 ++ samples/helpers/get-env/inc/get_env.hpp | 36 +++++++++ samples/helpers/get-env/src/get_env.cpp | 48 ++++++++++++ .../cmake-fetch-content/CMakeLists.txt | 2 +- .../cmake-fetch-content/src/main.cpp | 2 + .../integration/vcpkg-keyvault/CMakeLists.txt | 1 + .../integration/vcpkg-keyvault/src/main.cpp | 4 +- .../integration/vcpkg-storage/CMakeLists.txt | 1 + .../integration/vcpkg-storage/src/main.cpp | 2 + sdk/core/azure-core-test/CMakeLists.txt | 2 - .../inc/azure/core/test/test_base.hpp | 14 +--- sdk/core/azure-core-test/src/environment.cpp | 75 ------------------- .../src/interceptor_manager.cpp | 35 +++++++-- .../src/private/environment.hpp | 20 ----- .../azure-core-test/src/record_policy.cpp | 1 - sdk/core/azure-core/CHANGELOG.md | 2 + sdk/core/azure-core/CMakeLists.txt | 2 + .../inc/azure/core/internal}/environment.hpp | 5 +- sdk/core/azure-core/src/environment.cpp | 73 ++++++++++++++++++ .../src/environment_log_level_listener.cpp | 33 +------- .../environment_log_level_listener.hpp | 25 ------- .../environment_log_level_listener_test.cpp | 46 +++++------- sdk/identity/azure-identity/CHANGELOG.md | 2 + sdk/identity/azure-identity/CMakeLists.txt | 2 - .../azure-identity/src/environment.cpp | 45 ----------- .../src/environment_credential.cpp | 4 +- .../src/managed_identity_source.cpp | 3 +- .../test/e2e/azure_identity_e2e_test.cpp | 25 ++----- .../test/ut/credential_test_helper.cpp | 45 +---------- .../test/ut/credential_test_helper.hpp | 2 - .../test/ut/environment_credential_test.cpp | 30 -------- .../CMakeLists.txt | 3 +- .../certificate_basic_operations.cpp | 5 +- .../CMakeLists.txt | 2 +- .../certificate_get_certificates.cpp | 5 +- .../CMakeLists.txt | 2 +- .../certificate_import_certificate.cpp | 5 +- .../sample1-hello-world/CMakeLists.txt | 2 +- .../sample1_hello_world.cpp | 12 ++- .../sample2-backup-and-restore/CMakeLists.txt | 2 +- .../sample2_backup_and_restore.cpp | 4 +- .../samples/sample3-get-keys/CMakeLists.txt | 2 +- .../sample3-get-keys/sample3_get_keys.cpp | 4 +- .../sample4-encrypt-decrypt/CMakeLists.txt | 2 +- .../sample4_encrypt_decrypt.cpp | 4 +- .../sample5-sign-verify/CMakeLists.txt | 2 +- .../sample5_sign_verify.cpp | 4 +- .../sample6-wrap-unwrap/CMakeLists.txt | 2 +- .../sample6_wrap_unwrap.cpp | 4 +- .../test/ut/key_client_backup_test_live.cpp | 4 - .../test/ut/key_client_create_test_live.cpp | 4 - .../test/ut/key_client_delete_test_live.cpp | 4 - .../test/ut/key_client_get_test_live.cpp | 4 - .../test/ut/key_client_import_test_live.cpp | 4 - .../test/ut/key_client_update_test_live.cpp | 4 - .../ut/key_cryptographic_client_test_live.cpp | 4 - .../test/ut/mocked_client_test.cpp | 4 - .../test/ut/mocked_transport_adapter_test.hpp | 9 +++ .../sample1-basic-operations/CMakeLists.txt | 2 +- .../sample1_basic_operations.cpp | 5 +- .../sample2-backup-restore/CMakeLists.txt | 2 +- .../sample2_backup_restore.cpp | 4 +- .../sample3-delete-recover/CMakeLists.txt | 2 +- .../sample3_delete_recover.cpp | 5 +- .../CMakeLists.txt | 2 +- .../sample4_get_secrets_deleted.cpp | 4 +- .../test/test-app/test_app.cpp | 14 ++-- .../test/ut/secret_client_base_test.hpp | 2 +- .../samples/CMakeLists.txt | 8 +- .../samples/blob_getting_started.cpp | 4 +- .../samples/blob_list_operation.cpp | 4 +- .../azure-storage-blobs/samples/blob_sas.cpp | 4 +- .../samples/transactional_checksum.cpp | 4 +- ...zure_storage_blobs_fault_injector_test.cpp | 8 +- .../storage/blobs/test/blob_base_test.hpp | 4 +- .../test/ut/test_base.cpp | 12 ++- .../samples/CMakeLists.txt | 2 +- .../samples/datalake_getting_started.cpp | 4 +- .../samples/CMakeLists.txt | 2 +- .../samples/file_share_getting_started.cpp | 4 +- .../samples/CMakeLists.txt | 4 +- .../samples/queue_encode_message.cpp | 4 +- .../samples/queue_getting_started.cpp | 4 +- 86 files changed, 380 insertions(+), 481 deletions(-) create mode 100644 samples/helpers/get-env/CMakeLists.txt create mode 100644 samples/helpers/get-env/LICENSE create mode 100644 samples/helpers/get-env/README.md create mode 100644 samples/helpers/get-env/inc/get_env.hpp create mode 100644 samples/helpers/get-env/src/get_env.cpp delete mode 100644 sdk/core/azure-core-test/src/environment.cpp delete mode 100644 sdk/core/azure-core-test/src/private/environment.hpp rename sdk/{identity/azure-identity/src/private => core/azure-core/inc/azure/core/internal}/environment.hpp (64%) create mode 100644 sdk/core/azure-core/src/environment.cpp delete mode 100644 sdk/identity/azure-identity/src/environment.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e5b200877..6dff49856 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,10 +89,17 @@ include(AzureDoxygen) # Functions for library versions include(AzureVersion) +if(BUILD_SAMPLES) + add_subdirectory(samples/helpers/get-env) +endif() + # sub-projects add_subdirectory(sdk/core) add_subdirectory(sdk/identity) add_subdirectory(sdk/keyvault) add_subdirectory(sdk/storage) add_subdirectory(sdk/template) -add_subdirectory(samples/integration/vcpkg-keyvault) + +if(BUILD_SAMPLES) + add_subdirectory(samples/integration/vcpkg-keyvault) +endif() diff --git a/samples/helpers/get-env/CMakeLists.txt b/samples/helpers/get-env/CMakeLists.txt new file mode 100644 index 000000000..af2a896b3 --- /dev/null +++ b/samples/helpers/get-env/CMakeLists.txt @@ -0,0 +1,18 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: MIT + +cmake_minimum_required (VERSION 3.12) +project(get-env-helper LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED True) +set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + +add_library( + get-env-helper + OBJECT + inc/get_env.hpp + src/get_env.cpp +) + +target_include_directories(get-env-helper PUBLIC inc) diff --git a/samples/helpers/get-env/LICENSE b/samples/helpers/get-env/LICENSE new file mode 100644 index 000000000..51b6a76e5 --- /dev/null +++ b/samples/helpers/get-env/LICENSE @@ -0,0 +1,21 @@ + MIT License + + Copyright (c) Microsoft Corporation. All rights reserved. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. diff --git a/samples/helpers/get-env/README.md b/samples/helpers/get-env/README.md new file mode 100644 index 000000000..9fc8683fc --- /dev/null +++ b/samples/helpers/get-env/README.md @@ -0,0 +1,5 @@ +# Get Environment Variable for Samples + +This is a helper library for samples that deals with getting environment variables. + +Since `getenv()` may generate warnings on MSVC, and is not available on UWP, sample code gets cluttered with minor platform-specific nuances. This library hides all that, so that `std::getenv()` compiles and works the same on Linux, macOS, Win32, and UWP. diff --git a/samples/helpers/get-env/inc/get_env.hpp b/samples/helpers/get-env/inc/get_env.hpp new file mode 100644 index 000000000..ebd54669b --- /dev/null +++ b/samples/helpers/get-env/inc/get_env.hpp @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: MIT + +// Since `getenv()` may generate warnings on MSVC, and is not available on UWP, sample code +// gets cluttered with insignificant nuances. This file makes it so that `std::getenv()` compiles +// and works the same on Linux, macOS, Win32, and UWP. + +#pragma once + +#if !defined(_MSC_VER) + +// Linux and macOS +#include + +#else +#define _CRT_SECURE_NO_WARNINGS + +#if !defined(WIN32_LEAN_AND_MEAN) +#define WIN32_LEAN_AND_MEAN +#endif +#if !defined(NOMINMAX) +#define NOMINMAX +#endif +#include + +#if !defined(WINAPI_PARTITION_DESKTOP) || WINAPI_PARTITION_DESKTOP +// Win32 +#include +#else +// UWP +namespace std { +char* getenv(const char* name); +} +#endif + +#endif diff --git a/samples/helpers/get-env/src/get_env.cpp b/samples/helpers/get-env/src/get_env.cpp new file mode 100644 index 000000000..50921035e --- /dev/null +++ b/samples/helpers/get-env/src/get_env.cpp @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: MIT + +#include "get_env.hpp" + +#include + +#if defined(WINAPI_PARTITION_DESKTOP) && !WINAPI_PARTITION_DESKTOP + +char* std::getenv(const char* name) +{ + char* buf = GetEnvironmentStringsA(); + + for (auto i = 0; *buf != '\0';) + { + if (name[i] == '\0' && *buf == '=') + { + // We've found "name=", the rest will be the value with '\0' at the end. + return buf + 1; + } + + // We're still trying to match the name. + if (std::toupper(*buf) == std::toupper(name[i])) + { + // Matching so far, keep matching name and buffer, char by char, case insensitive. + ++i; + ++buf; + + continue; + } + + // Variable name character did not match the buffer, reset. + i = 0; + + // Skip till the end of current "name=value" pair. + do + { + ++buf; + } while (*buf != '\0'); + + // Increment buf to point to the start of the next "name=value" pair. + ++buf; + } + + return nullptr; +} + +#endif diff --git a/samples/integration/cmake-fetch-content/CMakeLists.txt b/samples/integration/cmake-fetch-content/CMakeLists.txt index a69e0f613..611149092 100644 --- a/samples/integration/cmake-fetch-content/CMakeLists.txt +++ b/samples/integration/cmake-fetch-content/CMakeLists.txt @@ -27,4 +27,4 @@ add_executable ( ) # Link to Azure SDK -target_link_libraries(application Azure::azure-storage-blobs) +target_link_libraries(application Azure::azure-storage-blobs get-env-helper) diff --git a/samples/integration/cmake-fetch-content/src/main.cpp b/samples/integration/cmake-fetch-content/src/main.cpp index 0cc0d158c..904255f1b 100644 --- a/samples/integration/cmake-fetch-content/src/main.cpp +++ b/samples/integration/cmake-fetch-content/src/main.cpp @@ -9,6 +9,8 @@ * */ +#include "get_env.hpp" + #include #include diff --git a/samples/integration/vcpkg-keyvault/CMakeLists.txt b/samples/integration/vcpkg-keyvault/CMakeLists.txt index 4e756c3a4..1707c6a2c 100644 --- a/samples/integration/vcpkg-keyvault/CMakeLists.txt +++ b/samples/integration/vcpkg-keyvault/CMakeLists.txt @@ -26,4 +26,5 @@ target_link_libraries(application PRIVATE Azure::azure-security-keyvault-keys Azure::azure-identity + get-env-helper ) diff --git a/samples/integration/vcpkg-keyvault/src/main.cpp b/samples/integration/vcpkg-keyvault/src/main.cpp index 3718f0964..b8d2c6923 100644 --- a/samples/integration/vcpkg-keyvault/src/main.cpp +++ b/samples/integration/vcpkg-keyvault/src/main.cpp @@ -13,9 +13,7 @@ * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/samples/integration/vcpkg-storage/CMakeLists.txt b/samples/integration/vcpkg-storage/CMakeLists.txt index 375adfc42..c6ca69c64 100644 --- a/samples/integration/vcpkg-storage/CMakeLists.txt +++ b/samples/integration/vcpkg-storage/CMakeLists.txt @@ -28,4 +28,5 @@ add_executable ( target_link_libraries(application PRIVATE Azure::azure-storage-blobs + get-env-helper ) diff --git a/samples/integration/vcpkg-storage/src/main.cpp b/samples/integration/vcpkg-storage/src/main.cpp index a961c2725..7d7f873a8 100644 --- a/samples/integration/vcpkg-storage/src/main.cpp +++ b/samples/integration/vcpkg-storage/src/main.cpp @@ -10,6 +10,8 @@ * */ +#include "get_env.hpp" + #include #include diff --git a/sdk/core/azure-core-test/CMakeLists.txt b/sdk/core/azure-core-test/CMakeLists.txt index 225091a2c..6ae3b14ef 100644 --- a/sdk/core/azure-core-test/CMakeLists.txt +++ b/sdk/core/azure-core-test/CMakeLists.txt @@ -20,9 +20,7 @@ set( set( AZURE_CORE_TEST_SOURCE - src/private/environment.hpp src/private/package_version.hpp - src/environment.cpp src/interceptor_manager.cpp src/playback_http_transport.cpp src/record_policy.cpp diff --git a/sdk/core/azure-core-test/inc/azure/core/test/test_base.hpp b/sdk/core/azure-core-test/inc/azure/core/test/test_base.hpp index 7cb5187c1..a43d8c92c 100644 --- a/sdk/core/azure-core-test/inc/azure/core/test/test_base.hpp +++ b/sdk/core/azure-core-test/inc/azure/core/test/test_base.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "azure/core/test/interceptor_manager.hpp" #include "azure/core/test/network_models.hpp" @@ -245,21 +246,14 @@ namespace Azure { namespace Core { namespace Test { // Util for tests getting env vars std::string GetEnv(const std::string& name) { -#if defined(_MSC_VER) -#pragma warning(push) -#pragma warning(disable : 4996) - const char* ret = std::getenv(name.data()); -#pragma warning(pop) -#else - const char* ret = std::getenv(name.data()); -#endif + const auto ret = Azure::Core::_internal::Environment::GetVariable(name.c_str()); - if (!ret) + if (ret.empty()) { throw std::runtime_error("Missing required environment variable: " + name); } - return std::string(ret); + return ret; } // Util to set recording path diff --git a/sdk/core/azure-core-test/src/environment.cpp b/sdk/core/azure-core-test/src/environment.cpp deleted file mode 100644 index 32d197ed4..000000000 --- a/sdk/core/azure-core-test/src/environment.cpp +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// SPDX-License-Identifier: MIT - -#include "private/environment.hpp" -#include "azure/core/platform.hpp" - -#include - -#include -#include - -#if defined(AZ_PLATFORM_WINDOWS) -#if !defined(WIN32_LEAN_AND_MEAN) -#define WIN32_LEAN_AND_MEAN -#endif -#if !defined(NOMINMAX) -#define NOMINMAX -#endif - -#include -#endif - -using namespace Azure::Core::Test::_detail; - -std::string Environment::GetVariable(const char* name) -{ -#if !defined(WINAPI_PARTITION_DESKTOP) \ - || WINAPI_PARTITION_DESKTOP // See azure/core/platform.hpp for explanation. -#if defined(_MSC_VER) -#pragma warning(push) -// warning C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s -// instead. -#pragma warning(disable : 4996) -#endif - - if (auto envVar = std::getenv(name)) - { - return std::string(envVar); - } - -#if defined(_MSC_VER) -#pragma warning(pop) -#endif -#endif - - return std::string(); -} - -Azure::Core::Test::TestMode Environment::GetTestMode() -{ - auto value = Environment::GetVariable("AZURE_TEST_MODE"); - if (value.empty()) - { - return Azure::Core::Test::TestMode::LIVE; - } - - if (Azure::Core::_internal::StringExtensions::LocaleInvariantCaseInsensitiveEqual( - value, "RECORD")) - { - return Azure::Core::Test::TestMode::RECORD; - } - else if (Azure::Core::_internal::StringExtensions::LocaleInvariantCaseInsensitiveEqual( - value, "PLAYBACK")) - { - return Azure::Core::Test::TestMode::PLAYBACK; - } - else if (Azure::Core::_internal::StringExtensions::LocaleInvariantCaseInsensitiveEqual( - value, "LIVE")) - { - return Azure::Core::Test::TestMode::LIVE; - } - - // unexpected variable value - throw std::runtime_error("Invalid environment variable: " + value); -} diff --git a/sdk/core/azure-core-test/src/interceptor_manager.cpp b/sdk/core/azure-core-test/src/interceptor_manager.cpp index c29296fda..60065922d 100644 --- a/sdk/core/azure-core-test/src/interceptor_manager.cpp +++ b/sdk/core/azure-core-test/src/interceptor_manager.cpp @@ -1,15 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#ifdef _MSC_VER -#define _CRT_SECURE_NO_WARNINGS // for std::getenv() -#endif - +#include #include #include #include "azure/core/test/interceptor_manager.hpp" -#include "private/environment.hpp" #include #include @@ -20,8 +16,35 @@ using namespace Azure::Core::Test; using namespace Azure::Core::Json::_internal; using namespace Azure::Core; +using Azure::Core::_internal::Environment; -TestMode InterceptorManager::GetTestMode() { return _detail::Environment::GetTestMode(); } +TestMode InterceptorManager::GetTestMode() +{ + auto value = Environment::GetVariable("AZURE_TEST_MODE"); + if (value.empty()) + { + return Azure::Core::Test::TestMode::LIVE; + } + + if (Azure::Core::_internal::StringExtensions::LocaleInvariantCaseInsensitiveEqual( + value, "RECORD")) + { + return Azure::Core::Test::TestMode::RECORD; + } + else if (Azure::Core::_internal::StringExtensions::LocaleInvariantCaseInsensitiveEqual( + value, "PLAYBACK")) + { + return Azure::Core::Test::TestMode::PLAYBACK; + } + else if (Azure::Core::_internal::StringExtensions::LocaleInvariantCaseInsensitiveEqual( + value, "LIVE")) + { + return Azure::Core::Test::TestMode::LIVE; + } + + // unexpected variable value + throw std::runtime_error("Invalid environment variable: " + value); +} void InterceptorManager::LoadTestData() { diff --git a/sdk/core/azure-core-test/src/private/environment.hpp b/sdk/core/azure-core-test/src/private/environment.hpp deleted file mode 100644 index bdd8c38e5..000000000 --- a/sdk/core/azure-core-test/src/private/environment.hpp +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// SPDX-License-Identifier: MIT - -#pragma once - -#include - -#include - -namespace Azure { namespace Core { namespace Test { namespace _detail { - class Environment final { - private: - Environment() = delete; - ~Environment() = delete; - - public: - static std::string GetVariable(const char* name); - static Azure::Core::Test::TestMode GetTestMode(); - }; -}}}} // namespace Azure::Core::Test::_detail diff --git a/sdk/core/azure-core-test/src/record_policy.cpp b/sdk/core/azure-core-test/src/record_policy.cpp index 0e2f46466..b2474de12 100644 --- a/sdk/core/azure-core-test/src/record_policy.cpp +++ b/sdk/core/azure-core-test/src/record_policy.cpp @@ -4,7 +4,6 @@ #include "azure/core/test/interceptor_manager.hpp" #include "azure/core/test/network_models.hpp" #include "azure/core/test/record_network_call_policy.hpp" -#include "private/environment.hpp" #include diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index e570e081e..21939dc60 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -4,6 +4,8 @@ ### Features Added +- Enabled environment-controlled console logging on UWP. + ### Breaking Changes - Removed the `AzureNoReturnPath()` function from the global namespace, and deprecated the associated macros, such as `AZURE_ASSERT` since they are meant for internal use only. If your code was using the `AZURE_ASSERT` macro, consider using the standard library's `assert` as an alternative. diff --git a/sdk/core/azure-core/CMakeLists.txt b/sdk/core/azure-core/CMakeLists.txt index d17937827..99d2ed94c 100644 --- a/sdk/core/azure-core/CMakeLists.txt +++ b/sdk/core/azure-core/CMakeLists.txt @@ -71,6 +71,7 @@ set( inc/azure/core/internal/contract.hpp inc/azure/core/internal/cryptography/sha_hash.hpp inc/azure/core/internal/diagnostics/log.hpp + inc/azure/core/internal/environment.hpp inc/azure/core/internal/http/pipeline.hpp inc/azure/core/internal/io/null_body_stream.hpp inc/azure/core/internal/json/json_serializable.hpp @@ -122,6 +123,7 @@ set( src/base64.cpp src/context.cpp src/datetime.cpp + src/environment.cpp src/environment_log_level_listener.cpp src/etag.cpp src/exception.cpp diff --git a/sdk/identity/azure-identity/src/private/environment.hpp b/sdk/core/azure-core/inc/azure/core/internal/environment.hpp similarity index 64% rename from sdk/identity/azure-identity/src/private/environment.hpp rename to sdk/core/azure-core/inc/azure/core/internal/environment.hpp index 251d0bf41..1e42e29bd 100644 --- a/sdk/identity/azure-identity/src/private/environment.hpp +++ b/sdk/core/azure-core/inc/azure/core/internal/environment.hpp @@ -5,7 +5,7 @@ #include -namespace Azure { namespace Identity { namespace _detail { +namespace Azure { namespace Core { namespace _internal { class Environment final { private: Environment() = delete; @@ -13,5 +13,6 @@ namespace Azure { namespace Identity { namespace _detail { public: static std::string GetVariable(const char* name); + static void SetVariable(const char* name, const char* value); }; -}}} // namespace Azure::Identity::_detail +}}} // namespace Azure::Core::_internal diff --git a/sdk/core/azure-core/src/environment.cpp b/sdk/core/azure-core/src/environment.cpp new file mode 100644 index 000000000..cb4c9c47b --- /dev/null +++ b/sdk/core/azure-core/src/environment.cpp @@ -0,0 +1,73 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: MIT + +#include "azure/core/platform.hpp" + +#include "azure/core/internal/environment.hpp" + +#if defined(AZ_PLATFORM_WINDOWS) +#if !defined(WIN32_LEAN_AND_MEAN) +#define WIN32_LEAN_AND_MEAN +#endif +#if !defined(NOMINMAX) +#define NOMINMAX +#endif + +#include + +#include +#else +#include +#endif + +using Azure::Core::_internal::Environment; + +std::string Environment::GetVariable(const char* name) +{ + if (name != nullptr && name[0] != 0) + { +#if defined(AZ_PLATFORM_WINDOWS) + std::vector bufferVector; + std::string::value_type* buffer = nullptr; + DWORD bufferSize = 0; + while (const auto requiredSize = GetEnvironmentVariableA(name, buffer, bufferSize)) + { + if (requiredSize < bufferSize) + { + return std::string(buffer, buffer + (bufferSize - 1)); + } + + bufferVector.resize(static_cast(requiredSize)); + bufferSize = requiredSize; + buffer = bufferVector.data(); + } +#else + if (const auto value = getenv(name)) + { + return std::string(value); + } +#endif + } + return std::string(); +} + +void Environment::SetVariable(const char* name, const char* value) +{ + if (name != nullptr && name[0] != 0) + { + const auto isEmptyValue = (value == nullptr || value[0] == 0); + +#if defined(AZ_PLATFORM_WINDOWS) + static_cast(SetEnvironmentVariableA(name, isEmptyValue ? nullptr : value)); +#else + if (isEmptyValue) + { + static_cast(unsetenv(name)); + } + else + { + static_cast(setenv(name, value, 1)); + } +#endif + } +} diff --git a/sdk/core/azure-core/src/environment_log_level_listener.cpp b/sdk/core/azure-core/src/environment_log_level_listener.cpp index 0f187924b..ffcf1e607 100644 --- a/sdk/core/azure-core/src/environment_log_level_listener.cpp +++ b/sdk/core/azure-core/src/environment_log_level_listener.cpp @@ -1,30 +1,16 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#include "azure/core/platform.hpp" - -#if defined(AZ_PLATFORM_WINDOWS) -#if !defined(WIN32_LEAN_AND_MEAN) -#define WIN32_LEAN_AND_MEAN -#endif -#if !defined(NOMINMAX) -#define NOMINMAX -#endif - -#include -#endif - -#if (!defined(WINAPI_PARTITION_DESKTOP) || WINAPI_PARTITION_DESKTOP) // See azure/core/platform.hpp - // for explanation. - #include "private/environment_log_level_listener.hpp" #include "azure/core/datetime.hpp" +#include "azure/core/internal/environment.hpp" #include "azure/core/internal/strings.hpp" #include #include +using Azure::Core::_internal::Environment; using namespace Azure::Core::Diagnostics; using namespace Azure::Core::Diagnostics::_detail; using Azure::Core::Diagnostics::_detail::EnvironmentLogLevelListener; @@ -38,18 +24,8 @@ Logger::Level const* GetEnvironmentLogLevel() { EnvironmentLogLevelListener::SetInitialized(true); -#if defined(_MSC_VER) -#pragma warning(push) -// warning C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s -// instead. -#pragma warning(disable : 4996) -#endif - auto envVar = std::getenv("AZURE_LOG_LEVEL"); -#if defined(_MSC_VER) -#pragma warning(pop) -#endif - - if (envVar) + auto const envVar = Environment::GetVariable("AZURE_LOG_LEVEL"); + if (!envVar.empty()) { auto const logLevelStr = Azure::Core::_internal::StringExtensions::ToLower(envVar); @@ -151,4 +127,3 @@ static bool g_initialized; bool EnvironmentLogLevelListener::IsInitialized() { return g_initialized; } void EnvironmentLogLevelListener::SetInitialized(bool value) { g_initialized = value; } -#endif diff --git a/sdk/core/azure-core/src/private/environment_log_level_listener.hpp b/sdk/core/azure-core/src/private/environment_log_level_listener.hpp index 9fdf4b000..3dab1c880 100644 --- a/sdk/core/azure-core/src/private/environment_log_level_listener.hpp +++ b/sdk/core/azure-core/src/private/environment_log_level_listener.hpp @@ -5,18 +5,6 @@ #include "azure/core/diagnostics/logger.hpp" -#if defined(AZ_PLATFORM_WINDOWS) -#if !defined(WIN32_LEAN_AND_MEAN) -#define WIN32_LEAN_AND_MEAN -#endif -#if !defined(NOMINMAX) -#define NOMINMAX -#endif - -// This use of windows.h within the header is OK because the header is private and in source only. -#include -#endif - namespace Azure { namespace Core { namespace Diagnostics { namespace _detail { class EnvironmentLogLevelListener final { @@ -30,17 +18,4 @@ namespace Azure { namespace Core { namespace Diagnostics { namespace _detail { static void SetInitialized(bool value); }; -#if (defined(WINAPI_PARTITION_DESKTOP) && !WINAPI_PARTITION_DESKTOP) // See azure/core/platform.hpp - // for explanation. - inline Logger::Level EnvironmentLogLevelListener::GetLogLevel(Logger::Level defaultValue) - { - return defaultValue; - } - - inline std::function - EnvironmentLogLevelListener::GetLogListener() - { - return nullptr; - } -#endif }}}} // namespace Azure::Core::Diagnostics::_detail diff --git a/sdk/core/azure-core/test/ut/environment_log_level_listener_test.cpp b/sdk/core/azure-core/test/ut/environment_log_level_listener_test.cpp index fb63af6f1..85f3b009f 100644 --- a/sdk/core/azure-core/test/ut/environment_log_level_listener_test.cpp +++ b/sdk/core/azure-core/test/ut/environment_log_level_listener_test.cpp @@ -1,23 +1,22 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#include #include +#include + +#include + using Azure::Core::Diagnostics::Logger; using Azure::Core::Diagnostics::_detail::EnvironmentLogLevelListener; namespace { -std::string environmentVariable = "AZURE_LOG_LEVEL"; +const auto EnvironmentVariable = "AZURE_LOG_LEVEL"; void SetLogLevel(std::string const& value) { -#if defined(_MSC_VER) - static_cast(_putenv((environmentVariable + "=" + value).c_str())); -#else - static_cast(setenv(environmentVariable.c_str(), value.c_str(), 1)); -#endif + Azure::Core::_internal::Environment::SetVariable(EnvironmentVariable, value.c_str()); } } // namespace @@ -25,17 +24,7 @@ class EnvironmentLogLevelListenerTest : public testing::Test { protected: void SetUp() override { -#if defined(_MSC_VER) -#pragma warning(push) -// warning C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s -// instead. -#pragma warning(disable : 4996) -#endif - auto const value = std::getenv(environmentVariable.c_str()); - m_previousValue = value == nullptr ? "" : value; -#if defined(_MSC_VER) -#pragma warning(pop) -#endif + m_previousValue = Azure::Core::_internal::Environment::GetVariable(EnvironmentVariable); } void TearDown() override { SetLogLevel(m_previousValue); } @@ -119,17 +108,17 @@ TEST_F(EnvironmentLogLevelListenerTest, LogLevelVerbose) { EnvironmentLogLevelListener::SetInitialized(false); SetLogLevel("verbose"); - auto level = EnvironmentLogLevelListener::GetLogLevel(Logger::Level::Verbose); + auto level = EnvironmentLogLevelListener::GetLogLevel(Logger::Level::Error); EXPECT_EQ(level, Logger::Level::Verbose); EnvironmentLogLevelListener::SetInitialized(false); SetLogLevel("debug"); - level = EnvironmentLogLevelListener::GetLogLevel(Logger::Level::Verbose); + level = EnvironmentLogLevelListener::GetLogLevel(Logger::Level::Error); EXPECT_EQ(level, Logger::Level::Verbose); EnvironmentLogLevelListener::SetInitialized(false); SetLogLevel("1"); - level = EnvironmentLogLevelListener::GetLogLevel(Logger::Level::Verbose); + level = EnvironmentLogLevelListener::GetLogLevel(Logger::Level::Error); EXPECT_EQ(level, Logger::Level::Verbose); } @@ -144,8 +133,9 @@ TEST_F(EnvironmentLogLevelListenerTest, GetLogListenerVerbose) std::string text = buffer.str(); // text will now contain "Bla\n" auto listener = EnvironmentLogLevelListener::GetLogListener(); - listener(Logger::Level::Verbose, "message"); EXPECT_NE(listener, nullptr); + + listener(Logger::Level::Verbose, "message"); EXPECT_NE(buffer.str().find("DEBUG : message"), std::string::npos); std::cerr.rdbuf(old); } @@ -161,8 +151,9 @@ TEST_F(EnvironmentLogLevelListenerTest, GetLogListenerError) std::string text = buffer.str(); // text will now contain "Bla\n" auto listener = EnvironmentLogLevelListener::GetLogListener(); - listener(Logger::Level::Error, "message"); EXPECT_NE(listener, nullptr); + + listener(Logger::Level::Error, "message"); EXPECT_NE(buffer.str().find("ERROR : message"), std::string::npos); std::cerr.rdbuf(old); } @@ -178,8 +169,9 @@ TEST_F(EnvironmentLogLevelListenerTest, GetLogListenerWarning) std::string text = buffer.str(); // text will now contain "Bla\n" auto listener = EnvironmentLogLevelListener::GetLogListener(); - listener(Logger::Level::Warning, "message"); EXPECT_NE(listener, nullptr); + + listener(Logger::Level::Warning, "message"); EXPECT_NE(buffer.str().find("WARN : message"), std::string::npos); std::cerr.rdbuf(old); } @@ -195,8 +187,9 @@ TEST_F(EnvironmentLogLevelListenerTest, GetLogListenerInformational) std::string text = buffer.str(); // text will now contain "Bla\n" auto listener = EnvironmentLogLevelListener::GetLogListener(); - listener(Logger::Level::Informational, "message"); EXPECT_NE(listener, nullptr); + + listener(Logger::Level::Informational, "message"); EXPECT_NE(buffer.str().find("INFO : message"), std::string::npos); std::cerr.rdbuf(old); } @@ -212,8 +205,9 @@ TEST_F(EnvironmentLogLevelListenerTest, GetLogListenerUnknown) std::string text = buffer.str(); // text will now contain "Bla\n" auto listener = EnvironmentLogLevelListener::GetLogListener(); - listener(static_cast(42), "message"); EXPECT_NE(listener, nullptr); + + listener(static_cast(42), "message"); EXPECT_NE(buffer.str().find("????? : message"), std::string::npos); std::cerr.rdbuf(old); } diff --git a/sdk/identity/azure-identity/CHANGELOG.md b/sdk/identity/azure-identity/CHANGELOG.md index 9dadb211c..24572e0ed 100644 --- a/sdk/identity/azure-identity/CHANGELOG.md +++ b/sdk/identity/azure-identity/CHANGELOG.md @@ -4,6 +4,8 @@ ### Features Added +- Enabled `EnvironmentCredential` and `ManagedIdentityCredential` to work on UWP. + ### Breaking Changes ### Bugs Fixed diff --git a/sdk/identity/azure-identity/CMakeLists.txt b/sdk/identity/azure-identity/CMakeLists.txt index e5fe3a159..36bef37aa 100644 --- a/sdk/identity/azure-identity/CMakeLists.txt +++ b/sdk/identity/azure-identity/CMakeLists.txt @@ -55,12 +55,10 @@ set( set( AZURE_IDENTITY_SOURCE - src/private/environment.hpp src/private/managed_identity_source.hpp src/private/package_version.hpp src/private/token_credential_impl.hpp src/client_secret_credential.cpp - src/environment.cpp src/environment_credential.cpp src/managed_identity_credential.cpp src/managed_identity_source.cpp diff --git a/sdk/identity/azure-identity/src/environment.cpp b/sdk/identity/azure-identity/src/environment.cpp deleted file mode 100644 index 29a83b70c..000000000 --- a/sdk/identity/azure-identity/src/environment.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// SPDX-License-Identifier: MIT - -#include "private/environment.hpp" - -#include "azure/core/platform.hpp" - -#include - -#if defined(AZ_PLATFORM_WINDOWS) -#if !defined(WIN32_LEAN_AND_MEAN) -#define WIN32_LEAN_AND_MEAN -#endif -#if !defined(NOMINMAX) -#define NOMINMAX -#endif - -#include -#endif - -using namespace Azure::Identity::_detail; - -std::string Environment::GetVariable(const char* name) -{ -#if !defined(WINAPI_PARTITION_DESKTOP) \ - || WINAPI_PARTITION_DESKTOP // See azure/core/platform.hpp for explanation. -#if defined(_MSC_VER) -#pragma warning(push) -// warning C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s -// instead. -#pragma warning(disable : 4996) -#endif - - if (auto envVar = std::getenv(name)) - { - return std::string(envVar); - } - -#if defined(_MSC_VER) -#pragma warning(pop) -#endif -#endif - - return std::string(); -} diff --git a/sdk/identity/azure-identity/src/environment_credential.cpp b/sdk/identity/azure-identity/src/environment_credential.cpp index e3bdb4029..f7f3660de 100644 --- a/sdk/identity/azure-identity/src/environment_credential.cpp +++ b/sdk/identity/azure-identity/src/environment_credential.cpp @@ -4,14 +4,14 @@ #include "azure/identity/environment_credential.hpp" #include "azure/identity/client_secret_credential.hpp" -#include "private/environment.hpp" +#include using namespace Azure::Identity; EnvironmentCredential::EnvironmentCredential( Azure::Core::Credentials::TokenCredentialOptions options) { - using _detail::Environment; + using Azure::Core::_internal::Environment; auto tenantId = Environment::GetVariable("AZURE_TENANT_ID"); auto clientId = Environment::GetVariable("AZURE_CLIENT_ID"); diff --git a/sdk/identity/azure-identity/src/managed_identity_source.cpp b/sdk/identity/azure-identity/src/managed_identity_source.cpp index 10a6219dd..ba0164b9d 100644 --- a/sdk/identity/azure-identity/src/managed_identity_source.cpp +++ b/sdk/identity/azure-identity/src/managed_identity_source.cpp @@ -3,7 +3,7 @@ #include "private/managed_identity_source.hpp" -#include "private/environment.hpp" +#include #include #include @@ -11,6 +11,7 @@ #include using namespace Azure::Identity::_detail; +using Azure::Core::_internal::Environment; Azure::Core::Url ManagedIdentitySource::ParseEndpointUrl( std::string const& url, diff --git a/sdk/identity/azure-identity/test/e2e/azure_identity_e2e_test.cpp b/sdk/identity/azure-identity/test/e2e/azure_identity_e2e_test.cpp index d893f6c75..8afb6760f 100644 --- a/sdk/identity/azure-identity/test/e2e/azure_identity_e2e_test.cpp +++ b/sdk/identity/azure-identity/test/e2e/azure_identity_e2e_test.cpp @@ -3,6 +3,8 @@ #include +#include + #include #include #include @@ -10,25 +12,12 @@ #include #include -namespace { -std::string GetEnv(std::string const& varName) -{ -#if defined(_MSC_VER) -#pragma warning(push) -// warning C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s -// instead. -#pragma warning(disable : 4996) -#endif - auto const value = std::getenv(varName.c_str()); -#if defined(_MSC_VER) -#pragma warning(pop) -#endif - return value ? std::string(value) : std::string(); -} +using Azure::Core::_internal::Environment; +namespace { std::string FormatEnvVarValue(std::string const& varName, bool isSecret) { - auto const value = GetEnv(varName); + auto const value = Environment::GetVariable(varName.c_str()); if (value.empty()) { return varName + " is not defined."; @@ -76,7 +65,7 @@ int main(int argc, char** argv) constexpr char const* resourceUrlEnvVarName = "AZURE_IDENTITY_TEST_VAULT_URL"; std::string const defaultResourceUrl = "https://management.azure.com/"; - auto resourceUrl = GetEnv(resourceUrlEnvVarName); + auto resourceUrl = Environment::GetVariable(resourceUrlEnvVarName); if (resourceUrl.empty()) { constexpr char const* simpleSwitch = "--simple"; @@ -103,7 +92,7 @@ int main(int argc, char** argv) options.Log.AllowedHttpHeaders.insert("Metadata"); ManagedIdentityCredential credential( - GetEnv("AZURE_IDENTITY_TEST_MANAGED_IDENTITY_CLIENT_ID"), options); + Environment::GetVariable("AZURE_IDENTITY_TEST_MANAGED_IDENTITY_CLIENT_ID"), options); auto const token = credential.GetToken({{resourceUrl}}, Context()); diff --git a/sdk/identity/azure-identity/test/ut/credential_test_helper.cpp b/sdk/identity/azure-identity/test/ut/credential_test_helper.cpp index 41c1b2808..8c0868535 100644 --- a/sdk/identity/azure-identity/test/ut/credential_test_helper.cpp +++ b/sdk/identity/azure-identity/test/ut/credential_test_helper.cpp @@ -3,24 +3,11 @@ #include "credential_test_helper.hpp" -#include "private/environment.hpp" - -#include +#include #include #include -#if defined(AZ_PLATFORM_WINDOWS) -#if !defined(WIN32_LEAN_AND_MEAN) -#define WIN32_LEAN_AND_MEAN -#endif -#if !defined(NOMINMAX) -#define NOMINMAX -#endif - -#include -#endif - namespace { class TestTransport final : public Azure::Core::Http::HttpTransport { public: @@ -45,39 +32,13 @@ public: using namespace Azure::Identity::Test::_detail; -bool const CredentialTestHelper::EnvironmentOverride::IsEnvironmentAvailable( -#if !defined(WINAPI_PARTITION_DESKTOP) \ - || WINAPI_PARTITION_DESKTOP // See azure/core/platform.hpp for explanation. - true -#else - false -#endif -); - void CredentialTestHelper::EnvironmentOverride::SetVariables( std::map const& vars) { -#if !defined(WINAPI_PARTITION_DESKTOP) \ - || WINAPI_PARTITION_DESKTOP // See azure/core/platform.hpp for explanation. for (auto var : vars) { - auto const& name = var.first; - auto const& value = var.second; - -#if defined(_MSC_VER) - static_cast(_putenv((name + "=" + value).c_str())); -#else - if (value.empty()) - { - static_cast(unsetenv(name.c_str())); - } - else - { - static_cast(setenv(name.c_str(), value.c_str(), 1)); - } -#endif + Azure::Core::_internal::Environment::SetVariable(var.first.c_str(), var.second.c_str()); } -#endif } CredentialTestHelper::EnvironmentOverride::EnvironmentOverride( @@ -85,7 +46,7 @@ CredentialTestHelper::EnvironmentOverride::EnvironmentOverride( { for (auto var : environment) { - m_originalEnv[var.first] = Identity::_detail::Environment::GetVariable(var.first.c_str()); + m_originalEnv[var.first] = Azure::Core::_internal::Environment::GetVariable(var.first.c_str()); } SetVariables(environment); diff --git a/sdk/identity/azure-identity/test/ut/credential_test_helper.hpp b/sdk/identity/azure-identity/test/ut/credential_test_helper.hpp index 3b901a234..bd35726c6 100644 --- a/sdk/identity/azure-identity/test/ut/credential_test_helper.hpp +++ b/sdk/identity/azure-identity/test/ut/credential_test_helper.hpp @@ -30,8 +30,6 @@ namespace Azure { namespace Identity { namespace Test { namespace _detail { static void SetVariables(std::map const& vars); public: - static bool const IsEnvironmentAvailable; - virtual ~EnvironmentOverride() { SetVariables(m_originalEnv); } explicit EnvironmentOverride(std::map const& environment); }; diff --git a/sdk/identity/azure-identity/test/ut/environment_credential_test.cpp b/sdk/identity/azure-identity/test/ut/environment_credential_test.cpp index d644a352c..f7c07e657 100644 --- a/sdk/identity/azure-identity/test/ut/environment_credential_test.cpp +++ b/sdk/identity/azure-identity/test/ut/environment_credential_test.cpp @@ -14,11 +14,6 @@ using Azure::Identity::Test::_detail::CredentialTestHelper; TEST(EnvironmentCredential, RegularClientSecretCredential) { - if (!CredentialTestHelper::EnvironmentOverride::IsEnvironmentAvailable) - { - return; - } - auto const actual = CredentialTestHelper::SimulateTokenRequest( [](auto transport) { TokenCredentialOptions options; @@ -74,11 +69,6 @@ TEST(EnvironmentCredential, RegularClientSecretCredential) TEST(EnvironmentCredential, AzureStackClientSecretCredential) { - if (!CredentialTestHelper::EnvironmentOverride::IsEnvironmentAvailable) - { - return; - } - auto const actual = CredentialTestHelper::SimulateTokenRequest( [](auto transport) { TokenCredentialOptions options; @@ -163,11 +153,6 @@ TEST(EnvironmentCredential, Unavailable) TEST(EnvironmentCredential, ClientSecretDefaultAuthority) { - if (!CredentialTestHelper::EnvironmentOverride::IsEnvironmentAvailable) - { - return; - } - auto const actual = CredentialTestHelper::SimulateTokenRequest( [](auto transport) { TokenCredentialOptions options; @@ -223,11 +208,6 @@ TEST(EnvironmentCredential, ClientSecretDefaultAuthority) TEST(EnvironmentCredential, ClientSecretNoTenantId) { - if (!CredentialTestHelper::EnvironmentOverride::IsEnvironmentAvailable) - { - return; - } - using Azure::Core::Credentials::AccessToken; using Azure::Core::Credentials::AuthenticationException; @@ -259,11 +239,6 @@ TEST(EnvironmentCredential, ClientSecretNoTenantId) TEST(EnvironmentCredential, ClientSecretNoClientId) { - if (!CredentialTestHelper::EnvironmentOverride::IsEnvironmentAvailable) - { - return; - } - using Azure::Core::Credentials::AccessToken; using Azure::Core::Credentials::AuthenticationException; @@ -295,11 +270,6 @@ TEST(EnvironmentCredential, ClientSecretNoClientId) TEST(EnvironmentCredential, ClientSecretNoClientSecret) { - if (!CredentialTestHelper::EnvironmentOverride::IsEnvironmentAvailable) - { - return; - } - using Azure::Core::Credentials::AccessToken; using Azure::Core::Credentials::AuthenticationException; diff --git a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/CMakeLists.txt index 1db39bdf8..e87b8563f 100644 --- a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/CMakeLists.txt @@ -11,9 +11,10 @@ add_executable ( certificate-basic-operations certificate_basic_operations.cpp ) + create_per_service_target_build(certificate-basic-operations keyvault) -target_link_libraries(certificate-basic-operations PRIVATE azure-security-keyvault-certificates azure-identity ) +target_link_libraries(certificate-basic-operations PRIVATE azure-security-keyvault-certificates azure-identity get-env-helper) # Add the sample to be run on CI. # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. diff --git a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/certificate_basic_operations.cpp b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/certificate_basic_operations.cpp index ecc373edd..9096db104 100644 --- a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/certificate_basic_operations.cpp +++ b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/certificate_basic_operations.cpp @@ -12,9 +12,8 @@ * - AZURE_CLIENT_SECRET: The client secret. * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif + +#include "get_env.hpp" #include #include diff --git a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/CMakeLists.txt index d6a904619..e8b03ce6b 100644 --- a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/CMakeLists.txt @@ -13,7 +13,7 @@ add_executable ( ) create_per_service_target_build(certificate-get-certificates keyvault) -target_link_libraries(certificate-get-certificates PRIVATE azure-security-keyvault-certificates azure-identity ) +target_link_libraries(certificate-get-certificates PRIVATE azure-security-keyvault-certificates azure-identity get-env-helper) # Add the sample to be run on CI. # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. diff --git a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/certificate_get_certificates.cpp b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/certificate_get_certificates.cpp index a5c408f9d..2b90deb53 100644 --- a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/certificate_get_certificates.cpp +++ b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/certificate_get_certificates.cpp @@ -15,9 +15,8 @@ * - AZURE_CLIENT_SECRET: The client secret. * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif + +#include "get_env.hpp" #include #include diff --git a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-import-certificate/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-import-certificate/CMakeLists.txt index 7865dd34f..fc976bf8b 100644 --- a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-import-certificate/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-import-certificate/CMakeLists.txt @@ -13,7 +13,7 @@ add_executable ( ) create_per_service_target_build(certificate-import-certificate keyvault) -target_link_libraries(certificate-import-certificate PRIVATE azure-security-keyvault-certificates azure-identity ) +target_link_libraries(certificate-import-certificate PRIVATE azure-security-keyvault-certificates azure-identity get-env-helper) # Add the sample to be run on CI. # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. diff --git a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-import-certificate/certificate_import_certificate.cpp b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-import-certificate/certificate_import_certificate.cpp index d87a4ba45..6eda42234 100644 --- a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-import-certificate/certificate_import_certificate.cpp +++ b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-import-certificate/certificate_import_certificate.cpp @@ -12,9 +12,8 @@ * - AZURE_CLIENT_SECRET: The client secret. * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif + +#include "get_env.hpp" #include #include diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample1-hello-world/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample1-hello-world/CMakeLists.txt index 73fda18df..378d58b2e 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample1-hello-world/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample1-hello-world/CMakeLists.txt @@ -18,4 +18,4 @@ create_per_service_target_build(sample1-hello-world keyvault) # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. file(APPEND ${CMAKE_BINARY_DIR}/keyvault-samples.txt "${CMAKE_CURRENT_BINARY_DIR}/sample1-hello-world\n") -target_link_libraries(sample1-hello-world PRIVATE azure-security-keyvault-keys azure-identity) +target_link_libraries(sample1-hello-world PRIVATE azure-security-keyvault-keys azure-identity get-env-helper) diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample1-hello-world/sample1_hello_world.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample1-hello-world/sample1_hello_world.cpp index 1c577ae2d..56e2331ce 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample1-hello-world/sample1_hello_world.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample1-hello-world/sample1_hello_world.cpp @@ -13,9 +13,7 @@ * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include @@ -30,10 +28,10 @@ using namespace Azure::Security::KeyVault::Keys; int main() { - auto const tenantId = std::string(std::getenv("AZURE_TENANT_ID")); - auto const clientId = std::string(std::getenv("AZURE_CLIENT_ID")); - auto const clientSecret = std::string(std::getenv("AZURE_CLIENT_SECRET")); - auto const keyVaultUrl = std::string(std::getenv("AZURE_KEYVAULT_URL")); + auto const tenantId = std::getenv("AZURE_TENANT_ID"); + auto const clientId = std::getenv("AZURE_CLIENT_ID"); + auto const clientSecret = std::getenv("AZURE_CLIENT_SECRET"); + auto const keyVaultUrl = std::getenv("AZURE_KEYVAULT_URL"); auto credential = std::make_shared(tenantId, clientId, clientSecret); diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample2-backup-and-restore/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample2-backup-and-restore/CMakeLists.txt index 349115e97..1de4a9136 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample2-backup-and-restore/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample2-backup-and-restore/CMakeLists.txt @@ -17,4 +17,4 @@ create_per_service_target_build(sample2-backup-and-restore keyvault) # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. file(APPEND ${CMAKE_BINARY_DIR}/keyvault-samples.txt "${CMAKE_CURRENT_BINARY_DIR}/sample2-backup-and-restore\n") -target_link_libraries(sample2-backup-and-restore PRIVATE azure-security-keyvault-keys azure-identity) +target_link_libraries(sample2-backup-and-restore PRIVATE azure-security-keyvault-keys azure-identity get-env-helper) diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample2-backup-and-restore/sample2_backup_and_restore.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample2-backup-and-restore/sample2_backup_and_restore.cpp index d8c65185c..94e42a725 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample2-backup-and-restore/sample2_backup_and_restore.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample2-backup-and-restore/sample2_backup_and_restore.cpp @@ -13,9 +13,7 @@ * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample3-get-keys/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample3-get-keys/CMakeLists.txt index ae6f05f96..f0b22a623 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample3-get-keys/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample3-get-keys/CMakeLists.txt @@ -17,4 +17,4 @@ create_per_service_target_build(sample3-get-keys keyvault) # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. file(APPEND ${CMAKE_BINARY_DIR}/keyvault-samples.txt "${CMAKE_CURRENT_BINARY_DIR}/sample3-get-keys\n") -target_link_libraries(sample3-get-keys PRIVATE azure-security-keyvault-keys azure-identity) +target_link_libraries(sample3-get-keys PRIVATE azure-security-keyvault-keys azure-identity get-env-helper) diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample3-get-keys/sample3_get_keys.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample3-get-keys/sample3_get_keys.cpp index b7836ad16..681f6c0b2 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample3-get-keys/sample3_get_keys.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample3-get-keys/sample3_get_keys.cpp @@ -14,9 +14,7 @@ * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample4-encrypt-decrypt/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample4-encrypt-decrypt/CMakeLists.txt index cde808371..73f5eed90 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample4-encrypt-decrypt/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample4-encrypt-decrypt/CMakeLists.txt @@ -17,4 +17,4 @@ create_per_service_target_build(sample4-encrypt-decrypt keyvault) # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. file(APPEND ${CMAKE_BINARY_DIR}/keyvault-samples.txt "${CMAKE_CURRENT_BINARY_DIR}/sample4-encrypt-decrypt\n") -target_link_libraries(sample4-encrypt-decrypt PRIVATE azure-security-keyvault-keys azure-identity) +target_link_libraries(sample4-encrypt-decrypt PRIVATE azure-security-keyvault-keys azure-identity get-env-helper) diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample4-encrypt-decrypt/sample4_encrypt_decrypt.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample4-encrypt-decrypt/sample4_encrypt_decrypt.cpp index 538c47545..d6e81bcd3 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample4-encrypt-decrypt/sample4_encrypt_decrypt.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample4-encrypt-decrypt/sample4_encrypt_decrypt.cpp @@ -13,9 +13,7 @@ * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample5-sign-verify/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample5-sign-verify/CMakeLists.txt index b17ce7076..8e6975a87 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample5-sign-verify/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample5-sign-verify/CMakeLists.txt @@ -17,4 +17,4 @@ create_per_service_target_build(sample5-sign-verify keyvault) # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. file(APPEND ${CMAKE_BINARY_DIR}/keyvault-samples.txt "${CMAKE_CURRENT_BINARY_DIR}/sample5-sign-verify\n") -target_link_libraries(sample5-sign-verify PRIVATE azure-security-keyvault-keys azure-identity) +target_link_libraries(sample5-sign-verify PRIVATE azure-security-keyvault-keys azure-identity get-env-helper) diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample5-sign-verify/sample5_sign_verify.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample5-sign-verify/sample5_sign_verify.cpp index c6c9acfef..8749233ac 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample5-sign-verify/sample5_sign_verify.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample5-sign-verify/sample5_sign_verify.cpp @@ -13,9 +13,7 @@ * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample6-wrap-unwrap/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample6-wrap-unwrap/CMakeLists.txt index f6fc48017..1920ff4fb 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample6-wrap-unwrap/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample6-wrap-unwrap/CMakeLists.txt @@ -17,4 +17,4 @@ create_per_service_target_build(sample6-wrap-unwrap keyvault) # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. file(APPEND ${CMAKE_BINARY_DIR}/keyvault-samples.txt "${CMAKE_CURRENT_BINARY_DIR}/sample6-wrap-unwrap\n") -target_link_libraries(sample6-wrap-unwrap PRIVATE azure-security-keyvault-keys azure-identity) +target_link_libraries(sample6-wrap-unwrap PRIVATE azure-security-keyvault-keys azure-identity get-env-helper) diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample6-wrap-unwrap/sample6_wrap_unwrap.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample6-wrap-unwrap/sample6_wrap_unwrap.cpp index de474327e..830f4e441 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample6-wrap-unwrap/sample6_wrap_unwrap.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample6-wrap-unwrap/sample6_wrap_unwrap.cpp @@ -13,9 +13,7 @@ * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_backup_test_live.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_backup_test_live.cpp index 5a4863a73..00a912ee8 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_backup_test_live.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_backup_test_live.cpp @@ -1,10 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif - #include "gtest/gtest.h" #include "key_client_base_test.hpp" diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_create_test_live.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_create_test_live.cpp index cbe931b7a..65ce57888 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_create_test_live.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_create_test_live.cpp @@ -1,10 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif - #include "gtest/gtest.h" #include "key_client_base_test.hpp" diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_delete_test_live.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_delete_test_live.cpp index f1e22f281..626987623 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_delete_test_live.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_delete_test_live.cpp @@ -1,10 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif - #include "gtest/gtest.h" #include "key_client_base_test.hpp" diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_get_test_live.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_get_test_live.cpp index 2471dd077..7259ff228 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_get_test_live.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_get_test_live.cpp @@ -1,10 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif - #include "gtest/gtest.h" #include "key_client_base_test.hpp" diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_import_test_live.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_import_test_live.cpp index f77d4dd9b..af640c578 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_import_test_live.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_import_test_live.cpp @@ -1,10 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif - #include "gtest/gtest.h" #include diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_update_test_live.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_update_test_live.cpp index 9896a842d..2bcb5445c 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_update_test_live.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_client_update_test_live.cpp @@ -1,10 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif - #include "gtest/gtest.h" #include "key_client_base_test.hpp" diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_cryptographic_client_test_live.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_cryptographic_client_test_live.cpp index e2604510c..1250216b2 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_cryptographic_client_test_live.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/ut/key_cryptographic_client_test_live.cpp @@ -1,10 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif - #include "gtest/gtest.h" #include diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_client_test.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_client_test.cpp index 29101d442..d1c37374c 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_client_test.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_client_test.cpp @@ -1,10 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif - #include "gtest/gtest.h" #include "mocked_transport_adapter_test.hpp" diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_transport_adapter_test.hpp b/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_transport_adapter_test.hpp index be9d7f452..97146f8ed 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_transport_adapter_test.hpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_transport_adapter_test.hpp @@ -92,7 +92,16 @@ namespace Azure { namespace Security { namespace KeyVault { namespace Keys { nam } result = new char[std::string(fakeKey).size() + keyType.size()]; + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4996) +#endif std::sprintf(result, fakeKey, keyType.c_str()); +#if defined(_MSC_VER) +#pragma warning(pop) +#endif + return result; } }; diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample1-basic-operations/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample1-basic-operations/CMakeLists.txt index 61bc77e0f..ccb4dd775 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample1-basic-operations/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample1-basic-operations/CMakeLists.txt @@ -13,7 +13,7 @@ add_executable ( ) create_per_service_target_build(sample1-basic-operations keyvault) -target_link_libraries(sample1-basic-operations PRIVATE azure-security-keyvault-secrets azure-identity) +target_link_libraries(sample1-basic-operations PRIVATE azure-security-keyvault-secrets azure-identity get-env-helper) # Add the sample to be run on CI. # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample1-basic-operations/sample1_basic_operations.cpp b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample1-basic-operations/sample1_basic_operations.cpp index 42a5481f9..c4c1cc44e 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample1-basic-operations/sample1_basic_operations.cpp +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample1-basic-operations/sample1_basic_operations.cpp @@ -12,9 +12,8 @@ * - AZURE_CLIENT_SECRET: The client secret. * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif + +#include "get_env.hpp" #include #include diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample2-backup-restore/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample2-backup-restore/CMakeLists.txt index c0434de98..30faa89aa 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample2-backup-restore/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample2-backup-restore/CMakeLists.txt @@ -14,7 +14,7 @@ add_executable ( create_per_service_target_build(sample2-backup-restore keyvault) -target_link_libraries(sample2-backup-restore PRIVATE azure-security-keyvault-secrets azure-identity) +target_link_libraries(sample2-backup-restore PRIVATE azure-security-keyvault-secrets azure-identity get-env-helper) # Add the sample to be run on CI. # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample2-backup-restore/sample2_backup_restore.cpp b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample2-backup-restore/sample2_backup_restore.cpp index dd2ed1fb0..340bb1040 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample2-backup-restore/sample2_backup_restore.cpp +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample2-backup-restore/sample2_backup_restore.cpp @@ -13,9 +13,7 @@ * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample3-delete-recover/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample3-delete-recover/CMakeLists.txt index 74932d491..115f159dd 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample3-delete-recover/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample3-delete-recover/CMakeLists.txt @@ -14,7 +14,7 @@ add_executable ( create_per_service_target_build(sample3-delete-recover keyvault) -target_link_libraries(sample3-delete-recover PRIVATE azure-security-keyvault-secrets azure-identity) +target_link_libraries(sample3-delete-recover PRIVATE azure-security-keyvault-secrets azure-identity get-env-helper) # Add the sample to be run on CI. # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample3-delete-recover/sample3_delete_recover.cpp b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample3-delete-recover/sample3_delete_recover.cpp index d5bb1db78..905d67456 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample3-delete-recover/sample3_delete_recover.cpp +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample3-delete-recover/sample3_delete_recover.cpp @@ -13,9 +13,7 @@ * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include @@ -23,6 +21,7 @@ #include #include #include + using namespace Azure::Security::KeyVault::Secrets; using namespace std::chrono_literals; void AssertSecretsEqual(KeyVaultSecret const& expected, KeyVaultSecret const& actual); diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample4-get-secrets-deleted/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample4-get-secrets-deleted/CMakeLists.txt index e1a42dedc..22442e8cb 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample4-get-secrets-deleted/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample4-get-secrets-deleted/CMakeLists.txt @@ -14,7 +14,7 @@ add_executable ( create_per_service_target_build(sample4-get-secrets-deleted keyvault) -target_link_libraries(sample4-get-secrets-deleted PRIVATE azure-security-keyvault-secrets azure-identity) +target_link_libraries(sample4-get-secrets-deleted PRIVATE azure-security-keyvault-secrets azure-identity get-env-helper) # Add the sample to be run on CI. # CI pipeline reads the {service}-samples.txt and runs the binaries listed there. diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample4-get-secrets-deleted/sample4_get_secrets_deleted.cpp b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample4-get-secrets-deleted/sample4_get_secrets_deleted.cpp index d4558de8a..a148760e4 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample4-get-secrets-deleted/sample4_get_secrets_deleted.cpp +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample4-get-secrets-deleted/sample4_get_secrets_deleted.cpp @@ -13,9 +13,7 @@ * */ -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/test-app/test_app.cpp b/sdk/keyvault/azure-security-keyvault-secrets/test/test-app/test_app.cpp index fa0c2f653..7bf83e9de 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/test-app/test_app.cpp +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/test-app/test_app.cpp @@ -1,23 +1,23 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif #include "azure/keyvault/keyvault_secrets.hpp" +#include #include using namespace Azure::Security::KeyVault::Secrets; int main() { - auto tenantId = std::getenv("AZURE_TENANT_ID"); - auto clientId = std::getenv("AZURE_CLIENT_ID"); - auto clientSecret = std::getenv("AZURE_CLIENT_SECRET"); + using Azure::Core::_internal::Environment; + + auto tenantId = Environment::GetVariable("AZURE_TENANT_ID"); + auto clientId = Environment::GetVariable("AZURE_CLIENT_ID"); + auto clientSecret = Environment::GetVariable("AZURE_CLIENT_SECRET"); auto credential = std::make_shared(tenantId, clientId, clientSecret); - SecretClient secretClient(std::getenv("AZURE_KEYVAULT_URL"), credential); + SecretClient secretClient(Environment::GetVariable("AZURE_KEYVAULT_URL"), credential); // just a response, with a secret // auto response = secretClient.GetSecret("testSecret"); // response.Value.Properties.ContentType = "content"; diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/ut/secret_client_base_test.hpp b/sdk/keyvault/azure-security-keyvault-secrets/test/ut/secret_client_base_test.hpp index ae3537887..95de32c49 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/ut/secret_client_base_test.hpp +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/ut/secret_client_base_test.hpp @@ -30,7 +30,7 @@ namespace Azure { namespace Security { namespace KeyVault { namespace Secrets { Azure::Security::KeyVault::Secrets::SecretClient const& GetClientForTest( std::string const& testName) { - //_putenv_s("AZURE_TEST_MODE", "PLAYBACK"); + // Azure::Core::_internal::Environment::SetVariable("AZURE_TEST_MODE", "PLAYBACK"); // keep this here to quickly switch between test modes InitializeClient(); // set the interceptor for the current test diff --git a/sdk/storage/azure-storage-blobs/samples/CMakeLists.txt b/sdk/storage/azure-storage-blobs/samples/CMakeLists.txt index a5b30df62..d47166dbe 100644 --- a/sdk/storage/azure-storage-blobs/samples/CMakeLists.txt +++ b/sdk/storage/azure-storage-blobs/samples/CMakeLists.txt @@ -4,17 +4,17 @@ cmake_minimum_required (VERSION 3.13) add_executable(blob-getting-started blob_getting_started.cpp) -target_link_libraries(blob-getting-started PRIVATE azure-storage-blobs) +target_link_libraries(blob-getting-started PRIVATE azure-storage-blobs get-env-helper) create_per_service_target_build(blob-getting-started storage) add_executable(blob-list-operation blob_list_operation.cpp) -target_link_libraries(blob-list-operation PRIVATE azure-storage-blobs) +target_link_libraries(blob-list-operation PRIVATE azure-storage-blobs get-env-helper) create_per_service_target_build(blob-list-operation storage) add_executable(blob-sas blob_sas.cpp) -target_link_libraries(blob-sas PRIVATE azure-storage-blobs) +target_link_libraries(blob-sas PRIVATE azure-storage-blobs get-env-helper) create_per_service_target_build(blob-sas storage) add_executable(transactional-checksum transactional_checksum.cpp) -target_link_libraries(transactional-checksum PRIVATE azure-storage-blobs) +target_link_libraries(transactional-checksum PRIVATE azure-storage-blobs get-env-helper) create_per_service_target_build(transactional-checksum storage) diff --git a/sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp b/sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp index 8c7c08042..04886e438 100644 --- a/sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp +++ b/sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp @@ -1,9 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/storage/azure-storage-blobs/samples/blob_list_operation.cpp b/sdk/storage/azure-storage-blobs/samples/blob_list_operation.cpp index 7bc24eed4..697615a68 100644 --- a/sdk/storage/azure-storage-blobs/samples/blob_list_operation.cpp +++ b/sdk/storage/azure-storage-blobs/samples/blob_list_operation.cpp @@ -1,9 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/storage/azure-storage-blobs/samples/blob_sas.cpp b/sdk/storage/azure-storage-blobs/samples/blob_sas.cpp index fa46b669d..10440602d 100644 --- a/sdk/storage/azure-storage-blobs/samples/blob_sas.cpp +++ b/sdk/storage/azure-storage-blobs/samples/blob_sas.cpp @@ -1,9 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/storage/azure-storage-blobs/samples/transactional_checksum.cpp b/sdk/storage/azure-storage-blobs/samples/transactional_checksum.cpp index 96e034235..6982dc63c 100644 --- a/sdk/storage/azure-storage-blobs/samples/transactional_checksum.cpp +++ b/sdk/storage/azure-storage-blobs/samples/transactional_checksum.cpp @@ -1,9 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/storage/azure-storage-blobs/test/fault-injector/azure_storage_blobs_fault_injector_test.cpp b/sdk/storage/azure-storage-blobs/test/fault-injector/azure_storage_blobs_fault_injector_test.cpp index 1efd3d8fc..51eed8b7a 100644 --- a/sdk/storage/azure-storage-blobs/test/fault-injector/azure_storage_blobs_fault_injector_test.cpp +++ b/sdk/storage/azure-storage-blobs/test/fault-injector/azure_storage_blobs_fault_injector_test.cpp @@ -10,10 +10,7 @@ * */ -#if defined(_MSC_VER) -// For using std::getenv() -#define _CRT_SECURE_NO_WARNINGS -#endif +#include #include @@ -88,7 +85,8 @@ int main() auto implementationClient = std::make_shared(winHttpOptions); #endif - std::string connectionString(std::getenv("STORAGE_CONNECTION_STRING")); + std::string connectionString( + Azure::Core::_internal::Environment::GetVariable("STORAGE_CONNECTION_STRING")); // Set the options for the FaultInjectorClient FaultInjectionClientOptions options; diff --git a/sdk/storage/azure-storage-blobs/test/perf/inc/azure/storage/blobs/test/blob_base_test.hpp b/sdk/storage/azure-storage-blobs/test/perf/inc/azure/storage/blobs/test/blob_base_test.hpp index 93437b4d3..0d1c04d07 100644 --- a/sdk/storage/azure-storage-blobs/test/perf/inc/azure/storage/blobs/test/blob_base_test.hpp +++ b/sdk/storage/azure-storage-blobs/test/perf/inc/azure/storage/blobs/test/blob_base_test.hpp @@ -9,6 +9,7 @@ #pragma once +#include #include #include @@ -62,7 +63,8 @@ namespace Azure { namespace Storage { namespace Blobs { namespace Test { void Setup() override { // Get connection string from env - const static std::string envConnectionString = std::getenv("STORAGE_CONNECTION_STRING"); + const static std::string envConnectionString + = Azure::Core::_internal::Environment::GetVariable("STORAGE_CONNECTION_STRING"); m_connectionString = envConnectionString; // Generate random container and blob names. diff --git a/sdk/storage/azure-storage-common/test/ut/test_base.cpp b/sdk/storage/azure-storage-common/test/ut/test_base.cpp index ce33598b5..5b1532794 100644 --- a/sdk/storage/azure-storage-common/test/ut/test_base.cpp +++ b/sdk/storage/azure-storage-common/test/ut/test_base.cpp @@ -1,10 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif - #include "test_base.hpp" #include @@ -272,7 +268,15 @@ namespace Azure { namespace Storage { namespace Test { std::vector StorageTest::ReadFile(const std::string& filename) { +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4996) +#endif FILE* fin = fopen(filename.data(), "rb"); +#if defined(_MSC_VER) +#pragma warning(pop) +#endif + if (!fin) { throw std::runtime_error("Failed to open file."); diff --git a/sdk/storage/azure-storage-files-datalake/samples/CMakeLists.txt b/sdk/storage/azure-storage-files-datalake/samples/CMakeLists.txt index 9bec84521..a5b3167a2 100644 --- a/sdk/storage/azure-storage-files-datalake/samples/CMakeLists.txt +++ b/sdk/storage/azure-storage-files-datalake/samples/CMakeLists.txt @@ -5,4 +5,4 @@ cmake_minimum_required (VERSION 3.13) add_executable(datalake-getting-started datalake_getting_started.cpp) create_per_service_target_build(datalake-getting-started storage) -target_link_libraries(datalake-getting-started PRIVATE azure-storage-files-datalake) +target_link_libraries(datalake-getting-started PRIVATE azure-storage-files-datalake get-env-helper) diff --git a/sdk/storage/azure-storage-files-datalake/samples/datalake_getting_started.cpp b/sdk/storage/azure-storage-files-datalake/samples/datalake_getting_started.cpp index 4cbbec7e2..2119245c6 100644 --- a/sdk/storage/azure-storage-files-datalake/samples/datalake_getting_started.cpp +++ b/sdk/storage/azure-storage-files-datalake/samples/datalake_getting_started.cpp @@ -1,9 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/storage/azure-storage-files-shares/samples/CMakeLists.txt b/sdk/storage/azure-storage-files-shares/samples/CMakeLists.txt index 289622000..a84321399 100644 --- a/sdk/storage/azure-storage-files-shares/samples/CMakeLists.txt +++ b/sdk/storage/azure-storage-files-shares/samples/CMakeLists.txt @@ -5,4 +5,4 @@ cmake_minimum_required (VERSION 3.13) add_executable(file-share-getting-started file_share_getting_started.cpp) create_per_service_target_build(file-share-getting-started storage) -target_link_libraries(file-share-getting-started PRIVATE azure-storage-files-shares) +target_link_libraries(file-share-getting-started PRIVATE azure-storage-files-shares get-env-helper) diff --git a/sdk/storage/azure-storage-files-shares/samples/file_share_getting_started.cpp b/sdk/storage/azure-storage-files-shares/samples/file_share_getting_started.cpp index 96a1bb10f..5a7cf241e 100644 --- a/sdk/storage/azure-storage-files-shares/samples/file_share_getting_started.cpp +++ b/sdk/storage/azure-storage-files-shares/samples/file_share_getting_started.cpp @@ -1,9 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/storage/azure-storage-queues/samples/CMakeLists.txt b/sdk/storage/azure-storage-queues/samples/CMakeLists.txt index 4e4b30c70..5a9fe72cd 100644 --- a/sdk/storage/azure-storage-queues/samples/CMakeLists.txt +++ b/sdk/storage/azure-storage-queues/samples/CMakeLists.txt @@ -4,9 +4,9 @@ cmake_minimum_required (VERSION 3.13) add_executable(queue-getting-started queue_getting_started.cpp) -target_link_libraries(queue-getting-started PRIVATE azure-storage-queues) +target_link_libraries(queue-getting-started PRIVATE azure-storage-queues get-env-helper) create_per_service_target_build(queue-getting-started storage) add_executable(queue-encode-message queue_encode_message.cpp) -target_link_libraries(queue-encode-message PRIVATE azure-storage-queues) +target_link_libraries(queue-encode-message PRIVATE azure-storage-queues get-env-helper) create_per_service_target_build(queue-encode-message storage) diff --git a/sdk/storage/azure-storage-queues/samples/queue_encode_message.cpp b/sdk/storage/azure-storage-queues/samples/queue_encode_message.cpp index 3d8f1682a..b4d8904a3 100644 --- a/sdk/storage/azure-storage-queues/samples/queue_encode_message.cpp +++ b/sdk/storage/azure-storage-queues/samples/queue_encode_message.cpp @@ -1,9 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include diff --git a/sdk/storage/azure-storage-queues/samples/queue_getting_started.cpp b/sdk/storage/azure-storage-queues/samples/queue_getting_started.cpp index 0c37e4d65..d621571a7 100644 --- a/sdk/storage/azure-storage-queues/samples/queue_getting_started.cpp +++ b/sdk/storage/azure-storage-queues/samples/queue_getting_started.cpp @@ -1,9 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif +#include "get_env.hpp" #include #include