|
12 | 12 | // limitations under the License.
|
13 | 13 | // ----------------------------------------------------------------------------------
|
14 | 14 |
|
| 15 | +using System; |
| 16 | +using System.Security.Cryptography.X509Certificates; |
15 | 17 | using System.Threading.Tasks;
|
| 18 | +using Microsoft.Azure.Commands.RecoveryServices.Properties; |
| 19 | +using Microsoft.Azure.Commands.RecoveryServices.SiteRecovery; |
| 20 | +using Microsoft.Azure.Portal.HybridServicesCore; |
| 21 | +using Microsoft.Azure.Portal.RecoveryServices.Models.Common; |
16 | 22 | using Microsoft.WindowsAzure;
|
17 | 23 | using Microsoft.WindowsAzure.Management.SiteRecovery.Models;
|
| 24 | +using rpError = Microsoft.Azure.Commands.RecoveryServices.RestApiInfra; |
18 | 25 |
|
19 | 26 | namespace Microsoft.Azure.Commands.RecoveryServices
|
20 | 27 | {
|
@@ -53,5 +60,150 @@ public async Task<UploadCertificateResponse> UpdateVaultCertificate(CertificateA
|
53 | 60 | {
|
54 | 61 | return await this.GetSiteRecoveryClient().VaultExtendedInfo.UploadCertificateAsync(args, this.GetRequestHeaders(false));
|
55 | 62 | }
|
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Gets the vault credential object |
| 66 | + /// </summary> |
| 67 | + /// <param name="managementCert">certificate to be uploaded</param> |
| 68 | + /// <param name="vault">vault object</param> |
| 69 | + /// <param name="site">site object </param> |
| 70 | + /// <returns>credential object</returns> |
| 71 | + public ASRVaultCreds GetVaultCrentials(X509Certificate2 managementCert, ASRVault vault, Site site) |
| 72 | + { |
| 73 | + Utilities.UpdateVaultSettings(new ASRVaultCreds() |
| 74 | + { |
| 75 | + CloudServiceName = vault.CloudServiceName, |
| 76 | + ResourceName = vault.Name |
| 77 | + }); |
| 78 | + |
| 79 | + // Get Channel Integrity key |
| 80 | + string channelIntegrityKey; |
| 81 | + Task<string> getChannelIntegrityKey = this.GetChannelIntegrityKey(); |
| 82 | + |
| 83 | + // Making sure we can generate the file, once the SDK and portal are inter-operable |
| 84 | + // upload certificate and fetch of ACIK can be made parallel to improvve the performace. |
| 85 | + getChannelIntegrityKey.Wait(); |
| 86 | + |
| 87 | + // Upload certificate |
| 88 | + UploadCertificateResponse acsDetails; |
| 89 | + Task<UploadCertificateResponse> uploadCertificate = this.UpdateVaultCertificate(managementCert); |
| 90 | + uploadCertificate.Wait(); |
| 91 | + |
| 92 | + acsDetails = uploadCertificate.Result; |
| 93 | + channelIntegrityKey = getChannelIntegrityKey.Result; |
| 94 | + |
| 95 | + ASRVaultCreds asrVaultCreds = this.GenerateCredential( |
| 96 | + managementCert, |
| 97 | + acsDetails, |
| 98 | + channelIntegrityKey, |
| 99 | + vault, |
| 100 | + site); |
| 101 | + |
| 102 | + return asrVaultCreds; |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Method to update vault certificate |
| 107 | + /// </summary> |
| 108 | + /// <param name="cert">certificate object </param> |
| 109 | + /// <returns>Upload Certificate Response</returns> |
| 110 | + private async Task<UploadCertificateResponse> UpdateVaultCertificate(X509Certificate2 cert) |
| 111 | + { |
| 112 | + var certificateArgs = new CertificateArgs() |
| 113 | + { |
| 114 | + Certificate = Convert.ToBase64String(cert.GetRawCertData()), |
| 115 | + ContractVersion = "V2012_12" |
| 116 | + }; |
| 117 | + |
| 118 | + UploadCertificateResponse response = await this.UpdateVaultCertificate(certificateArgs); |
| 119 | + |
| 120 | + return response; |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Get the Integrity key |
| 125 | + /// </summary> |
| 126 | + /// <returns>key as string.</returns> |
| 127 | + private async Task<string> GetChannelIntegrityKey() |
| 128 | + { |
| 129 | + ResourceExtendedInformation extendedInformation = null; |
| 130 | + try |
| 131 | + { |
| 132 | + extendedInformation = await this.GetExtendedInfo(); |
| 133 | + } |
| 134 | + catch (Exception exception) |
| 135 | + { |
| 136 | + CloudException cloudException = exception as CloudException; |
| 137 | + |
| 138 | + if (cloudException != null && cloudException.Response != null && !string.IsNullOrEmpty(cloudException.Response.Content)) |
| 139 | + { |
| 140 | + rpError.Error error = (rpError.Error)Utilities.Deserialize<rpError.Error>(cloudException.Response.Content); |
| 141 | + if (error.ErrorCode.Equals(RpErrorCode.ResourceExtendedInfoNotFound.ToString(), StringComparison.InvariantCultureIgnoreCase)) |
| 142 | + { |
| 143 | + extendedInformation = new ResourceExtendedInformation(); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + ResourceExtendedInfo extendedInfo = Utilities.Deserialize<ResourceExtendedInfo>(extendedInformation.ExtendedInfo); |
| 149 | + |
| 150 | + if (extendedInfo == null) |
| 151 | + { |
| 152 | + extendedInfo = this.CreateVaultExtendedInformatino(); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + if (!extendedInfo.Algorithm.Equals(CryptoAlgorithm.None.ToString(), StringComparison.InvariantCultureIgnoreCase)) |
| 157 | + { |
| 158 | + // In case this condition is true that means the credential was first generated in portal |
| 159 | + // and hence can not be fetched here. |
| 160 | + throw new CloudException(Resources.VaultCredentialGenerationUnSopported); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return extendedInfo.ChannelIntegrityKey; |
| 165 | + } |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// Method to create the extended info for the vault. |
| 169 | + /// </summary> |
| 170 | + /// <returns>returns the object as task</returns> |
| 171 | + private ResourceExtendedInfo CreateVaultExtendedInformatino() |
| 172 | + { |
| 173 | + ResourceExtendedInfo extendedInfo = new ResourceExtendedInfo(); |
| 174 | + extendedInfo.GenerateSecurityInfo(); |
| 175 | + ResourceExtendedInformationArgs extendedInfoArgs = extendedInfo.Translate(); |
| 176 | + this.CreateExtendedInfo(extendedInfoArgs); |
| 177 | + |
| 178 | + return extendedInfo; |
| 179 | + } |
| 180 | + |
| 181 | + /// <summary> |
| 182 | + /// Method to generate the credential file content |
| 183 | + /// </summary> |
| 184 | + /// <param name="managementCert">management cert</param> |
| 185 | + /// <param name="acsDetails">ACS details</param> |
| 186 | + /// <param name="channelIntegrityKey">Integrity key</param> |
| 187 | + /// <param name="vault">vault object</param> |
| 188 | + /// <param name="site">site object</param> |
| 189 | + /// <returns>vault credential object</returns> |
| 190 | + private ASRVaultCreds GenerateCredential(X509Certificate2 managementCert, UploadCertificateResponse acsDetails, string channelIntegrityKey, ASRVault vault, Site site) |
| 191 | + { |
| 192 | + string serializedCertifivate = Convert.ToBase64String(managementCert.Export(X509ContentType.Pfx)); |
| 193 | + |
| 194 | + AcsNamespace acsNamespace = new AcsNamespace(acsDetails); |
| 195 | + |
| 196 | + ASRVaultCreds vaultCreds = new ASRVaultCreds( |
| 197 | + vault.SubscriptionId, |
| 198 | + vault.Name, |
| 199 | + serializedCertifivate, |
| 200 | + acsNamespace, |
| 201 | + channelIntegrityKey, |
| 202 | + vault.CloudServiceName, |
| 203 | + site.ID, |
| 204 | + site.Name); |
| 205 | + |
| 206 | + return vaultCreds; |
| 207 | + } |
56 | 208 | }
|
57 | 209 | }
|
0 commit comments