/// <summary> /// Changes the Vault context /// </summary> /// <param name="vault">vault object</param> /// <returns>credential object</returns> public ASRVaultCreds ChangeVaultContext(ASRVault vault) { string resourceProviderNamespace = string.Empty; string resourceType = string.Empty; Utilities.GetResourceProviderNamespaceAndType(vault.ID, out resourceProviderNamespace, out resourceType); Utilities.UpdateCurrentVaultContext(new ASRVaultCreds() { ResourceGroupName = vault.ResourceGroupName, ResourceName = vault.Name, ResourceNamespace = resourceProviderNamespace, ARMResourceType = resourceType }); // Get Channel Integrity key Task <string> getChannelIntegrityKey = this.GetChannelIntegrityKey(); getChannelIntegrityKey.Wait(); // Update vault settings along with Channel integrity key Utilities.UpdateCurrentVaultContext(new ASRVaultCreds() { ResourceGroupName = vault.ResourceGroupName, ResourceName = vault.Name, ChannelIntegrityKey = getChannelIntegrityKey.Result, ResourceNamespace = resourceProviderNamespace, ARMResourceType = resourceType }); return(asrVaultCreds); }
/// <summary> /// Gets the vault credential object /// </summary> /// <param name="managementCert">certificate to be uploaded</param> /// <param name="vault">vault object</param> /// <returns>credential object</returns> public ASRVaultCreds GenerateVaultCredential(X509Certificate2 managementCert, ASRVault vault, ASRSite site) { ASRVaultCreds currentVaultContext = new ASRVaultCreds( PSRecoveryServicesClient.asrVaultCreds.SubscriptionId, PSRecoveryServicesClient.asrVaultCreds.ResourceName, PSRecoveryServicesClient.asrVaultCreds.ManagementCert, PSRecoveryServicesClient.asrVaultCreds.AcsNamespace, PSRecoveryServicesClient.asrVaultCreds.ChannelIntegrityKey, PSRecoveryServicesClient.asrVaultCreds.ResourceGroupName, PSRecoveryServicesClient.asrVaultCreds.SiteId, PSRecoveryServicesClient.asrVaultCreds.SiteName, PSRecoveryServicesClient.asrVaultCreds.ResourceNamespace, PSRecoveryServicesClient.asrVaultCreds.ResourceType); string resourceProviderNamespace = string.Empty; string resourceType = string.Empty; Utilities.GetResourceProviderNamespaceAndType(vault.ID, out resourceProviderNamespace, out resourceType); // Update vault settings with the working vault to generate file Utilities.UpdateCurrentVaultContext(new ASRVaultCreds() { ResourceGroupName = vault.ResourceGroupName, ResourceName = vault.Name, ResourceNamespace = resourceProviderNamespace, ARMResourceType = resourceType }); // Get Channel Integrity key string channelIntegrityKey; Task <string> getChannelIntegrityKey = this.GetChannelIntegrityKey(); // Making sure we can generate the file, once the SDK and portal are inter-operable // upload certificate and fetch of ACIK can be made parallel to improvve the performace. getChannelIntegrityKey.Wait(); // Upload certificate UploadCertificateResponse acsDetails; Task <UploadCertificateResponse> uploadCertificate = this.UpdateVaultCertificate(managementCert); uploadCertificate.Wait(); acsDetails = uploadCertificate.Result; channelIntegrityKey = getChannelIntegrityKey.Result; ASRVaultCreds asrVaultCreds = this.GenerateCredentialObject( managementCert, acsDetails, channelIntegrityKey, vault, site); // Update back the original vault settings Utilities.UpdateCurrentVaultContext(currentVaultContext); return(asrVaultCreds); }
/// <summary> /// Initializes a new instance of the <see cref="PSRecoveryServicesClient" /> class with /// required current subscription. /// </summary> /// <param name="azureSubscription">Azure Subscription</param> public PSRecoveryServicesClient(IAzureProfile azureProfile) { System.Configuration.Configuration siteRecoveryConfig = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location); System.Configuration.AppSettingsSection appSettings = (System.Configuration.AppSettingsSection)siteRecoveryConfig.GetSection("appSettings"); string resourceNamespace = string.Empty; string resourceType = string.Empty; // Get Resource provider namespace and type from config only if Vault context is not set // (hopefully it is required only for Vault related cmdlets) if (string.IsNullOrEmpty(asrVaultCreds.ResourceNamespace) || string.IsNullOrEmpty(asrVaultCreds.ARMResourceType)) { if (appSettings.Settings.Count == 0) { resourceNamespace = "Microsoft.SiteRecovery"; // ProviderNameSpace for Production is taken as default resourceType = ARMResourceTypeConstants.SiteRecoveryVault; } else { resourceNamespace = null == appSettings.Settings["ProviderNamespace"] ? "Microsoft.SiteRecovery" : appSettings.Settings["ProviderNamespace"].Value; resourceType = null == appSettings.Settings["ResourceType"] ? ARMResourceTypeConstants.SiteRecoveryVault : appSettings.Settings["ResourceType"].Value; } Utilities.UpdateCurrentVaultContext(new ASRVaultCreds() { ResourceNamespace = resourceNamespace, ARMResourceType = resourceType }); } if (null == endPointUri) { if (appSettings.Settings.Count == 0) { endPointUri = azureProfile.Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager); } else { if (null == appSettings.Settings["RDFEProxy"]) { endPointUri = azureProfile.Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager); } else { // Setting Endpoint to RDFE Proxy if (null == ServicePointManager.ServerCertificateValidationCallback) { ServicePointManager.ServerCertificateValidationCallback = IgnoreCertificateErrorHandler; } endPointUri = new Uri(appSettings.Settings["RDFEProxy"].Value); } } } cloudCredentials = AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(azureProfile.Context); this.recoveryServicesClient = AzureSession.ClientFactory.CreateCustomClient <SiteRecoveryVaultManagementClient>( asrVaultCreds.ResourceNamespace, asrVaultCreds.ARMResourceType, cloudCredentials, azureProfile.Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)); }
/// <summary> /// ProcessRecord of the command. /// </summary> public override void ExecuteSiteRecoveryCmdlet() { base.ExecuteSiteRecoveryCmdlet(); this.WriteVerbose("Vault Settings File path: " + this.Path); ASRVaultCreds asrVaultCreds = null; if (File.Exists(this.Path)) { try { var serializer = new DataContractSerializer(typeof(ASRVaultCreds)); using (var s = new FileStream( this.Path, FileMode.Open, FileAccess.Read, FileShare.Read)) { asrVaultCreds = (ASRVaultCreds)serializer.ReadObject(s); } } catch (XmlException xmlException) { throw new XmlException( string.Format(Properties.Resources.InvalidXml, xmlException)); } catch (SerializationException serializationException) { throw new SerializationException( string.Format(Properties.Resources.InvalidXml, serializationException)); } } else { throw new FileNotFoundException( Properties.Resources.VaultSettingFileNotFound, this.Path); } // Validate required parameters taken from the Vault settings file. if (string.IsNullOrEmpty(asrVaultCreds.ResourceName)) { throw new ArgumentException( Properties.Resources.ResourceNameNullOrEmpty, asrVaultCreds.ResourceName); } if (string.IsNullOrEmpty(asrVaultCreds.ResourceGroupName)) { throw new ArgumentException( Properties.Resources.CloudServiceNameNullOrEmpty, asrVaultCreds.ResourceGroupName); } Utilities.UpdateCurrentVaultContext(asrVaultCreds); RecoveryServicesClient.ValidateVaultSettings( asrVaultCreds.ResourceName, asrVaultCreds.ResourceGroupName); this.WriteObject(new ASRVaultSettings(asrVaultCreds)); }