|
| 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 | + |
0 commit comments