From 4eb33a00152d0bd3f95a4826933478151ddaed8f Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Mon, 11 Oct 2021 14:17:55 -0700 Subject: [PATCH] Update Key Vault packages to use `Shared-code` (#2905) * Use key vault shared code strategy for code-reuse between packages * use a class instead of function only * unwanted change * unwanted change * remove the install interface * typo --- .../CMakeLists.txt | 8 ++ .../src/certificate_client.cpp | 23 +----- .../CHANGELOG.md | 26 ------ .../CMakeLists.txt | 81 ------------------- .../azure-security-keyvault-common/NOTICE.txt | 32 -------- .../cgmanifest.json | 36 --------- .../keyvault/common/dll_import_export.hpp | 40 --------- .../src/private/package_version.hpp | 63 --------------- .../test/ut/CMakeLists.txt | 29 ------- .../azure_security_keyvault_common_test.cpp | 11 --- .../vcpkg/Config.cmake.in | 11 --- .../vcpkg/portfile.cmake | 21 ----- .../vcpkg/vcpkg.json | 28 ------- .../CMakeLists.txt | 8 ++ .../src/cryptography/cryptography_client.cpp | 24 +----- .../src/key_client.cpp | 25 +----- .../CMakeLists.txt | 8 ++ .../src/secret_client.cpp | 25 ++---- .../azure/keyvault/shared/keyvault_shared.hpp | 47 +++++++++++ 19 files changed, 85 insertions(+), 461 deletions(-) delete mode 100644 sdk/keyvault/azure-security-keyvault-common/CHANGELOG.md delete mode 100644 sdk/keyvault/azure-security-keyvault-common/CMakeLists.txt delete mode 100644 sdk/keyvault/azure-security-keyvault-common/NOTICE.txt delete mode 100644 sdk/keyvault/azure-security-keyvault-common/cgmanifest.json delete mode 100644 sdk/keyvault/azure-security-keyvault-common/inc/azure/keyvault/common/dll_import_export.hpp delete mode 100644 sdk/keyvault/azure-security-keyvault-common/src/private/package_version.hpp delete mode 100644 sdk/keyvault/azure-security-keyvault-common/test/ut/CMakeLists.txt delete mode 100644 sdk/keyvault/azure-security-keyvault-common/test/ut/azure_security_keyvault_common_test.cpp delete mode 100644 sdk/keyvault/azure-security-keyvault-common/vcpkg/Config.cmake.in delete mode 100644 sdk/keyvault/azure-security-keyvault-common/vcpkg/portfile.cmake delete mode 100644 sdk/keyvault/azure-security-keyvault-common/vcpkg/vcpkg.json create mode 100644 sdk/keyvault/azure-security-keyvault-shared/inc/azure/keyvault/shared/keyvault_shared.hpp diff --git a/sdk/keyvault/azure-security-keyvault-certificates/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-certificates/CMakeLists.txt index a3a4cb0f1..3f90d91c7 100644 --- a/sdk/keyvault/azure-security-keyvault-certificates/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-certificates/CMakeLists.txt @@ -54,6 +54,14 @@ target_include_directories( $ ) +# Include shared source code +# NOTE: Use shared-code only within .cpp files. DO NEVER consume the shared-code from header files. +target_include_directories( + azure-security-keyvault-certificates + PRIVATE + $ +) + target_link_libraries(azure-security-keyvault-certificates PUBLIC Azure::azure-core) # coverage. Has no effect if BUILD_CODE_COVERAGE is OFF diff --git a/sdk/keyvault/azure-security-keyvault-certificates/src/certificate_client.cpp b/sdk/keyvault/azure-security-keyvault-certificates/src/certificate_client.cpp index bd6fc2700..a86810a65 100644 --- a/sdk/keyvault/azure-security-keyvault-certificates/src/certificate_client.cpp +++ b/sdk/keyvault/azure-security-keyvault-certificates/src/certificate_client.cpp @@ -8,6 +8,8 @@ #include "private/keyvault_certificates_common_request.hpp" #include "private/package_version.hpp" +#include + #include #include #include @@ -28,25 +30,6 @@ using namespace Azure::Core::Http::_internal; using namespace Azure::Security::KeyVault::_detail; namespace { -// This is a Key-Vault only patch to calculate token scope/audience -std::string GetScopeFromUrl(Azure::Core::Url const& url) -{ - std::string calculatedScope(url.GetScheme() + "://"); - auto const& hostWithAccount = url.GetHost(); - auto hostNoAccountStart = std::find(hostWithAccount.begin(), hostWithAccount.end(), '.'); - - // Insert the calculated scope only when then host in the url contains at least a `.` - // Otherwise, only the default scope will be there. - // We don't want to throw/validate input but just leave the values go to azure to decide what to - // do. - if (hostNoAccountStart != hostWithAccount.end()) - { - calculatedScope.append(hostNoAccountStart + 1, hostWithAccount.end()); - calculatedScope.append("/.default"); - } - - return calculatedScope; -} } // namespace std::unique_ptr CertificateClient::SendRequest( @@ -76,7 +59,7 @@ CertificateClient::CertificateClient( std::vector> perRetrypolicies; { Azure::Core::Credentials::TokenRequestContext const tokenContext - = {{::GetScopeFromUrl(m_vaultUrl)}}; + = {{_internal::UrlScope::GetScopeFromUrl(m_vaultUrl)}}; perRetrypolicies.emplace_back( std::make_unique(credential, std::move(tokenContext))); diff --git a/sdk/keyvault/azure-security-keyvault-common/CHANGELOG.md b/sdk/keyvault/azure-security-keyvault-common/CHANGELOG.md deleted file mode 100644 index d461122e8..000000000 --- a/sdk/keyvault/azure-security-keyvault-common/CHANGELOG.md +++ /dev/null @@ -1,26 +0,0 @@ -# Release History - -## 4.0.0-beta.4 (Unreleased) - -### Breaking Changes - -- Removed `SHA256`, `SHA384`, and `SHA512` hashing classes by making them internal since the end user doesn't need them. -- Removed header `single_page.hpp`. - -## 4.0.0-beta.3 (2021-06-08) - -No breaking changes or new features added. Includes only implementation enhancements. - -## 4.0.0-beta.2 (2021-05-18) - -### Breaking Changes - -- Added `final` specifier to classes and structures that are are not expected to be inheritable at the moment. -- Removed `KeyVaultException`. -- Removed `ClientOptions`. - -## 4.0.0-beta.1 (2021-04-07) - -### New Features - -- KeyVaultException. diff --git a/sdk/keyvault/azure-security-keyvault-common/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-common/CMakeLists.txt deleted file mode 100644 index d35a27cee..000000000 --- a/sdk/keyvault/azure-security-keyvault-common/CMakeLists.txt +++ /dev/null @@ -1,81 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# SPDX-License-Identifier: MIT - -cmake_minimum_required (VERSION 3.13) -project(azure-security-keyvault-common LANGUAGES CXX) - -set(CMAKE_CXX_STANDARD 14) -set(CMAKE_CXX_STANDARD_REQUIRED True) -set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) - -list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake-modules") - -include(AzureVcpkg) -include(AzureVersion) -include(AzureCodeCoverage) -include(AzureTransportAdapters) -include(AzureDoxygen) -include(AzureGlobalCompileOptions) - -az_vcpkg_integrate() - -if(NOT AZ_ALL_LIBRARIES) - find_package(azure-core-cpp "1.2.0" CONFIG QUIET) - if(NOT azure-core-cpp_FOUND) - find_package(azure-core-cpp "1.2.0" REQUIRED) - endif() -endif() - -set( - AZURE_KEYVAULT_COMMON_SOURCE - src/private/package_version.hpp -) - -add_library( - azure-security-keyvault-common - ${AZURE_KEYVAULT_COMMON_SOURCE} -) -add_library(Azure::azure-security-keyvault-common ALIAS azure-security-keyvault-common) - -target_include_directories( - azure-security-keyvault-common - PUBLIC - $ - $ -) - -target_link_libraries(azure-security-keyvault-common PUBLIC Azure::azure-core) - -if(WIN32) - target_link_libraries(azure-security-keyvault-common PRIVATE bcrypt crypt32) -else() - find_package(OpenSSL REQUIRED) - target_link_libraries(azure-security-keyvault-common PRIVATE OpenSSL::SSL) -endif() - -if(MSVC) - # C28020 and C28204 are introduced by nlohmann/json - target_compile_options(azure-security-keyvault-common PUBLIC /wd28204 /wd28020) -endif() - -# coverage. Has no effect if BUILD_CODE_COVERAGE is OFF -create_code_coverage(keyvault azure-security-keyvault-common azure-security-keyvault-common-test) - -get_az_version("${CMAKE_CURRENT_SOURCE_DIR}/src/private/package_version.hpp") -#generate_documentation(azure-security-keyvault-common ${AZ_LIBRARY_VERSION}) - -if(BUILD_TESTING) - # tests - if (NOT AZ_ALL_LIBRARIES) - include(AddGoogleTest) - enable_testing () - endif() - - add_subdirectory(test/ut) -endif() - -az_vcpkg_export( - azure-security-keyvault-common - SECURITY_KEYVAULT_COMMON - "azure/keyvault/common/dll_import_export.hpp" - ) diff --git a/sdk/keyvault/azure-security-keyvault-common/NOTICE.txt b/sdk/keyvault/azure-security-keyvault-common/NOTICE.txt deleted file mode 100644 index 6e1bceb63..000000000 --- a/sdk/keyvault/azure-security-keyvault-common/NOTICE.txt +++ /dev/null @@ -1,32 +0,0 @@ -azure-sdk-for-cpp - -NOTICES AND INFORMATION -Do Not Translate or Localize - -This software incorporates material from third parties. Microsoft makes certain -open source code available at https://3rdpartysource.microsoft.com, or you may -send a check or money order for US $5.00, including the product name, the open -source component name, and version number, to: - -Source Code Compliance Team -Microsoft Corporation -One Microsoft Way -Redmond, WA 98052 -USA - -Notwithstanding any other terms, you may reverse engineer this software to the -extent required to debug changes to any libraries licensed under the GNU Lesser -General Public License. - ------------------------------------------------------------------------------- - -Azure SDK for C++ uses third-party libraries or other resources that may be -distributed under licenses different than the Azure SDK for C++ software. - -In the event that we accidentally failed to list a required notice, please -bring it to our attention. Post an issue or email us: - - azcppsdkhelp@microsoft.com - -The attached notices are provided for information only. - diff --git a/sdk/keyvault/azure-security-keyvault-common/cgmanifest.json b/sdk/keyvault/azure-security-keyvault-common/cgmanifest.json deleted file mode 100644 index 7f2901e70..000000000 --- a/sdk/keyvault/azure-security-keyvault-common/cgmanifest.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "Registrations": [ - { - "Component": { - "Type": "git", - "git": { - "RepositoryUrl": "https://github.com/google/googletest", - "CommitHash": "703bd9caab50b139428cea1aaff9974ebee5742e" - } - }, - "DevelopmentDependency": true - }, - { - "Component": { - "Type": "other", - "Other": { - "Name": "clang-format", - "Version": "9.0.0-2", - "DownloadUrl": "https://ubuntu.pkgs.org/18.04/ubuntu-updates-universe-amd64/clang-format-9_9-2~ubuntu18.04.2_amd64.deb.html" - } - }, - "DevelopmentDependency": true - }, - { - "Component": { - "Type": "other", - "Other": { - "Name": "doxygen", - "Version": "1.8.20", - "DownloadUrl": "http://doxygen.nl/files/doxygen-1.8.20-setup.exe" - } - }, - "DevelopmentDependency": true - } - ] -} diff --git a/sdk/keyvault/azure-security-keyvault-common/inc/azure/keyvault/common/dll_import_export.hpp b/sdk/keyvault/azure-security-keyvault-common/inc/azure/keyvault/common/dll_import_export.hpp deleted file mode 100644 index c9d08a7e6..000000000 --- a/sdk/keyvault/azure-security-keyvault-common/inc/azure/keyvault/common/dll_import_export.hpp +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// SPDX-License-Identifier: MIT - -/** - * @file - * @brief DLL export macro. - */ - -// For explanation, see the comment in azure/core/dll_import_export.hpp - -#pragma once - -/** - * @def AZ_SECURITY_KEYVAULT_COMMON_DLLEXPORT - * @brief Applies DLL export attribute, when applicable. - * @note See https://docs.microsoft.com/cpp/cpp/dllexport-dllimport?view=msvc-160. - */ - -#if defined(AZ_SECURITY_KEYVAULT_COMMON_DLL) \ - || (0 /*@AZ_SECURITY_KEYVAULT_COMMON_DLL_INSTALLED_AS_PACKAGE@*/) -#define AZ_SECURITY_KEYVAULT_COMMON_BUILT_AS_DLL 1 -#else -#define AZ_SECURITY_KEYVAULT_COMMON_BUILT_AS_DLL 0 -#endif - -#if AZ_SECURITY_KEYVAULT_COMMON_BUILT_AS_DLL -#if defined(_MSC_VER) -#if defined(AZ_SECURITY_KEYVAULT_COMMON_BEING_BUILT) -#define AZ_SECURITY_KEYVAULT_COMMON_DLLEXPORT __declspec(dllexport) -#else // !defined(AZ_SECURITY_KEYVAULT_COMMON_BEING_BUILT) -#define AZ_SECURITY_KEYVAULT_COMMON_DLLEXPORT __declspec(dllimport) -#endif // AZ_SECURITY_KEYVAULT_COMMON_BEING_BUILT -#else // !defined(_MSC_VER) -#define AZ_SECURITY_KEYVAULT_COMMON_DLLEXPORT -#endif // _MSC_VER -#else // !AZ_SECURITY_KEYVAULT_COMMON_BUILT_AS_DLL -#define AZ_SECURITY_KEYVAULT_COMMON_DLLEXPORT -#endif // AZ_SECURITY_KEYVAULT_COMMON_BUILT_AS_DLL - -#undef AZ_SECURITY_KEYVAULT_COMMON_BUILT_AS_DLL diff --git a/sdk/keyvault/azure-security-keyvault-common/src/private/package_version.hpp b/sdk/keyvault/azure-security-keyvault-common/src/private/package_version.hpp deleted file mode 100644 index 1ac4da35d..000000000 --- a/sdk/keyvault/azure-security-keyvault-common/src/private/package_version.hpp +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// SPDX-License-Identifier: MIT - -/** - * @file - * @brief Provides version information. - */ - -#pragma once - -#include - -#define AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MAJOR 4 -#define AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MINOR 0 -#define AZURE_SECURITY_KEYVAULT_COMMON_VERSION_PATCH 0 -#define AZURE_SECURITY_KEYVAULT_COMMON_VERSION_PRERELEASE "beta.4" - -#define AZURE_SECURITY_KEYVAULT_COMMON_VERSION_ITOA_HELPER(i) #i -#define AZURE_SECURITY_KEYVAULT_COMMON_VERSION_ITOA(i) \ - AZURE_SECURITY_KEYVAULT_COMMON_VERSION_ITOA_HELPER(i) - -namespace Azure { namespace Security { namespace KeyVault { namespace Common { namespace _detail { - /** - * @brief Provides version information. - * - */ - class PackageVersion final { - public: - /// Major numeric identifier. - static constexpr int32_t Major = AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MAJOR; - - /// Minor numeric identifier. - static constexpr int32_t Minor = AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MINOR; - - /// Patch numeric identifier. - static constexpr int32_t Patch = AZURE_SECURITY_KEYVAULT_COMMON_VERSION_PATCH; - - /// Indicates whether the SDK is in a pre-release state. - static constexpr bool IsPreRelease - = sizeof(AZURE_SECURITY_KEYVAULT_COMMON_VERSION_PRERELEASE) != sizeof(""); - - /** - * @brief The version in string format used for telemetry following the `semver.org` standard - * (https://semver.org). - */ - static constexpr const char* ToString() - { - return IsPreRelease - ? AZURE_SECURITY_KEYVAULT_COMMON_VERSION_ITOA(AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MAJOR) "." AZURE_SECURITY_KEYVAULT_COMMON_VERSION_ITOA( - AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MINOR) "." AZURE_SECURITY_KEYVAULT_COMMON_VERSION_ITOA(AZURE_SECURITY_KEYVAULT_COMMON_VERSION_PATCH) "-" AZURE_SECURITY_KEYVAULT_COMMON_VERSION_PRERELEASE - : AZURE_SECURITY_KEYVAULT_COMMON_VERSION_ITOA(AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MAJOR) "." AZURE_SECURITY_KEYVAULT_COMMON_VERSION_ITOA( - AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MINOR) "." AZURE_SECURITY_KEYVAULT_COMMON_VERSION_ITOA(AZURE_SECURITY_KEYVAULT_COMMON_VERSION_PATCH); - } - }; -}}}}} // namespace Azure::Security::KeyVault::Common::_detail - -#undef AZURE_SECURITY_KEYVAULT_COMMON_VERSION_ITOA_HELPER -#undef AZURE_SECURITY_KEYVAULT_COMMON_VERSION_ITOA - -#undef AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MAJOR -#undef AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MINOR -#undef AZURE_SECURITY_KEYVAULT_COMMON_VERSION_PATCH -#undef AZURE_SECURITY_KEYVAULT_COMMON_VERSION_PRERELEASE diff --git a/sdk/keyvault/azure-security-keyvault-common/test/ut/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-common/test/ut/CMakeLists.txt deleted file mode 100644 index 0efaa0175..000000000 --- a/sdk/keyvault/azure-security-keyvault-common/test/ut/CMakeLists.txt +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# SPDX-License-Identifier: MIT - -cmake_minimum_required (VERSION 3.13) - -project (azure-security-keyvault-common-test LANGUAGES CXX) -set(CMAKE_CXX_STANDARD 14) -set(CMAKE_CXX_STANDARD_REQUIRED True) - -include(GoogleTest) - -add_executable ( - azure-security-keyvault-common-test - azure_security_keyvault_common_test.cpp -) - -if (MSVC) - target_compile_options(azure-security-keyvault-common-test PUBLIC /wd6326 /wd26495 /wd26812) -endif() - -target_link_libraries(azure-security-keyvault-common-test PUBLIC gtest gmock) - -# gtest_discover_tests will scan the test from azure-core-test and call add_test -# for each test to ctest. This enables `ctest -r` to run specific tests directly. -gtest_discover_tests(azure-security-keyvault-common-test - TEST_PREFIX azure-security-keyvault-common-unittest. - NO_PRETTY_TYPES - NO_PRETTY_VALUES -) diff --git a/sdk/keyvault/azure-security-keyvault-common/test/ut/azure_security_keyvault_common_test.cpp b/sdk/keyvault/azure-security-keyvault-common/test/ut/azure_security_keyvault_common_test.cpp deleted file mode 100644 index 3d0b851aa..000000000 --- a/sdk/keyvault/azure-security-keyvault-common/test/ut/azure_security_keyvault_common_test.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// SPDX-License-Identifier: MIT - -#include "gtest/gtest.h" - -int main(int argc, char** argv) -{ - testing::InitGoogleTest(&argc, argv); - auto r = RUN_ALL_TESTS(); - return r; -} diff --git a/sdk/keyvault/azure-security-keyvault-common/vcpkg/Config.cmake.in b/sdk/keyvault/azure-security-keyvault-common/vcpkg/Config.cmake.in deleted file mode 100644 index e656dbdb0..000000000 --- a/sdk/keyvault/azure-security-keyvault-common/vcpkg/Config.cmake.in +++ /dev/null @@ -1,11 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# SPDX-License-Identifier: MIT - -@PACKAGE_INIT@ - -include(CMakeFindDependencyMacro) -find_dependency(azure-core-cpp "1.2.0") - -include("${CMAKE_CURRENT_LIST_DIR}/azure-security-keyvault-common-cppTargets.cmake") - -check_required_components("azure-security-keyvault-common-cpp") diff --git a/sdk/keyvault/azure-security-keyvault-common/vcpkg/portfile.cmake b/sdk/keyvault/azure-security-keyvault-common/vcpkg/portfile.cmake deleted file mode 100644 index 7114b95f2..000000000 --- a/sdk/keyvault/azure-security-keyvault-common/vcpkg/portfile.cmake +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# SPDX-License-Identifier: MIT - -vcpkg_from_github( - OUT_SOURCE_PATH SOURCE_PATH - REPO Azure/azure-sdk-for-cpp - REF azure-security-keyvault-common_@AZ_LIBRARY_VERSION@ - SHA512 0 -) - -vcpkg_cmake_configure( - SOURCE_PATH ${SOURCE_PATH}/sdk/keyvault/azure-security-keyvault-common/ - OPTIONS - -DWARNINGS_AS_ERRORS=OFF -) - -vcpkg_cmake_install() -file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") -vcpkg_cmake_config_fixup() -file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share") -vcpkg_copy_pdbs() diff --git a/sdk/keyvault/azure-security-keyvault-common/vcpkg/vcpkg.json b/sdk/keyvault/azure-security-keyvault-common/vcpkg/vcpkg.json deleted file mode 100644 index 9a70f4f8f..000000000 --- a/sdk/keyvault/azure-security-keyvault-common/vcpkg/vcpkg.json +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# SPDX-License-Identifier: MIT - -{ - "name": "azure-security-keyvault-common-cpp", - "version-semver": "@AZ_LIBRARY_VERSION@", - "description": [ - "Microsoft Azure Common Key Vault SDK for C++", - "This library provides common Azure Key Vault related abstractions for Azure SDK." - ], - "homepage": "https://github.com/Azure/azure-sdk-for-cpp/tree/main/sdk/keyvault/azure-security-keyvault-common", - "license": "MIT", - "dependencies": [ - { - "name": "azure-core-cpp", - "default-features": false, - "version>=": "1.2.0" - }, - { - "name": "vcpkg-cmake", - "host": true - }, - { - "name": "vcpkg-cmake-config", - "host": true - } - ] -} diff --git a/sdk/keyvault/azure-security-keyvault-keys/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-keys/CMakeLists.txt index e31b7ba96..ccfac1c9c 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-keys/CMakeLists.txt @@ -95,6 +95,14 @@ target_include_directories( $ ) +# Include shared source code +# NOTE: Use shared-code only within .cpp files. DO NEVER consume the shared-code from header files. +target_include_directories( + azure-security-keyvault-keys + PRIVATE + $ +) + target_link_libraries(azure-security-keyvault-keys PUBLIC Azure::azure-core) # coverage. Has no effect if BUILD_CODE_COVERAGE is OFF diff --git a/sdk/keyvault/azure-security-keyvault-keys/src/cryptography/cryptography_client.cpp b/sdk/keyvault/azure-security-keyvault-keys/src/cryptography/cryptography_client.cpp index 317b454b2..d9d157258 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/src/cryptography/cryptography_client.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/src/cryptography/cryptography_client.cpp @@ -6,6 +6,8 @@ #include #include +#include + #include "azure/keyvault/keys/cryptography/cryptography_client.hpp" #include "azure/keyvault/keys/key_client_models.hpp" @@ -59,26 +61,6 @@ inline std::vector CreateDigest( return hashAlgorithm->Final(data.data(), data.size()); } -// This is a Key-Vault only patch to calculate token scope/audience -std::string GetScopeFromUrl(Azure::Core::Url const& url) -{ - std::string calculatedScope(url.GetScheme() + "://"); - auto const& hostWithAccount = url.GetHost(); - auto hostNoAccountStart = std::find(hostWithAccount.begin(), hostWithAccount.end(), '.'); - - // Insert the calculated scope only when then host in the url contains at least a `.` - // Otherwise, only the default scope will be there. - // We don't want to throw/validate input but just leave the values go to azure to decide what to - // do. - if (hostNoAccountStart != hostWithAccount.end()) - { - calculatedScope.append(hostNoAccountStart + 1, hostWithAccount.end()); - calculatedScope.append("/.default"); - } - - return calculatedScope; -} - } // namespace Request CryptographyClient::CreateRequest( @@ -120,7 +102,7 @@ CryptographyClient::CryptographyClient( std::vector> perRetrypolicies; { Azure::Core::Credentials::TokenRequestContext const tokenContext - = {{::GetScopeFromUrl(m_keyId)}}; + = {{_internal::UrlScope::GetScopeFromUrl(m_keyId)}}; perRetrypolicies.emplace_back( std::make_unique(credential, tokenContext)); diff --git a/sdk/keyvault/azure-security-keyvault-keys/src/key_client.cpp b/sdk/keyvault/azure-security-keyvault-keys/src/key_client.cpp index 5ec77fc8a..6f3f80ad1 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/src/key_client.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/src/key_client.cpp @@ -6,6 +6,8 @@ #include #include +#include + #include "azure/keyvault/keys/key_client.hpp" #include "private/cryptography_internal_access.hpp" #include "private/key_backup.hpp" @@ -30,27 +32,6 @@ using namespace Azure::Core::Http::_internal; namespace { constexpr static const char KeyVaultServicePackageName[] = "keyvault-keys"; constexpr static const char CreateValue[] = "create"; - -// This is a Key-Vault only patch to calculate token scope/audience -std::string GetScopeFromUrl(Azure::Core::Url const& url) -{ - std::string calculatedScope(url.GetScheme() + "://"); - auto const& hostWithAccount = url.GetHost(); - auto hostNoAccountStart = std::find(hostWithAccount.begin(), hostWithAccount.end(), '.'); - - // Insert the calculated scope only when then host in the url contains at least a `.` - // Otherwise, only the default scope will be there. - // We don't want to throw/validate input but just leave the values go to azure to decide what to - // do. - if (hostNoAccountStart != hostWithAccount.end()) - { - calculatedScope.append(hostNoAccountStart + 1, hostWithAccount.end()); - calculatedScope.append("/.default"); - } - - return calculatedScope; -} - } // namespace std::unique_ptr KeyClient::SendRequest( @@ -93,7 +74,7 @@ KeyClient::KeyClient( std::vector> perRetrypolicies; { Azure::Core::Credentials::TokenRequestContext const tokenContext - = {{::GetScopeFromUrl(m_vaultUrl)}}; + = {{_internal::UrlScope::GetScopeFromUrl(m_vaultUrl)}}; perRetrypolicies.emplace_back( std::make_unique(credential, std::move(tokenContext))); diff --git a/sdk/keyvault/azure-security-keyvault-secrets/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-secrets/CMakeLists.txt index cedd0ccb8..607d7e12b 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-secrets/CMakeLists.txt @@ -64,6 +64,14 @@ target_include_directories( $ ) +# Include shared source code +# NOTE: Use shared-code only within .cpp files. DO NEVER consume the shared-code from header files. +target_include_directories( + azure-security-keyvault-secrets + PRIVATE + $ +) + target_link_libraries(azure-security-keyvault-secrets PUBLIC Azure::azure-core) # coverage. Has no effect if BUILD_CODE_COVERAGE is OFF diff --git a/sdk/keyvault/azure-security-keyvault-secrets/src/secret_client.cpp b/sdk/keyvault/azure-security-keyvault-secrets/src/secret_client.cpp index 54c0da5c2..fea853113 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/src/secret_client.cpp +++ b/sdk/keyvault/azure-security-keyvault-secrets/src/secret_client.cpp @@ -12,6 +12,9 @@ #include "private/package_version.hpp" #include "private/secret_constants.hpp" #include "private/secret_serializers.hpp" + +#include + #include #include #include @@ -53,25 +56,6 @@ static inline RequestWithContinuationToken BuildRequestFromContinuationToken( return request; } -// This is a Key-Vault only patch to calculate token scope/audience -std::string GetScopeFromUrl(Azure::Core::Url const& url) -{ - std::string calculatedScope(url.GetScheme() + "://"); - auto const& hostWithAccount = url.GetHost(); - auto hostNoAccountStart = std::find(hostWithAccount.begin(), hostWithAccount.end(), '.'); - - // Insert the calculated scope only when then host in the url contains at least a `.` - // Otherwise, only the default scope will be there. - // We don't want to throw/validate input but just leave the values go to azure to decide what to - // do. - if (hostNoAccountStart != hostWithAccount.end()) - { - calculatedScope.append(hostNoAccountStart + 1, hostWithAccount.end()); - calculatedScope.append("/.default"); - } - - return calculatedScope; -} } // namespace const ServiceVersion ServiceVersion::V7_2("7.2"); @@ -86,7 +70,8 @@ SecretClient::SecretClient( std::vector> perRetrypolicies; { - Azure::Core::Credentials::TokenRequestContext const tokenContext = {{::GetScopeFromUrl(url)}}; + Azure::Core::Credentials::TokenRequestContext const tokenContext + = {{_internal::UrlScope::GetScopeFromUrl(url)}}; perRetrypolicies.emplace_back( std::make_unique(credential, tokenContext)); diff --git a/sdk/keyvault/azure-security-keyvault-shared/inc/azure/keyvault/shared/keyvault_shared.hpp b/sdk/keyvault/azure-security-keyvault-shared/inc/azure/keyvault/shared/keyvault_shared.hpp new file mode 100644 index 000000000..83dd8458d --- /dev/null +++ b/sdk/keyvault/azure-security-keyvault-shared/inc/azure/keyvault/shared/keyvault_shared.hpp @@ -0,0 +1,47 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: MIT + +/** + * @file + * @brief Shared code between Key Vault services. + * + */ + +#pragma once + +#include + +#include + +namespace Azure { namespace Security { namespace KeyVault { namespace _internal { + + /** + * @brief Provides functionality to get scope information from a URL. + * + */ + class UrlScope { + UrlScope() = delete; + + public: + // This is a Key-Vault only patch to calculate token scope/audience + static std::string GetScopeFromUrl(Azure::Core::Url const& url) + { + std::string calculatedScope(url.GetScheme() + "://"); + auto const& hostWithAccount = url.GetHost(); + auto hostNoAccountStart = std::find(hostWithAccount.begin(), hostWithAccount.end(), '.'); + + // Insert the calculated scope only when the host in the url contains at least a `.` + // Otherwise, only the default scope will be there. + // We don't want to throw/validate input but just leave the values go to azure to decide what + // to do. + if (hostNoAccountStart != hostWithAccount.end()) + { + calculatedScope.append(hostNoAccountStart + 1, hostWithAccount.end()); + calculatedScope.append("/.default"); + } + + return calculatedScope; + } + }; + +}}}} // namespace Azure::Security::KeyVault::_internal