CreateSelfSignedCertificate() публичный статический Метод

Method to generate a self signed certificate
public static CreateSelfSignedCertificate ( int validForHours, string subscriptionId, string certificateNamePrefix, string issuer = DefaultIssuer, string password = DefaultPassword ) : X509Certificate2
validForHours int number of hours for which the certificate is valid.
subscriptionId string subscriptionId in question
certificateNamePrefix string prefix for the certificate name
issuer string issuer for the certificate
password string certificate password
Результат System.Security.Cryptography.X509Certificates.X509Certificate2
        /// <summary>
        /// Method to execute the command
        /// </summary>
        private void GetVaultSettingsFile()
        {
            AzureSubscription subscription = DefaultProfile.Context.Subscription;

            // Generate certificate
            X509Certificate2 cert = CertUtils.CreateSelfSignedCertificate(VaultCertificateExpiryInHoursForHRM, subscription.Id.ToString(), this.Vault.Name);

            ASRSite site = new ASRSite();

            if (!string.IsNullOrEmpty(this.SiteIdentifier) && !string.IsNullOrEmpty(this.SiteFriendlyName))
            {
                site.ID   = this.SiteIdentifier;
                site.Name = this.SiteFriendlyName;
            }

            // Generate file.
            ASRVaultCreds vaultCreds = RecoveryServicesClient.GenerateVaultCredential(
                cert,
                this.Vault,
                site);

            string filePath = string.IsNullOrEmpty(this.Path) ? Utilities.GetDefaultPath() : this.Path;
            string fileName = this.GenerateFileName();

            // write the content to a file.
            VaultSettingsFilePath output = new VaultSettingsFilePath()
            {
                FilePath = Utilities.WriteToFile <ASRVaultCreds>(vaultCreds, filePath, fileName)
            };

            // print the path to the user.
            this.WriteObject(output, true);
        }
Пример #2
0
        /// <summary>
        /// Method to execute the command
        /// </summary>
        private void GetByObject()
        {
            IAzureSubscription subscription = this.Profile.Context.Subscription;

            this.Vault.SubscriptionId = subscription.Id.ToString();

            CloudService cloudService = RecoveryServicesClient.GetCloudServiceForVault(this.Vault);

            this.Vault.CloudServiceName = cloudService.Name;

            // Generate certificate
            X509Certificate2 cert = CertUtils.CreateSelfSignedCertificate(VaultCertificateExpiryInHoursForHRM, subscription.Id.ToString(), this.Vault.Name);

            var site = new Site();

            if (this.Site != null)
            {
                site.ID   = this.Site.ID;
                site.Name = this.Site.Name;
                site.Type = this.Site.Type;
            }

            // Generate file.
            ASRVaultCreds vaultCreds = RecoveryServicesClient.GenerateVaultCredential(
                cert,
                this.Vault,
                site);

            string filePath = string.IsNullOrEmpty(this.Path) ? Utilities.GetDefaultPath() : this.Path;
            string fileName = this.GenerateFileName();

            // write the content to a file.
            VaultSettingsFilePath output = new VaultSettingsFilePath()
            {
                FilePath = Utilities.WriteToFile <ASRVaultCreds>(vaultCreds, filePath, fileName)
            };

            // print the path to the user.
            this.WriteObject(output, true);
        }
        /// <summary>
        /// Get vault credentials for backup vault type.
        /// </summary>
        public void GetAzureRMRecoveryServicesVaultBackupCredentials()
        {
            string targetLocation = string.IsNullOrEmpty(this.Path) ? Utilities.GetDefaultPath() : this.Path;

            if (!Directory.Exists(targetLocation))
            {
                throw new ArgumentException(Resources.VaultCredPathException);
            }

            string subscriptionId = DefaultContext.Subscription.Id.ToString();
            string displayName    = this.Vault.Name;

            WriteDebug(string.Format(CultureInfo.InvariantCulture,
                                     Resources.ExecutingGetVaultCredCmdlet,
                                     subscriptionId, this.Vault.ResourceGroupName, this.Vault.Name, targetLocation));

            // Generate certificate
            X509Certificate2 cert = CertUtils.CreateSelfSignedCertificate(
                VaultCertificateExpiryInHoursForBackup, subscriptionId.ToString(), this.Vault.Name);

            VaultCertificateResponse vaultCertificateResponse = null;
            string channelIntegrityKey = string.Empty;

            try
            {
                // Upload cert into ID Mgmt
                WriteDebug(string.Format(CultureInfo.InvariantCulture, Resources.UploadingCertToIdmgmt));
                vaultCertificateResponse = UploadCert(cert);
                WriteDebug(string.Format(CultureInfo.InvariantCulture, Resources.UploadedCertToIdmgmt));
            }
            catch (Exception exception)
            {
                throw exception;
            }

            // generate vault credentials
            string vaultCredsFileContent = GenerateVaultCreds(cert, subscriptionId, vaultCertificateResponse);

            // NOTE: One of the scenarios for this cmdlet is to generate a file which will be an input
            //       to DPM servers.
            //       We found a bug in the DPM UI which is looking for a particular namespace in the input file.
            //       The below is a hack to circumvent this issue and this would be removed once the bug can be fixed.
            vaultCredsFileContent = vaultCredsFileContent.Replace("Microsoft.Azure.Commands.AzureBackup.Models",
                                                                  "Microsoft.Azure.Portal.RecoveryServices.Models.Common");

            // prepare for download
            string fileName = string.Format("{0}_{1:ddd MMM dd yyyy}.VaultCredentials", displayName, DateTime.UtcNow);
            string filePath = System.IO.Path.Combine(targetLocation, fileName);

            WriteDebug(string.Format(Resources.SavingVaultCred, filePath));

            File.WriteAllBytes(filePath, Encoding.UTF8.GetBytes(vaultCredsFileContent));

            VaultSettingsFilePath output = new VaultSettingsFilePath()
            {
                FilePath = filePath,
            };

            // Output filename back to user
            WriteObject(output);
        }
        /// <summary>
        /// Method to execute the command
        /// </summary>
        private void GetSiteRecoveryCredentials()
        {
            IAzureSubscription subscription = DefaultProfile.DefaultContext.Subscription;

            // Generate certificate
            X509Certificate2 cert = CertUtils.CreateSelfSignedCertificate(
                VaultCertificateExpiryInHoursForHRM,
                subscription.Id.ToString(),
                this.Vault.Name);

            ASRSite site = new ASRSite();

            if (!string.IsNullOrEmpty(this.SiteIdentifier) &&
                !string.IsNullOrEmpty(this.SiteFriendlyName))
            {
                site.ID   = this.SiteIdentifier;
                site.Name = this.SiteFriendlyName;
            }

            string fileName = this.GenerateFileName();

            string filePath = string.IsNullOrEmpty(this.Path) ? Utilities.GetDefaultPath() : this.Path;

            // Generate file.
            if (RecoveryServicesClient.getVaultAuthType(this.Vault.ResourceGroupName, this.Vault.Name) == 0)
            {
                ASRVaultCreds vaultCreds = RecoveryServicesClient.GenerateVaultCredential(
                    cert,
                    this.Vault,
                    site,
                    AuthType.ACS);

                // write the content to a file.
                VaultSettingsFilePath output = new VaultSettingsFilePath()
                {
                    FilePath = Utilities.WriteToFile <ASRVaultCreds>(vaultCreds, filePath, fileName)
                };

                // print the path to the user.
                this.WriteObject(output, true);
            }
            else
            {
                string fullFilePath = System.IO.Path.Combine(filePath, fileName);
                WriteDebug(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        Resources.ExecutingGetVaultCredCmdlet,
                        subscription.Id,
                        this.Vault.ResourceGroupName,
                        this.Vault.Name,
                        fullFilePath));

                VaultCertificateResponse vaultCertificateResponse = null;
                try
                {
                    // Upload cert into ID Mgmt
                    WriteDebug(string.Format(CultureInfo.InvariantCulture, Resources.UploadingCertToIdmgmt));
                    vaultCertificateResponse = UploadCert(cert);
                    WriteDebug(string.Format(CultureInfo.InvariantCulture, Resources.UploadedCertToIdmgmt));

                    // generate vault credentials
                    string vaultCredsFileContent = GenerateVaultCredsForSiteRecovery(
                        cert,
                        subscription.Id,
                        vaultCertificateResponse,
                        site);

                    WriteDebug(string.Format(Resources.SavingVaultCred, fullFilePath));

                    File.WriteAllBytes(fullFilePath, Encoding.UTF8.GetBytes(vaultCredsFileContent));

                    VaultSettingsFilePath output = new VaultSettingsFilePath()
                    {
                        FilePath = fullFilePath,
                    };

                    // Output filename back to user
                    WriteObject(output, true);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
        }