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.
This commit is contained in:
Anton Kolesnyk 2022-01-29 00:22:33 -08:00 committed by GitHub
parent b265366d30
commit 2aae6be7a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
86 changed files with 380 additions and 481 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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.

View File

@ -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.

View File

@ -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 <cstdlib>
#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 <windows.h>
#if !defined(WINAPI_PARTITION_DESKTOP) || WINAPI_PARTITION_DESKTOP
// Win32
#include <cstdlib>
#else
// UWP
namespace std {
char* getenv(const char* name);
}
#endif
#endif

View File

@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include "get_env.hpp"
#include <cctype>
#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

View File

@ -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)

View File

@ -9,6 +9,8 @@
*
*/
#include "get_env.hpp"
#include <azure/storage/blobs.hpp>
#include <exception>

View File

@ -26,4 +26,5 @@ target_link_libraries(application
PRIVATE
Azure::azure-security-keyvault-keys
Azure::azure-identity
get-env-helper
)

View File

@ -13,9 +13,7 @@
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "get_env.hpp"
#include <azure/core.hpp>
#include <azure/identity.hpp>

View File

@ -28,4 +28,5 @@ add_executable (
target_link_libraries(application
PRIVATE
Azure::azure-storage-blobs
get-env-helper
)

View File

@ -10,6 +10,8 @@
*
*/
#include "get_env.hpp"
#include <iostream>
#include <azure/storage/blobs.hpp>

View File

@ -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

View File

@ -11,6 +11,7 @@
#include <azure/core/credentials/token_credential_options.hpp>
#include <azure/core/internal/client_options.hpp>
#include <azure/core/internal/diagnostics/log.hpp>
#include <azure/core/internal/environment.hpp>
#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

View File

@ -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 <azure/core/internal/strings.hpp>
#include <cstdlib>
#include <stdexcept>
#if defined(AZ_PLATFORM_WINDOWS)
#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif
#if !defined(NOMINMAX)
#define NOMINMAX
#endif
#include <windows.h>
#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);
}

View File

@ -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 <azure/core/internal/environment.hpp>
#include <azure/core/internal/json/json.hpp>
#include <azure/core/internal/strings.hpp>
#include "azure/core/test/interceptor_manager.hpp"
#include "private/environment.hpp"
#include <fstream>
#include <iostream>
@ -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()
{

View File

@ -1,20 +0,0 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#pragma once
#include <azure/core/test/network_models.hpp>
#include <string>
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

View File

@ -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 <azure/core/internal/strings.hpp>

View File

@ -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.

View File

@ -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

View File

@ -5,7 +5,7 @@
#include <string>
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

View File

@ -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 <vector>
#include <windows.h>
#else
#include <stdlib.h>
#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<std::string::value_type> 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<decltype(bufferVector)::size_type>(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<void>(SetEnvironmentVariableA(name, isEmptyValue ? nullptr : value));
#else
if (isEmptyValue)
{
static_cast<void>(unsetenv(name));
}
else
{
static_cast<void>(setenv(name, value, 1));
}
#endif
}
}

View File

@ -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 <windows.h>
#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 <iostream>
#include <string>
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

View File

@ -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 <windows.h>
#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<void(Logger::Level level, std::string const& message)>
EnvironmentLogLevelListener::GetLogListener()
{
return nullptr;
}
#endif
}}}} // namespace Azure::Core::Diagnostics::_detail

View File

@ -1,23 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include <gtest/gtest.h>
#include <private/environment_log_level_listener.hpp>
#include <azure/core/internal/environment.hpp>
#include <gtest/gtest.h>
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<void>(_putenv((environmentVariable + "=" + value).c_str()));
#else
static_cast<void>(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<Logger::Level>(42), "message");
EXPECT_NE(listener, nullptr);
listener(static_cast<Logger::Level>(42), "message");
EXPECT_NE(buffer.str().find("????? : message"), std::string::npos);
std::cerr.rdbuf(old);
}

View File

@ -4,6 +4,8 @@
### Features Added
- Enabled `EnvironmentCredential` and `ManagedIdentityCredential` to work on UWP.
### Breaking Changes
### Bugs Fixed

View File

@ -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

View File

@ -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 <cstdlib>
#if defined(AZ_PLATFORM_WINDOWS)
#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif
#if !defined(NOMINMAX)
#define NOMINMAX
#endif
#include <windows.h>
#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();
}

View File

@ -4,14 +4,14 @@
#include "azure/identity/environment_credential.hpp"
#include "azure/identity/client_secret_credential.hpp"
#include "private/environment.hpp"
#include <azure/core/internal/environment.hpp>
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");

View File

@ -3,7 +3,7 @@
#include "private/managed_identity_source.hpp"
#include "private/environment.hpp"
#include <azure/core/internal/environment.hpp>
#include <fstream>
#include <iterator>
@ -11,6 +11,7 @@
#include <utility>
using namespace Azure::Identity::_detail;
using Azure::Core::_internal::Environment;
Azure::Core::Url ManagedIdentitySource::ParseEndpointUrl(
std::string const& url,

View File

@ -3,6 +3,8 @@
#include <azure/identity/managed_identity_credential.hpp>
#include <azure/core/internal/environment.hpp>
#include <chrono>
#include <cstdlib>
#include <exception>
@ -10,25 +12,12 @@
#include <iostream>
#include <string>
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());

View File

@ -3,24 +3,11 @@
#include "credential_test_helper.hpp"
#include "private/environment.hpp"
#include <azure/core/platform.hpp>
#include <azure/core/internal/environment.hpp>
#include <stdlib.h>
#include <type_traits>
#if defined(AZ_PLATFORM_WINDOWS)
#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif
#if !defined(NOMINMAX)
#define NOMINMAX
#endif
#include <windows.h>
#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<std::string, std::string> 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<void>(_putenv((name + "=" + value).c_str()));
#else
if (value.empty())
{
static_cast<void>(unsetenv(name.c_str()));
}
else
{
static_cast<void>(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);

View File

@ -30,8 +30,6 @@ namespace Azure { namespace Identity { namespace Test { namespace _detail {
static void SetVariables(std::map<std::string, std::string> const& vars);
public:
static bool const IsEnvironmentAvailable;
virtual ~EnvironmentOverride() { SetVariables(m_originalEnv); }
explicit EnvironmentOverride(std::map<std::string, std::string> const& environment);
};

View File

@ -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;

View File

@ -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.

View File

@ -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 <azure/identity.hpp>
#include <azure/keyvault/keyvault_certificates.hpp>

View File

@ -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.

View File

@ -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 <azure/identity.hpp>
#include <azure/keyvault/keyvault_certificates.hpp>

View File

@ -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.

View File

@ -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 <azure/core.hpp>
#include <azure/identity.hpp>

View File

@ -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)

View File

@ -13,9 +13,7 @@
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "get_env.hpp"
#include <azure/core.hpp>
#include <azure/identity.hpp>
@ -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<Azure::Identity::ClientSecretCredential>(tenantId, clientId, clientSecret);

View File

@ -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)

View File

@ -13,9 +13,7 @@
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "get_env.hpp"
#include <azure/core.hpp>
#include <azure/identity.hpp>

View File

@ -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)

View File

@ -14,9 +14,7 @@
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "get_env.hpp"
#include <azure/core.hpp>
#include <azure/identity.hpp>

View File

@ -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)

View File

@ -13,9 +13,7 @@
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "get_env.hpp"
#include <azure/core.hpp>
#include <azure/identity.hpp>

View File

@ -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)

View File

@ -13,9 +13,7 @@
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "get_env.hpp"
#include <azure/core.hpp>
#include <azure/identity.hpp>

View File

@ -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)

View File

@ -13,9 +13,7 @@
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "get_env.hpp"
#include <azure/core.hpp>
#include <azure/identity.hpp>

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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 <azure/core/base64.hpp>

View File

@ -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"

View File

@ -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 <azure/core/internal/cryptography/sha_hash.hpp>

View File

@ -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"

View File

@ -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;
}
};

View File

@ -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.

View File

@ -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 <azure/identity.hpp>
#include <azure/keyvault/keyvault_secrets.hpp>

View File

@ -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.

View File

@ -13,9 +13,7 @@
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "get_env.hpp"
#include <azure/identity.hpp>
#include <azure/keyvault/keyvault_secrets.hpp>

View File

@ -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.

View File

@ -13,9 +13,7 @@
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "get_env.hpp"
#include <azure/identity.hpp>
#include <azure/keyvault/keyvault_secrets.hpp>
@ -23,6 +21,7 @@
#include <assert.h>
#include <chrono>
#include <iostream>
using namespace Azure::Security::KeyVault::Secrets;
using namespace std::chrono_literals;
void AssertSecretsEqual(KeyVaultSecret const& expected, KeyVaultSecret const& actual);

View File

@ -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.

View File

@ -13,9 +13,7 @@
*
*/
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "get_env.hpp"
#include <azure/identity.hpp>
#include <azure/keyvault/keyvault_secrets.hpp>

View File

@ -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 <azure/core/internal/environment.hpp>
#include <azure/identity.hpp>
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<Azure::Identity::ClientSecretCredential>(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";

View File

@ -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

View File

@ -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)

View File

@ -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 <cstdio>
#include <iostream>

View File

@ -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 <cstdio>
#include <iostream>

View File

@ -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 <cstdio>
#include <iostream>

View File

@ -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 <cstdio>
#include <iostream>

View File

@ -10,10 +10,7 @@
*
*/
#if defined(_MSC_VER)
// For using std::getenv()
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <azure/core/internal/environment.hpp>
#include <azure/storage/blobs.hpp>
@ -88,7 +85,8 @@ int main()
auto implementationClient = std::make_shared<Azure::Core::Http::WinHttpTransport>(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;

View File

@ -9,6 +9,7 @@
#pragma once
#include <azure/core/internal/environment.hpp>
#include <azure/core/uuid.hpp>
#include <azure/perf.hpp>
@ -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.

View File

@ -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 <algorithm>
@ -272,7 +268,15 @@ namespace Azure { namespace Storage { namespace Test {
std::vector<uint8_t> 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.");

View File

@ -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)

View File

@ -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 <cstdio>
#include <iostream>

View File

@ -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)

View File

@ -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 <cstdio>
#include <iostream>

View File

@ -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)

View File

@ -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 <cassert>
#include <cstdio>

View File

@ -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 <cstdio>
#include <iostream>