Skip to content

Commit feb7a85

Browse files
DataBoxEdge SDK Rel- 2021 (Azure#23746)
* SDK Release 2021 * Updated TestConstants with the new resource details * Activation Key Generation Code * GenerateActivationKey and Tests * Save CIK in KeyVault * KeyVault Update Extended Info * Session Records with new resource * Running Order Tests * Updated Comments * Fixed ResourceName * Minor changes * Minor change * Fixed the buld error * Addressed Comments
1 parent 1f92b10 commit feb7a85

File tree

178 files changed

+17819
-3664
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+17819
-3664
lines changed
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
Installing AutoRest version: latest
1+
Installing AutoRest version: v2
22
AutoRest installed successfully.
33
Commencing code generation
44
Generating CSharp code
55
Executing AutoRest command
6-
cmd.exe /c autorest.cmd https://github.com/Azure/azure-rest-api-specs/blob/master/specification/databoxedge/resource-manager/readme.md --csharp --version=latest --reflect-api-versions --csharp-sdks-folder=D:\Code\azure-sdk-for-net\azure-sdk-for-net\sdk
7-
2019-12-09 08:27:42 UTC
6+
cmd.exe /c autorest.cmd https://github.com/Azure/azure-rest-api-specs/blob/master/specification/databoxedge/resource-manager/readme.md --csharp --version=v2 --reflect-api-versions --csharp-sdks-folder=D:\SDKRepos\azure-sdk-for-net\sdk
7+
Autorest CSharp Version: 2.3.82
8+
2021-08-30 14:10:19 UTC
89
Azure-rest-api-specs repository information
910
GitHub fork: Azure
1011
Branch: master
11-
Commit: ef354ec8d6580227707ed935684e533b898beabe
12+
Commit: b020247789ba2ab0065ebbcfa69050ce729493b8
1213
AutoRest information
13-
Requested version: latest
14-
Bootstrapper version: autorest@2.0.4407
14+
Requested version: v2
15+
Bootstrapper version: autorest@2.0.4413
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<!--This file and it's contents are updated at build time moving or editing might result in build failure. Take due deligence while editing this file-->
33
<PropertyGroup>
4-
<AzureApiTag>DataBox_2019-08-01;</AzureApiTag>
4+
<AzureApiTag>DataBoxEdge_2021-02-01;</AzureApiTag>
55
<PackageTags>$(PackageTags);$(CommonTags);$(AzureApiTag);</PackageTags>
66
</PropertyGroup>
77
</Project>
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
using Microsoft.Azure.Management.DataBoxEdge.Models;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Security.Cryptography;
5+
using System.Security.Cryptography.X509Certificates;
6+
using System.Text;
7+
8+
namespace Microsoft.Azure.Management.DataBoxEdge
9+
{
10+
internal static class ActivationKeyHelper
11+
{
12+
#region GenerateActivationKey
13+
14+
/// <summary>
15+
/// Generates the vault certificate.
16+
/// </summary>
17+
/// <param name="resourceGroupName"></param>
18+
/// <param name="resourceName"></param>
19+
/// <returns></returns>
20+
internal static GenerateCertResponse GenerateVaultCertificate(IDevicesOperations operations, string resourceGroupName,
21+
string resourceName)
22+
{
23+
return operations.GenerateCertificate(resourceName,
24+
resourceGroupName);
25+
}
26+
27+
/// <summary>
28+
/// Imports the raw data of a certificate into a X509Certificate2 object
29+
/// </summary>
30+
/// <returns>Returns the X509Certificate2 format of public part of the certificate</returns>
31+
internal static X509Certificate2 ImportCertificate(string rawData)
32+
{
33+
var rawDataByteArray = Encoding.UTF8.GetBytes(rawData);
34+
var cert = new X509Certificate2(rawDataByteArray);
35+
return cert;
36+
}
37+
38+
/// <summary>
39+
/// Uploads the vault certificate.
40+
/// </summary>
41+
/// <param name="resourceGroupName"></param>
42+
/// <param name="resourceName"></param>
43+
/// <param name="cert"></param>
44+
/// <returns></returns>
45+
internal static UploadCertificateResponse UploadVaultCertificate(IDevicesOperations operations, string resourceGroupName,
46+
string resourceName, X509Certificate2 cert)
47+
{
48+
var request = new UploadCertificateRequest
49+
{
50+
Certificate = Convert.ToBase64String(cert.RawData)
51+
};
52+
53+
return operations.UploadCertificate(resourceName, request,
54+
resourceGroupName);
55+
}
56+
57+
/// <summary>
58+
/// Uploads the vault certificate.
59+
/// </summary>
60+
/// <param name="resourceGroupName"></param>
61+
/// <param name="resourceName"></param>
62+
/// <param name="resourceLocation"></param>
63+
/// <param name="certWithPrivateKey"></param>
64+
/// <param name="uploadCertificate"></param>
65+
/// <returns></returns>
66+
internal static string GetAadActivationKey(string resourceGroupName, string resourceName,
67+
string resourceLocation, string certWithPrivateKey,
68+
UploadCertificateResponse uploadCertificate, string subscriptionId, string cik)
69+
{
70+
var RegistrationKeyHashSize = 16;
71+
var vault = GetVaultCredentials(resourceGroupName, resourceName, resourceLocation, certWithPrivateKey, uploadCertificate, subscriptionId, cik);
72+
73+
var st = JsonConvert.SerializeObject(vault);
74+
var plainTextBytes = Encoding.UTF8.GetBytes(st);
75+
var activation = Convert.ToBase64String(plainTextBytes);
76+
var hash = GenerateSha512Hash(activation);
77+
return $"{activation}#{hash.Substring(0, RegistrationKeyHashSize)}";
78+
}
79+
80+
/// <summary>
81+
/// Gets the vault credentials.
82+
/// </summary>
83+
/// <param name="resourceGroupName"></param>
84+
/// <param name="resourceName"></param>
85+
/// <param name="resourceLocation"></param>
86+
/// <param name="serializedCertificate"></param>
87+
/// <param name="uploadCertificate"></param>
88+
/// <returns></returns>
89+
public static ActivationKeyComponents GetVaultCredentials(string resourceGroupName,
90+
string resourceName, string resourceLocation, string serializedCertificate, UploadCertificateResponse uploadCertificate, string subscriptionId, string cik)
91+
{
92+
const string AudienceFormat = @"https://azuredataboxedge/{0}/{1}/{2}";
93+
94+
var aadAudience = string.IsNullOrWhiteSpace(uploadCertificate.AadAudience) ?
95+
string.Format(AudienceFormat, resourceLocation, uploadCertificate.ResourceId, uploadCertificate.ResourceId) :
96+
uploadCertificate.AadAudience;
97+
98+
var vault = new ActivationKeyComponents
99+
{
100+
SubscriptionId = subscriptionId,
101+
ResourceType = "dataBoxEdgeDevices",
102+
ResourceName = resourceName,
103+
ManagementCert = serializedCertificate,
104+
ResourceId = uploadCertificate.ResourceId,
105+
AadAuthority = uploadCertificate.AadAuthority,
106+
AadAudience = aadAudience,
107+
AadTenantId = uploadCertificate.AadTenantId,
108+
ServicePrincipalClientId = uploadCertificate.ServicePrincipalClientId,
109+
AzureManagementEndpointAudience = uploadCertificate.AzureManagementEndpointAudience,
110+
ProviderNamespace = "Microsoft.DataBoxEdge",
111+
ResourceGroup = resourceGroupName,
112+
ServiceDataIntegrityKey = cik,
113+
IdentityProvider = "AAD"
114+
};
115+
116+
return vault;
117+
}
118+
119+
/// <summary>
120+
/// Generates the sha512 hash.
121+
/// </summary>
122+
/// <param name="text">The text.</param>
123+
/// <returns></returns>
124+
internal static string GenerateSha512Hash(string text)
125+
{
126+
var alg = SHA512.Create();
127+
byte[] result = alg.ComputeHash(Encoding.UTF8.GetBytes(text));
128+
129+
return ConvertByteArrayToString(result);
130+
}
131+
132+
/// <summary>
133+
/// Converts the byte array to string.
134+
/// </summary>
135+
/// <param name="data">The data.</param>
136+
/// <returns></returns>
137+
private static string ConvertByteArrayToString(byte[] data)
138+
{
139+
StringBuilder sBuilder = new StringBuilder();
140+
141+
for (int i = 0; i < data.Length; i++)
142+
{
143+
sBuilder.Append(data[i].ToString("x2"));
144+
}
145+
146+
return sBuilder.ToString();
147+
}
148+
}
149+
150+
#region ActivationKeyComponents
151+
/// <summary>
152+
/// Components of Activation Key
153+
/// </summary>
154+
public class ActivationKeyComponents
155+
{
156+
#region Properties
157+
158+
/// <summary>
159+
/// Gets or Sets the SubscriptionId
160+
/// </summary>
161+
public string SubscriptionId { get; set; }
162+
163+
/// <summary>
164+
/// Gets or sets the key name for ResourceType entry.
165+
/// </summary>
166+
public string ResourceType { get; set; }
167+
168+
/// <summary>
169+
/// Gets or sets the key name for ResourceName entry.
170+
/// </summary>
171+
public string ResourceName { get; set; }
172+
173+
/// <summary>
174+
/// Gets or sets the key name for ManagementCert entry.
175+
/// </summary>
176+
public string ManagementCert { get; set; }
177+
178+
/// <summary>
179+
/// Gets or sets the resource id of the vault.
180+
/// </summary>
181+
public string ResourceId { get; set; }
182+
183+
/// <summary>
184+
/// Gets or sets the AAD Authority.
185+
/// </summary>
186+
public string AadAuthority { get; set; }
187+
188+
/// <summary>
189+
/// Gets or sets AadAudience
190+
/// </summary>
191+
public string AadAudience { get; set; }
192+
193+
/// <summary>
194+
/// Gets or sets the AAD Tenant Id.
195+
/// </summary>
196+
public string AadTenantId { get; set; }
197+
198+
/// <summary>
199+
/// Gets or sets the Service Principal Client Id.
200+
/// </summary>
201+
public string ServicePrincipalClientId { get; set; }
202+
203+
/// <summary>
204+
/// Gets or sets the Azure Management Endpoint Audience.
205+
/// </summary>
206+
public string AzureManagementEndpointAudience { get; set; }
207+
208+
/// <summary>
209+
/// Gets or sets the ProviderNamespace.
210+
/// </summary>
211+
public string ProviderNamespace { get; set; }
212+
213+
/// <summary>
214+
/// Gets or sets the Resource Group.
215+
/// </summary>
216+
public string ResourceGroup { get; set; }
217+
218+
/// <summary>
219+
/// Gets or sets the Location.
220+
/// </summary>
221+
public string ServiceDataIntegrityKey { get; set; }
222+
223+
/// <summary>
224+
/// Gets or sets the IdentityProvider.
225+
/// </summary>
226+
public string IdentityProvider { get; set; }
227+
#endregion
228+
}
229+
#endregion
230+
#endregion
231+
}
232+

sdk/databoxedge/Microsoft.Azure.Management.DataBoxEdge/src/Customizations/ExtendedClientMethods.cs

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,16 @@
11
using Microsoft.Azure.Management.DataBoxEdge.Models;
22
using Microsoft.Rest;
3-
using Newtonsoft.Json.Linq;
43
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
55
using System;
6-
using System.Collections.Generic;
7-
using System.Linq;
6+
using System.Security.Cryptography;
87
using System.Text;
9-
using System.Threading.Tasks;
108

119
namespace Microsoft.Azure.Management.DataBoxEdge
1210
{
1311
public static partial class ExtendedClientMethods
1412
{
15-
16-
/// <summary>
17-
/// Use this method to encrypt the user secrets (Storage Account Access Key, Volume Container Encryption Key etc.) using activation key
18-
/// </summary>
19-
/// <param name="deviceName">
20-
/// The resource name.
21-
/// </param>
22-
/// <param name="resourceGroupName">
23-
/// The resource group name.
24-
/// </param>
25-
/// <param name="plainTextSecret">
26-
/// The plain text secret.
27-
/// </param>
28-
/// <returns>
29-
/// The <see cref="AsymmetricEncryptedSecret"/>.
30-
/// </returns>
31-
/// <exception cref="ValidationException">
32-
/// </exception>
33-
/// <exception cref="InvalidOperationException">
34-
/// </exception>
35-
public static AsymmetricEncryptedSecret GetAsymmetricEncryptedSecretUsingActivationKey(
36-
this IDevicesOperations operations,
37-
string deviceName,
38-
string resourceGroupName,
39-
40-
string plainTextSecret,
41-
string activationKey)
42-
{
43-
if (string.IsNullOrWhiteSpace(activationKey))
44-
{
45-
throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.CannotBeNull, "activationKey");
46-
}
47-
48-
49-
50-
string channelIntegrationKey = GetChannelIntegrityKey(activationKey);
51-
return operations.GetAsymmetricEncryptedSecret(deviceName, resourceGroupName, plainTextSecret, channelIntegrationKey);
52-
}
13+
private const int StandardSizeOfCIK = 128;
5314

5415
/// <summary>
5516
/// Use this method to encrypt the user secrets (Storage Account Access Key, Volume Container Encryption Key etc.) using CIK
@@ -119,5 +80,43 @@ private static string GetChannelIntegrityKey(string activationKey)
11980
string serviceDataIntegrityKey = jsondata["serviceDataIntegrityKey"].Value<string>();
12081
return serviceDataIntegrityKey;
12182
}
83+
84+
/// <summary>
85+
/// Use this method to generate the activation key for a device to register it with the ASE resource
86+
/// </summary>
87+
/// <param name="resourceGroupName">Name of the resource group</param>
88+
/// <param name="resourceName">Name of the resource</param>
89+
/// <param name="resourceLocation">Location of the resource</param>
90+
91+
/// <returns></returns>
92+
public static string GenerateActivationKey(this IDevicesOperations operations,
93+
string resourceGroupName,
94+
string resourceName,
95+
string cik)
96+
{
97+
var resourceLocation = operations.Get(resourceName, resourceGroupName).Location;
98+
var subscriptionId = (operations as DevicesOperations).Client.SubscriptionId;
99+
var generateCertResponse = ActivationKeyHelper.GenerateVaultCertificate(operations, resourceGroupName, resourceName);
100+
var certPublicPart = ActivationKeyHelper.ImportCertificate(generateCertResponse.PublicKey);
101+
var uploadCertificateResponse = ActivationKeyHelper.UploadVaultCertificate(operations, resourceGroupName, resourceName, certPublicPart);
102+
var activationKeyToRegisterTheResource = ActivationKeyHelper.GetAadActivationKey(resourceGroupName, resourceName, resourceLocation,
103+
generateCertResponse.PrivateKey, uploadCertificateResponse, subscriptionId, cik);
104+
105+
return activationKeyToRegisterTheResource;
106+
}
107+
108+
/// <summary>
109+
/// This method generates the CIK of length 128 chars
110+
/// </summary>
111+
/// <returns></returns>
112+
public static string GenerateCIK(this IDevicesOperations operations)
113+
{
114+
var randomNumberGenerator = RandomNumberGenerator.Create();
115+
var byteArr = new byte[128];
116+
randomNumberGenerator.GetBytes(byteArr);
117+
var cik = Convert.ToBase64String(byteArr).Substring(0, StandardSizeOfCIK);
118+
119+
return cik;
120+
}
122121
}
123122
}

0 commit comments

Comments
 (0)