/// <summary>
        /// Changes the Vault context
        /// </summary>
        /// <param name="vault">vault object</param>
        /// <returns>credential object</returns>
        public ASRVaultCreds ChangeVaultContext(ASRVault vault)
        {
            // Update vault settings
            Utilities.UpdateVaultSettings(new ASRVaultCreds()
            {
                ResourceGroupName = vault.ResouceGroupName,
                ResourceName      = vault.Name
            });

            // Get Channel Integrity key
            Task <string> getChannelIntegrityKey = this.GetChannelIntegrityKey();

            getChannelIntegrityKey.Wait();


            // Update vault settings along with Channel integrity key
            Utilities.UpdateVaultSettings(new ASRVaultCreds()
            {
                ResourceGroupName   = vault.ResouceGroupName,
                ResourceName        = vault.Name,
                ChannelIntegrityKey = getChannelIntegrityKey.Result
            });

            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)
        {
            string currentResourceName      = PSRecoveryServicesClient.asrVaultCreds.ResourceName;
            string currentResourceGroupName = PSRecoveryServicesClient.asrVaultCreds.ResourceGroupName;

            // Update vault settings with the working vault to generate file
            Utilities.UpdateVaultSettings(new ASRVaultCreds()
            {
                ResourceGroupName = vault.ResouceGroupName,
                ResourceName      = vault.Name
            });

            // 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);

            // Update back the original vault settings
            Utilities.UpdateVaultSettings(new ASRVaultCreds()
            {
                ResourceGroupName = currentResourceGroupName,
                ResourceName      = currentResourceName
            });

            return(asrVaultCreds);
        }
Exemple #3
0
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        protected override void ProcessRecord()
        {
            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);
            }

            try
            {
                RecoveryServicesClient.ValidateVaultSettings(
                    asrVaultCreds.ResourceName,
                    asrVaultCreds.ResourceGroupName);

                Utilities.UpdateVaultSettings(asrVaultCreds);
                this.WriteObject(new ASRVaultSettings(
                                     asrVaultCreds.ResourceName,
                                     asrVaultCreds.ResourceGroupName));
            }
            catch (Exception exception)
            {
                this.HandleException(exception);
            }
        }