lab | ||||
---|---|---|---|---|
|
Given the dynamic nature of Microsoft cloud tools, you might experience Azure user interface (UI) changes after the development of this training content. These changes might cause the lab instructions and lab steps to not match.
Microsoft updates this training course when the community brings needed changes to our attention; however, because cloud updates occur frequently, you might encounter UI changes before this training content updates. If this occurs, adapt to the changes, and then work through them in the labs as needed.
Find the taskbar on your Windows 10 desktop. The taskbar contains the icons for the applications that you'll use in this lab:
-
Microsoft Edge
-
File Explorer
- On the taskbar, select the Microsoft Edge icon.
- In the open browser window, go to the Azure portal (https://portal.azure.com).
- Enter the email address for your Microsoft account, and then select Next.
- Enter the password for your Microsoft account, and then select Sign in.
Note: If this is your first time signing in to the Azure portal, you will be offered a tour of the portal. If you prefer to skip the tour, select Get Started to begin using the portal.
-
In the Azure portal's navigation pane, select All services.
-
From the All services blade, select Storage Accounts.
-
From the Storage accounts blade, find your list of Storage instances.
-
From the Storage accounts blade, select Add.
-
Find the tabs from the Create storage account blade, such as Basics.
Note: Each tab represents a step in the workflow to create a new storage account. You can select Review + Create at any time to skip the remaining tabs.
-
From the Basics tab, perform the following actions:
- Leave the Subscription text box set to its default value.
- In the Resource group section, select Create new, enter ConfidentialStack, and then select OK.
- In the Storage account name text box, enter securestor[yourname].
- In the Location drop-down list, select the (US) East US region.
- In the Performance section, select Standard.
- In the Account kind drop-down list, select StorageV2 (general purpose v2).
- In the Replication drop-down list, select Locally-redundant storage (LRS).
- Select Review + Create.
-
From the Review + Create tab, review the options that you selected during the previous steps.
-
Select Create to create the storage account by using your specified configuration.
Note: Wait for the creation task to complete before you move forward with this lab.
-
In the Azure portal's navigation pane, select All services.
-
From the All services blade, select Storage Accounts.
-
From the Storage accounts blade, select the securestor[yourname] storage account that you created earlier in this lab.
-
From the Storage account blade, find the Settings section, and then select the Access keys link.
-
From the Access keys blade, select any one of the keys and record the value in either of the Connection string boxes. You'll use this value later in this lab.
Note: It doesn't matter which connection string you choose. They are interchangeable.
-
In the Azure portal's navigation pane, select the Create a resource link.
-
From the New blade, find the Search the Marketplace text box above the list of featured services.
-
In the search box, enter Vault, and then select Enter.
-
From the Marketplace search results blade, select the Key Vault result.
-
From the Key Vault blade, select Create.
-
Find the tabs from the Create key vault blade, such as Basics.
Note: Each tab represents a step in the workflow to create a new key vault. You can select Review + Create at any time to skip the remaining tabs.
-
From the Basics tab, perform the following actions:
- Leave the Subscription text box set to its default value.
- In the Resource group section, select Use existing, and then select ConfidentialStack in the list.
- In the Key vault name text box, enter securevault[yourname].
- In the Region drop-down list, select the East US region.
- In the Pricing tier drop-down list, select Standard.
- Select Review + Create.
-
From the Review + Create tab, review the options that you selected during the previous steps.
-
Select Create to create the key vault by using your specified configuration.
Note: Wait for the creation task to complete before you move forward with this lab.
-
In the Azure portal's navigation pane, select the Create a resource link.
-
From the New blade, find the Search the Marketplace text box above the list of featured services.
-
In the search box, enter Function, and then select Enter.
-
From the Marketplace search results blade, select the Function App result.
-
From the Function App blade, select Create.
-
Find the tabs from the Function App blade, such as Basics.
Note: Each tab represents a step in the workflow to create a new function app. You can select Review + Create at any time to skip the remaining tabs.
-
From the Basics tab, perform the following actions:
-
Leave the Subscription text box set to its default value.
-
In the Resource group section, select Use existing, and then select SecureFunction in the list.
-
In the Function app name text box, enter securefunc[yourname].
-
In the Publish section, select Code.
-
In the Runtime stack drop-down list, select Node.js.
-
In the Version drop-down list, select 12.
-
In the Region drop-down list, select the East US region.
-
Select Next: Hosting.
-
-
From the Hosting tab, perform the following actions:
-
In the Operating System section, select Linux.
-
In the Storage account drop-down list, select the securestor[yourname] storage account that you created earlier in this lab.
-
In the Plan type drop-down list, select the Consumption (Serverless) option.
-
Select Review + Create.
-
-
From the Review + Create tab, review the options that you selected during the previous steps.
-
Select Create to create the function app by using your specified configuration.
Note: Wait for the creation task to complete before you move forward with this lab.
In this exercise, you created all the resources that you'll use for this lab.
- In the Azure portal's navigation pane, select the Resource groups link.
- From the Resource groups blade, find and then select the ConfidentialStack resource group that you created earlier in this lab.
- From the ConfidentialStack blade, select the securefunc[yourname] function app that you created earlier in this lab.
- From the App Service blade, select the Identity option from the Settings section.
- From the Identity pane, find the System assigned tab, and then perform the following actions:
- In the Status section, select On, and then select Save.
- In the confirmation dialog box, select Yes.
Note: Wait for the system-assigned managed identity to be created before you move forward with this lab.
- In the Azure portal's navigation pane, select the Resource groups link.
- From the Resource groups blade, find and then select the ConfidentialStack resource group that you created earlier in this lab.
- From the ConfidentialStack blade, select the securevault[yourname] key vault that you created earlier in this lab.
- From the Key Vault blade, select the Secrets link in the Settings section.
- In the Secrets pane, select Generate/Import.
- From the Create a secret blade, perform the following actions:
- In the Upload options drop-down list, select Manual.
- In the Name text box, enter storagecredentials.
- In the Value text box, enter the storage account connection string that you recorded earlier in this lab.
- Leave the Content Type text box set to its default value.
- Leave the Set activation date text box set to its default value.
- Leave the Set expiration date text box set to its default value.
- In the Enabled section, select Yes, and then select Create.
Note: Wait for the secret to be created before you move forward with this lab.
- Return to the Secrets pane, and then select the storagecredentials item in the list.
- In the Versions pane, select the latest version of the storagecredentials secret.
- In the Secret Version pane, perform the following actions:
- Find the metadata for the latest version of the secret.
- Select Show secret value to find the value of the secret.
- Record the value of the Secret Identifier text box because you'll use this later in the lab.
Note: You are recording the value of the Secret Identifier text box, not the Secret Value text box.
- In the Azure portal's navigation pane, select the Resource groups link.
- From the Resource groups blade, find and then select the ConfidentialStack resource group that you created earlier in this lab.
- From the ConfidentialStack blade, select the securevault[yourname] key vault that you created earlier in this lab.
- From the Key Vault blade, select the Access policies link in the Settings section.
- In the Access policies pane, select Add Access Policy.
- From the Add access policy blade, perform the following actions:
- Select the Select principal link.
- From the Principal blade, find and then select the service principal named securefunc[yourname], and then select Select.
Note: The system-assigned managed identity you created earlier in this lab will have the same name as the Azure Function resource.
- Leave the Key permissions list set to its default value.
- In the Secret permissions drop-down list, select the GET permission.
- Leave the Certificate permissions list set to its default value.
- Leave the Authorized application text box set to its default value.
- Select Add.
- Back in the Access policies pane, select Save.
Note: Wait for your changes to the access policies to save before you move forward with this lab.
-
In the Azure portal's navigation pane, select the Resource groups link.
-
From the Resource groups blade, find and then select the ConfidentialStack resource group that you created earlier in this lab.
-
From the ConfidentialStack blade, select the securefunc[yourname] function app that you created earlier in this lab.
-
From the App Service blade, select the Configuration option from the Settings section.
-
From the Configuration pane, perform the following actions:
- Select the Application settings tab, and then select New application setting.
- In the Add/Edit application setting pop-up window, in the Name text box, enter StorageConnectionString.
- In the Value text box, construct a value by using the following syntax:
@Microsoft.KeyVault(SecretUri=*Secret Identifier*)
Note: You'll need to build a reference to your Secret Identifier by using the above syntax. For example, if your secret identifier is
https://securevaultstudent.vault.azure.net/secrets/storagecredentials/17b41386df3e4191b92f089f5efb4cbf
, your value would be@Microsoft.KeyVault(SecretUri=https://securevaultstudent.vault.azure.net/secrets/storagecredentials/17b41386df3e4191b92f089f5efb4cbf)
. - Leave the deployment slot setting text box set to its default value.
- Select OK to close the pop-up window and return to the Configuration section.
- Select Save from the blade to save your settings.
- In the Save Changes confirmation pop-up dialog box, select Continue.
Note: Wait for your application settings to save before you move forward with the lab.
- In the Save Changes confirmation popup dialog, select Continue.
Note: Wait for your application settings to persist before you move forward with the lab.
-
In the Azure portal's navigation pane, select the Resource groups link.
-
From the Resource groups blade, find and then select the SecureFunction resource group that you created earlier in this lab.
-
From the SecureFunction blade, select the securefunc[yourname] function app that you created earlier in this lab.
-
From the Function App blade, select the plus sign (+) next to the Functions drop-down list.
-
In the New Azure Function quickstart, perform the following actions:
-
Under the Choose a Development Environment header, select In-Portal, and then select Continue.
-
Under the Create a Function header, select More templates, and then select Finish and view templates.
-
In the list of templates, select HTTP trigger.
-
In the New Function pop-up window, find the Name text box, and then enter FileParser.
-
In the New Function pop-up window, find the Authorization level list, and then select Anonymous.
-
In the New Function pop-up window, select Create.
-
-
In the function editor, find the example function script.
-
Delete all the example code, and then in the function editor, copy and paste the following placeholder function:
module.exports = async function (context, req) { context.log('JavaScript HTTP trigger function processed a request.'); context.res = { body: 'Test Successful' }; }
-
Select Save and run to save the script and perform a test of the function.
-
The Test and Logs panes will automatically open when the script runs for the first time.
Note: You may notice warning content during compilation in the log. This can be safely ignored.
-
Find the Output text box in the Test pane. You should now notice the value Test Successful returned from the function.
-
Observe the index.js file method, which should now include:
module.exports = async function (context, req) { context.log('JavaScript HTTP trigger function processed a request.'); context.res = { body: 'Test Successful' }; }
-
Add the following line of code to get the value of the StorageConnectionString application setting by using the process.env collection:
var connectionString = process.env['StorageConnectionString'];
-
Add the output lines of code to return the value of the connectionString variable:
context.res = { body: connectionString };
-
Observe the index.js file, which should now include:
module.exports = async function (context, req) { context.log('JavaScript HTTP trigger function processed a request.'); var connectionString = process.env['StorageConnectionString']; context.res = { body: connectionString }; }
-
Select Save and run to save the script and perform a test of the function.
-
Find the Output text box in the Test pane. You should now notice the connection string returned from the function.
In this exercise, you used a service identity to read the value of a secret stored in Key Vault and returned that value as the result of an function app.
- In the Azure portal's navigation pane, select the Resource groups link.
- From the Resource groups blade, find and then select the ConfidentialStack resource group that you created earlier in this lab.
- From the ConfidentialStack blade, select the securestor[yourname] storage account that you created earlier in this lab.
- From the Storage account blade, select the Containers link in the Blob service section.
- In the Containers section, select + Container.
- In the New container pop-up window, perform the following actions:
- In the Name text box, enter drop.
- In the Public access level drop-down list, select Blob (anonymous read access for blobs only), and then select OK.
- Return to the Containers section, and then select the newly created drop container.
- From the Container blade, select Upload.
- In the Upload blob pop-up window, perform the following actions:
- In the Files section, select the Folder icon.
- In the File Explorer window, browse to Allfiles (F):\Allfiles\Labs\07\Starter, select the records.json file, and then select Open.
- Ensure that Overwrite if files already exist is selected, and then select Upload.
Note: Wait for the blob to upload before you continue with this lab.
- Return to the Container blade, and then select the records.json blob in the list of blobs.
- From the Blob blade, find the blob metadata, and then copy the URL for the blob.
- On the taskbar, right-click the Microsoft Edge icon or activate the shortcut menu, and then select New window.
- In the new browser window, go to the URL that you copied for the blob.
- The JavaScript Object Notation (JSON) contents of the blob should now display. Close the browser window with the JSON contents.
- Return to the browser window with the Azure portal, and then close the Blob blade.
- Return to the Container blade, and then select Change access level policy.
- In the Change access level pop-up window, perform the following actions:
- In the Public access level drop-down list, select Private (no anonymous access).
- Select OK.
- On the taskbar, right-click the Microsoft Edge icon or activate the shortcut menu, and then select New window.
- In the new browser window, go to the URL that you copied for the blob.
- An error message indicating that the resource wasn't found should now display.
Note: If the error message doesn't display, your browser might have cached the file. Press Ctrl+F5 to refresh the page until the error message displays.
-
In the Azure portal's navigation pane, select the Resource groups link.
-
From the Resource groups blade, find and then select the SecureFunction resource group that you created earlier in this lab.
-
From the SecureFunction blade, select the securefunc[yourname] function app that you created earlier in this lab.
-
From the Function App blade, find and then select the Console to open consol for the function.
-
Execute following command to generate file
package.json
:npm init -y
-
The next command should add Azure Blob npm package:
npm install @azure/storage-blob
-
Return back to the function code. Add the following line with reference to package in the first line of the file index.js
const { BlobServiceClient } = require('@azure/storage-blob');
-
Observe the index.js file, which should now include:
const { BlobServiceClient } = require('@azure/storage-blob'); module.exports = async function (context, req) { context.log('JavaScript HTTP trigger function processed a request.'); var connectionString = process.env['StorageConnectionString']; context.res = { body: connectionString }; }
-
To test your changes you can run the function again. It should return the same output as previously. It should not return any errors. If you get error check the steps of creation
package.js
file and include required npm package.
-
Add the following line of code in the function the value of the StorageConnectionString application setting will be retrieved by using the process.env method:
var connectionString = process.env['StorageConnectionString'];
-
Add the following line of code to create a new instance of the class by call BlobServiceClient.fromConnectionString method and passing your connectionString variable to the method:
var blobServiceClient = await BlobServiceClient.fromConnectionString(connectionString);
-
Add the following line of code to use the BlobServiceClient.getContainerClient method, while passing in the drop container name to create a new instance of the BlobContainerClient class that references the container that you created earlier in this lab:
var containerClient = await blobServiceClient.getContainerClient("drop");
-
Add the following lines of code to use the BlobContainerClient.exists method, while passing reference to drop container to check if the container exists:
if (await containerClient.exists()) { context.res = { body: `container found` }; }
-
Observe the index.js file, which should now include:
const { BlobServiceClient } = require('@azure/storage-blob'); module.exports = async function (context, req) { context.log('JavaScript HTTP trigger function processed a request.'); var connectionString = process.env['StorageConnectionString']; var blobServiceClient = await BlobServiceClient.fromConnectionString(connectionString); var containerClient = await blobServiceClient.getContainerClient("drop"); if (await containerClient.exists()) { context.res = { body: `container found` }; } }
-
You can test the function again. IF the container found in the blob you will get appropriate message. If you get empty response or error you should check if the container exists.
-
Add the following line of code to use the BlobServiceClient.getContainerClient() method, while passing in the records.json blob name to create a new instance of the BlockBlobClient class that references the blob that you uploaded earlier in this lab:
var blockBlobClient = containerClient.getBlockBlobClient('records.json');
-
Add the following line of code to use the BlockBlobClient.download method to download the contents of the referenced blob asynchronously and store the result in a variable named downloadBlockBlobResponse:
var downloadBlockBlobResponse = await blockBlobClient.download(0);
-
Add the following line of code to return the various content stored in the downloadBlockBlobResponse variable by using the additional streamToString function, you can paste the code above the main function:
async function streamToString(readableStream) { return new Promise((resolve, reject) => { const chunks = []; readableStream.on('data', (data) => { chunks.push(data.toString()); }); readableStream.on('end', () => { resolve(chunks.join('')); }); readableStream.on('error', reject); }); }
-
Finlay you need to add call to the streamToString function from the main function and return content as response:
var filecontent = await streamToString(downloadBlockBlobResponse.readableStreamBody); context.res = { body: filecontent };
-
Observe the final code:
const { BlobServiceClient } = require('@azure/storage-blob'); async function streamToString(readableStream) { return new Promise((resolve, reject) => { const chunks = []; readableStream.on('data', (data) => { chunks.push(data.toString()); }); readableStream.on('end', () => { resolve(chunks.join('')); }); readableStream.on('error', reject); }); } module.exports = async function (context, req) { context.log('JavaScript HTTP trigger function processed a request.'); var connectionString = process.env['StorageConnectionString']; var blobServiceClient = await BlobServiceClient.fromConnectionString(connectionString); var containerClient = await blobServiceClient.getContainerClient("drop"); if (await containerClient.exists()) { var blockBlobClient = containerClient.getBlockBlobClient('records.json'); var downloadBlockBlobResponse = await blockBlobClient.download(0); var filecontent = await streamToString(downloadBlockBlobResponse.readableStreamBody); context.res = { body: filecontent }; } }
-
Select Save and run to save the script and perform a test of the function.
-
Find the Output text box in the Test pane. You should now notice the content of the $/drop/records.json blob stored in your storage account.
In this exercise, you used Node.js code to access a storage account, and then download the contents of a blob.
-
In the Azure portal's navigation pane, select the Cloud Shell icon to open a new shell instance.
Note: The Cloud Shell icon is represented by a greater than sign (>) and underscore character (_).
-
If this is your first time opening Cloud Shell using your subscription, you can use the Welcome to Azure Cloud Shell Wizard to configure Cloud Shell for first-time usage. Perform the following actions in the wizard:
- A dialog box prompts you to create a new storage account to begin using the shell. Accept the default settings, and then select Create storage.
Note: Wait for Cloud Shell to finish its initial setup procedures before moving forward with the lab. If Cloud Shell configuration options don't display, this is most likely because you are using an existing subscription with this course's labs. The labs are written with the presumption that you are using a new subscription.
-
When you receive the command prompt, enter the following command, and then select Enter to delete the ConfidentialStack resource group:
az group delete --name ConfidentialStack --no-wait --yes
-
Close the Cloud Shell pane in the portal.
- Close the currently running Microsoft Edge application.
Review: In this exercise, you cleaned up your subscription by removing the resource groups used in this lab.