Vcpkg sample (#2161)

* extra test

* sample

* typo

* Adding storage sample
This commit is contained in:
Victor Vazquez 2021-05-07 14:22:42 -07:00 committed by GitHub
parent fd9c3e5ebb
commit 1f5bfb6984
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 448 additions and 0 deletions

View File

@ -0,0 +1,7 @@
# base image installs keyvault beta1 from vcpkg only
FROM vhvb1989/keyvault:beta1
RUN cd vcpkg \
&& sudo ./vcpkg install azure-identity-cpp
ENV VCPKG_DEFAULT_TRIPLET=x64-linux

View File

@ -0,0 +1,20 @@
{
"name": "Ubuntu-21.04",
"dockerFile": "Dockerfile",
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
"extensions": [
"ms-vscode.cpptools-themes",
"bbenoist.doxygen",
"streetsidesoftware.code-spell-checker",
"ms-vscode.cpptools",
"xaver.clang-format",
"twxs.cmake",
"ms-vscode.cmake-tools",
"eamodio.gitlens",
"davidschuldenfrei.gtest-adapter"
],
// Do not run as root. See https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "azure-sdk-for-cpp"
}

View File

@ -0,0 +1,33 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: MIT
# VCPKG integration.
if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
CACHE STRING "")
endif()
if(DEFINED ENV{VCPKG_DEFAULT_TRIPLET} AND NOT DEFINED VCPKG_TARGET_TRIPLET)
set(VCPKG_TARGET_TRIPLET "$ENV{VCPKG_DEFAULT_TRIPLET}" CACHE STRING "")
endif()
# Project set up
cmake_minimum_required(VERSION 3.13)
project(Application-using-keyvault-from-vcpkg LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
find_package(CURL REQUIRED)
find_package(azure-security-keyvault-keys-cpp CONFIG REQUIRED)
find_package(azure-identity-cpp CONFIG REQUIRED)
add_executable (
application
src/main
)
# Link to Azure SDK
target_link_libraries(application
PRIVATE
Azure::azure-security-keyvault-keys
Azure::azure-identity
)

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,67 @@
# Integrating the Azure SDK for C++ into your application using VCPKG
This application shows how to integrate the Azure SDK for C++ in your application. It uses VCPKG to adquire and build the Azure SDK for C++ client libraries. Your CMake project needs to link the libraries from VCPKG by setting the toolchain file to VCPKG (shown below).
## Pre-requisites
There are two options to set up the development environment:
### Manual installation
Install the [Azure SDK for C++ dependencies](https://github.com/Azure/azure-sdk-for-cpp/blob/master/CONTRIBUTING.md#third-party-dependencies).
- CMake project (min version 3.13).
- C++ version 14 or greater.
### Container
The sample provides a .devcontainer folder which can be used by VSCode to build and run a docker container with the required C++ build tools and with VCPKG installed.
This method requires VSCode + [Remote Container](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) to be installed in the system. Also make sure to have Docker installed and running. This method works for any operating system where Docker and VSCode is supported like Windows, Linux and MacOS. The development environment will be Linux debian 10.
- Open vcpkg folder in VSCode.
- VSCode will detect the `devcontainer` configuration and ask you if you would like to re-open the folder in a container. Click Yes.
- If VSCode did not ask, you can press F1 and type `Reopen in container` option.
Once VSCode builds and run the container, open the terminal and continue to build step.
> Note: The container is set up to automatically link VCPKG to CMake projects by setting env variables that the CMake sample project will use to set the toolchain.
## Build
### Linux terminal
```bash
#
# Building the application.
# Instructions from application root directory.
#
# Create build directory just the first time.
mkdir build
cd build
# Generate and build
# This code assumes that the SDK dependencies were installed with VCPKG
# When using docker provided container, the TOOLCHAIN option is not required (cmake ..).
cmake -DCMAKE_TOOLCHAIN_FILE=path/to/vcpkg/scripts/buildsystems/vcpkg.cmake ..
cmake -build .
```
### Windows VS
If you are using Visual Studio, the toolchain to link VCPKG is set with `CMakeSettings.json`. Upate this file and set the VCPKG toolchain file for VCPKG (VCPKG_ROO\scripts\buildsystems\vcpkg.cmake). After setting the toolchain, VS can generate and build the sample. Use VS to open the sample folder only.
## Run application
Review source code header for `environment variables` that must be set up before running the app.
```bash
#
# Running the Application
# Instructions from inside the build directory.
#
# Run binary (.exe on Windows)
./application
```

View File

@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
/**
* @brief This sample provides the code implementation to use the Key Vault SDK client for C++
* to create, get, update, delete and purge a key.
*
* @remark The following environment variables must be set before running the sample.
* - AZURE_KEYVAULT_URL: To the KeyVault account url.
* - AZURE_TENANT_ID: Tenant id for the Azure account.
* - AZURE_CLIENT_ID: The client id to authenticate the request.
* - AZURE_CLIENT_SECRET: The secret id from the client id.
*
*/
#include <azure/core.hpp>
#include <azure/identity.hpp>
#include <azure/keyvault/key_vault.hpp>
#include <chrono>
#include <iostream>
#include <memory>
#include <thread>
using namespace Azure::Security::KeyVault::Keys;
int main()
{
auto tenantId = std::getenv("AZURE_TENANT_ID");
auto clientId = std::getenv("AZURE_CLIENT_ID");
auto clientSecret = std::getenv("AZURE_CLIENT_SECRET");
auto credential
= std::make_shared<Azure::Identity::ClientSecretCredential>(tenantId, clientId, clientSecret);
KeyClient keyClient(std::getenv("AZURE_KEYVAULT_URL"), credential);
std::string rsaKeyName("CloudRsaKey" + Azure::Core::Uuid::CreateUuid().ToString());
try
{
auto rsaKey = CreateRsaKeyOptions(rsaKeyName);
rsaKey.KeySize = 2048;
rsaKey.ExpiresOn = std::chrono::system_clock::now() + std::chrono::hours(24 * 365);
keyClient.CreateRsaKey(rsaKey);
KeyVaultKey cloudRsaKey = keyClient.GetKey(rsaKeyName).Value;
std::cout << "Key is returned with name " << cloudRsaKey.Name() << " and type "
<< KeyType::KeyTypeToString(cloudRsaKey.GetKeyType()) << std::endl;
cloudRsaKey.Properties.ExpiresOn
= cloudRsaKey.Properties.ExpiresOn.Value() + std::chrono::hours(24 * 365);
KeyVaultKey updatedKey = keyClient.UpdateKeyProperties(cloudRsaKey.Properties).Value;
std::cout << "Key's updated expiry time is " << updatedKey.Properties.ExpiresOn->ToString()
<< std::endl;
CreateRsaKeyOptions newRsaKey(rsaKeyName);
newRsaKey.KeySize = 4096;
newRsaKey.ExpiresOn = std::chrono::system_clock::now() + std::chrono::hours(24 * 365);
keyClient.CreateRsaKey(newRsaKey);
DeleteKeyOperation operation = keyClient.StartDeleteKey(rsaKeyName);
// You only need to wait for completion if you want to purge or recover the key.
operation.PollUntilDone(std::chrono::milliseconds(2000));
keyClient.PurgeDeletedKey(rsaKeyName);
}
catch (Azure::Core::Credentials::AuthenticationException const& e)
{
std::cout << "Authentication Exception happened:" << std::endl << e.what() << std::endl;
return 1;
}
catch (Azure::Core::RequestFailedException const& e)
{
std::cout << "KeyVault Client Exception happened:" << std::endl << e.Message << std::endl;
return 1;
}
return 0;
}

View File

@ -0,0 +1,14 @@
# base image installs dev env for storage v12 beta10
FROM vhvb1989/storage12:preview10
# Get vcpkg and install storage blobs
RUN sudo git clone https://github.com/microsoft/vcpkg.git \
&& cd vcpkg \
# VCPKG commit version for key vault keys 1.0.0-beta.1 - Storage beta 10 - Identity beta 5 - Core beta 8
&& sudo git checkout 414bec05f2a97cfc0ddb8e22fd4635dfe2a20ab8 \
&& sudo ./bootstrap-vcpkg.sh
ENV VCPKG_ROOT=/vcpkg
RUN cd vcpkg \
&& sudo ./vcpkg install azure-storage-blobs-cpp

View File

@ -0,0 +1,20 @@
{
"name": "AzureSDK-Storage-v12-beta10",
"dockerFile": "Dockerfile",
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
"extensions": [
"ms-vscode.cpptools-themes",
"bbenoist.doxygen",
"streetsidesoftware.code-spell-checker",
"ms-vscode.cpptools",
"xaver.clang-format",
"twxs.cmake",
"ms-vscode.cmake-tools",
"eamodio.gitlens",
"davidschuldenfrei.gtest-adapter"
],
// Do not run as root. See https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "azure-sdk-for-cpp"
}

View File

@ -0,0 +1,31 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: MIT
# VCPKG integration.
if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
CACHE STRING "")
endif()
if(DEFINED ENV{VCPKG_DEFAULT_TRIPLET} AND NOT DEFINED VCPKG_TARGET_TRIPLET)
set(VCPKG_TARGET_TRIPLET "$ENV{VCPKG_DEFAULT_TRIPLET}" CACHE STRING "")
endif()
# Project set up
cmake_minimum_required(VERSION 3.13)
project(Application-using-storage-blobs-from-vcpkg LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
find_package(CURL REQUIRED)
find_package(azure-storage-blobs-cpp CONFIG REQUIRED)
add_executable (
application
src/main
)
# Link to Azure SDK
target_link_libraries(application
PRIVATE
Azure::azure-storage-blobs
)

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,67 @@
# Integrating the Azure SDK for C++ into your application using VCPKG
This application shows how to integrate the Azure SDK for C++ in your application. It uses VCPKG to adquire and build the Azure SDK for C++ client libraries. Your CMake project needs to link the libraries from VCPKG by setting the toolchain file to VCPKG (shown below).
## Pre-requisites
There are two options to set up the development environment:
### Manual installation
Install the [Azure SDK for C++ dependencies](https://github.com/Azure/azure-sdk-for-cpp/blob/master/CONTRIBUTING.md#third-party-dependencies).
- CMake project (min version 3.13).
- C++ version 14 or greater.
### Container
The sample provides a .devcontainer folder which can be used by VSCode to build and run a docker container with the required C++ build tools and with VCPKG installed.
This method requires VSCode + [Remote Container](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) to be installed in the system. Also make sure to have Docker installed and running. This method works for any operating system where Docker and VSCode is supported like Windows, Linux and MacOS. The development environment will be Linux debian 10.
- Open vcpkg folder in VSCode.
- VSCode will detect the `devcontainer` configuration and ask you if you would like to re-open the folder in a container. Click Yes.
- If VSCode did not ask, you can press F1 and type `Reopen in container` option.
Once VSCode builds and run the container, open the terminal and continue to build step.
> Note: The container is set up to automatically link VCPKG to CMake projects by setting env variables that the CMake sample project will use to set the toolchain.
## Build
### Linux terminal
```bash
#
# Building the application.
# Instructions from application root directory.
#
# Create build directory just the first time.
mkdir build
cd build
# Generate and build
# This code assumes that the SDK dependencies were installed with VCPKG
# When using docker provided container, the TOOLCHAIN option is not required (cmake ..).
cmake -DCMAKE_TOOLCHAIN_FILE=path/to/vcpkg/scripts/buildsystems/vcpkg.cmake ..
cmake -build .
```
### Windows VS
If you are using Visual Studio, the toolchain to link VCPKG is set with `CMakeSettings.json`. Upate this file and set the VCPKG toolchain file for VCPKG (VCPKG_ROO\scripts\buildsystems\vcpkg.cmake). After setting the toolchain, VS can generate and build the sample. Use VS to open the sample folder only.
## Run application
Review source code header for `environment variables` that must be set up before running the app.
```bash
#
# Running the Application
# Instructions from inside the build directory.
#
# Run binary (.exe on Windows)
./application
```

View File

@ -0,0 +1,67 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
/**
* @brief This sample provides the code implementation to use the Storage blobs SDK client for C++
* to create a container and upload a blob to it.
*
* @remark The following environment variables must be set before running the sample.
* - AZURE_STORAGE_CONNECTION_STRING: Set it Azure Storage connection string.
*
*/
#include <iostream>
#include <azure/storage/blobs.hpp>
const std::string& GetConnectionString();
int main()
{
using namespace Azure::Storage::Blobs;
std::string containerName = "sample-container";
std::string blobName = "sample-blob";
std::string blobContent = "Hello Azure!";
auto containerClient
= BlobContainerClient::CreateFromConnectionString(GetConnectionString(), containerName);
containerClient.CreateIfNotExists();
BlockBlobClient blobClient = containerClient.GetBlockBlobClient(blobName);
blobClient.UploadFrom(reinterpret_cast<const uint8_t*>(blobContent.data()), blobContent.size());
Azure::Storage::Metadata blobMetadata = {{"key1", "value1"}, {"key2", "value2"}};
blobClient.SetMetadata(blobMetadata);
auto properties = blobClient.GetProperties().Value;
for (auto metadata : properties.Metadata)
{
std::cout << metadata.first << ":" << metadata.second << std::endl;
}
blobContent.resize(static_cast<std::size_t>(properties.BlobSize));
blobClient.DownloadTo(reinterpret_cast<uint8_t*>(&blobContent[0]), blobContent.size());
std::cout << blobContent << std::endl;
return 0;
}
const std::string& GetConnectionString()
{
const static std::string ConnectionString = "";
if (!ConnectionString.empty())
{
return ConnectionString;
}
const static std::string envConnectionString = std::getenv("AZURE_STORAGE_CONNECTION_STRING");
if (!envConnectionString.empty())
{
return envConnectionString;
}
throw std::runtime_error("Cannot find connection string");
}