|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Management.Automation; |
| 17 | +using System.Security.Cryptography.X509Certificates; |
| 18 | +using System.Threading.Tasks; |
| 19 | +using Microsoft.Azure.Commands.RecoveryServices.lib; |
| 20 | +using Microsoft.Azure.Commands.RecoveryServices.SiteRecovery; |
| 21 | +using Microsoft.Azure.Portal.HybridServicesCore; |
| 22 | +using Microsoft.Azure.Portal.RecoveryServices.Models.Common; |
| 23 | +using Microsoft.WindowsAzure.Management.SiteRecovery.Models; |
| 24 | +using Microsoft.WindowsAzure.Commands.Common; |
| 25 | +using Microsoft.WindowsAzure.Commands.Common.Models; |
| 26 | + |
| 27 | +namespace Microsoft.Azure.Commands.RecoveryServices |
| 28 | +{ |
| 29 | + /// <summary> |
| 30 | + /// Retrieves Azure Site Recovery Server. |
| 31 | + /// </summary> |
| 32 | + [Cmdlet(VerbsCommon.Get, "AzureSiteRecoveryVaultCredential", DefaultParameterSetName = ASRParameterSets.Default)] |
| 33 | + [OutputType(typeof(string))] |
| 34 | + public class GetVaultCredentialsFile : RecoveryServicesCmdletBase |
| 35 | + { |
| 36 | + private const int VaultCertificateExpiryInHoursForHRM = 120; |
| 37 | + |
| 38 | + #region Parameters |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Gets or sets the vault name |
| 42 | + /// </summary> |
| 43 | + [Parameter(ParameterSetName = ASRParameterSets.ByParam, HelpMessage = "Vault Name for which the cred file to be generated")] |
| 44 | + [ValidateNotNullOrEmpty] |
| 45 | + public string Name { get; set; } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Gets or sets the vault name |
| 49 | + /// </summary> |
| 50 | + [Parameter(ParameterSetName = ASRParameterSets.ByParam, HelpMessage = "Vault Name for which the cred file to be generated")] |
| 51 | + [ValidateNotNullOrEmpty] |
| 52 | + // TODO: devsri - Remove this. |
| 53 | + public string CloudServiceName { get; set; } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets or sets the location of the vault |
| 57 | + /// </summary> |
| 58 | + [Parameter(ParameterSetName = ASRParameterSets.ByParam, HelpMessage = "Geo Name to which the vault belongs")] |
| 59 | + [ValidateNotNullOrEmpty] |
| 60 | + public string Geo { get; set; } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Gets or sets the path where the credential file is to be generated |
| 64 | + /// </summary> |
| 65 | + [Parameter(ParameterSetName = ASRParameterSets.ByParam, Mandatory = false, HelpMessage = "The site name if the vault credentials to be downloaded for a Hyper-V sites.")] |
| 66 | + public string SiteName { get; set; } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Gets or sets the path where the credential file is to be generated |
| 70 | + /// </summary> |
| 71 | + [Parameter(ParameterSetName = ASRParameterSets.ByParam, Mandatory =false, HelpMessage = "The path where the vault credential file is to be created.")] |
| 72 | + // TODO:devsri - add file path validator over here. |
| 73 | + public string Path { get; set; } |
| 74 | + |
| 75 | + #endregion Parameters |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// ProcessRecord of the command. |
| 79 | + /// </summary> |
| 80 | + public override async void ExecuteCmdlet() |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + AzureSubscription subscription = AzureSession.CurrentContext.Subscription; |
| 85 | + |
| 86 | + // Generate certificate |
| 87 | + X509Certificate2 cert = CertUtils.CreateSelfSignedCertificate(VaultCertificateExpiryInHoursForHRM, subscription.Id.ToString(), this.Name); |
| 88 | + |
| 89 | + Utilities.UpdateVaultSettings(new ASRVaultCreds() |
| 90 | + { |
| 91 | + CloudServiceName = this.CloudServiceName, |
| 92 | + ResourceName = this.Name |
| 93 | + }); |
| 94 | + |
| 95 | + // Upload certificate |
| 96 | + UploadCertificateResponse acsDetails = await this.UpdateVaultCertificate(cert); |
| 97 | + |
| 98 | + // Get Channel Integrity key |
| 99 | + string channelIntegrityKey = await this.GetChannelIntegrityKey(); |
| 100 | + |
| 101 | + // Generate file. |
| 102 | + ASRVaultCreds vaultCreds = this.GenerateCredential( |
| 103 | + subscription.Id.ToString(), |
| 104 | + this.Name, |
| 105 | + cert, |
| 106 | + acsDetails, |
| 107 | + channelIntegrityKey, |
| 108 | + this.CloudServiceName); |
| 109 | + |
| 110 | + string filePath = string.IsNullOrEmpty(this.Path) ? Utilities.GetDefaultPath() : this.Path; |
| 111 | + string fileName = this.GenerateFileName(); |
| 112 | + |
| 113 | + // write the content to a file. |
| 114 | + Utilities.WriteToFile<ASRVaultCreds>(vaultCreds, filePath, fileName); |
| 115 | + } |
| 116 | + catch (Exception exception) |
| 117 | + { |
| 118 | + this.HandleException(exception); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private async Task<UploadCertificateResponse> UpdateVaultCertificate(X509Certificate2 cert) |
| 123 | + { |
| 124 | + var certificateArgs = new CertificateArgs() |
| 125 | + { |
| 126 | + Certificate = Convert.ToBase64String(cert.GetRawCertData()), |
| 127 | + ContractVersion = "V2012_12" |
| 128 | + }; |
| 129 | + |
| 130 | + UploadCertificateResponse response = await RecoveryServicesClient.UpdateVaultCertificate(certificateArgs); |
| 131 | + |
| 132 | + return response; |
| 133 | + } |
| 134 | + |
| 135 | + private async Task<string> GetChannelIntegrityKey() |
| 136 | + { |
| 137 | + ResourceExtendedInformation extendedInformation; |
| 138 | + try |
| 139 | + { |
| 140 | + extendedInformation = await RecoveryServicesClient.GetExtendedInfo(); |
| 141 | + } |
| 142 | + catch (Exception) |
| 143 | + { |
| 144 | + //TODO:devsri - Handle specific error rather than generic once |
| 145 | + extendedInformation = new ResourceExtendedInformation(); |
| 146 | + } |
| 147 | + |
| 148 | + ResourceExtendedInfo extendedInfo = Utilities.Deserialize<ResourceExtendedInfo>(extendedInformation.ExtendedInfo); |
| 149 | + |
| 150 | + if (extendedInfo == null) |
| 151 | + { |
| 152 | + ResourceExtendedInformationArgs extendedInfoArgs = extendedInfo.Translate(); |
| 153 | + extendedInformation = await RecoveryServicesClient.CreateExtendedInfo(extendedInfoArgs); |
| 154 | + |
| 155 | + extendedInfo = Utilities.Deserialize<ResourceExtendedInfo>(extendedInformation.ExtendedInfo); |
| 156 | + } |
| 157 | + |
| 158 | + return extendedInfo.ChannelIntegrityKey; |
| 159 | + } |
| 160 | + |
| 161 | + private ASRVaultCreds GenerateCredential(string subscriptionId, string resourceName, X509Certificate2 managementCert, UploadCertificateResponse acsDetails, string channelIntegrityKey, string cloudServiceName) |
| 162 | + { |
| 163 | + string serializedCertifivate = Convert.ToBase64String(managementCert.Export(X509ContentType.Pfx)); |
| 164 | + |
| 165 | + AcsNamespace acsNamespace = new AcsNamespace(acsDetails); |
| 166 | + |
| 167 | + ASRVaultCreds vaultCreds = new ASRVaultCreds( |
| 168 | + subscriptionId, |
| 169 | + resourceName, |
| 170 | + serializedCertifivate, |
| 171 | + acsNamespace, |
| 172 | + channelIntegrityKey, |
| 173 | + cloudServiceName); |
| 174 | + |
| 175 | + return vaultCreds; |
| 176 | + } |
| 177 | + |
| 178 | + private string GenerateFileName() |
| 179 | + { |
| 180 | + string fileName; |
| 181 | + |
| 182 | + if (string.IsNullOrEmpty(this.SiteName)) |
| 183 | + { |
| 184 | + fileName = string.Format("{0}_{1}.VaultCredentials", this.Name, DateTime.UtcNow.ToLongDateString()); |
| 185 | + } |
| 186 | + else |
| 187 | + { |
| 188 | + fileName = string.Format("{0}_{1}_{2}.VaultCredentials", this.SiteName, this.Name, DateTime.UtcNow.ToLongDateString()); |
| 189 | + } |
| 190 | + |
| 191 | + return fileName; |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments