Beispiel #1
0
        /// <summary>
        /// Wait until a certificate has been added to a hosted service.
        /// </summary>
        private void WaitForCertificateToBeAdded(ServiceConfigurationSchema.Certificate certificate)
        {
            Debug.Assert(
                !string.IsNullOrEmpty(_hostedServiceName),
                "_hostedServiceName cannot be null or empty.");

            CertificateList certificates = null;

            do
            {
                Thread.Sleep(TimeSpan.FromMilliseconds(500));
                certificates = RetryCall <CertificateList>(subscription =>
                                                           Channel.ListCertificates(subscription, _hostedServiceName));
            }while (certificates == null || certificates.Count <Certificate>(c => c.Thumbprint.Equals(
                                                                                 certificate.thumbprint, StringComparison.OrdinalIgnoreCase)) < 1);
        }
Beispiel #2
0
        private void UpdateServiceConfigurations(AzureService service, string forwarderName, ServiceConfigurationSchema.Certificate certElement, string encryptedPassword)
        {
            foreach (ServiceConfiguration config in new[] { service.Components.LocalConfig, service.Components.CloudConfig })
            {
                foreach (ServiceConfigurationSchema.RoleSettings role in config.Role)
                {
                    if (role.Certificates == null)
                    {
                        role.Certificates = new ServiceConfigurationSchema.Certificate[0];
                    }

                    ServiceConfigurationSchema.Certificate existingCert = role.Certificates.FirstOrDefault(c => c.name == certElement.name);
                    if (existingCert != null)
                    {
                        // ensure we're referencing the right cert
                        existingCert.thumbprint = certElement.thumbprint;
                    }
                    else
                    {
                        role.Certificates = role.Certificates.Concat(new[] { certElement }).ToArray();
                    }

                    Dictionary <string, string> settings = new Dictionary <string, string>();
                    foreach (ServiceConfigurationSchema.ConfigurationSetting setting in role.ConfigurationSettings)
                    {
                        settings[setting.name] = setting.value;
                    }
                    settings["Microsoft.WindowsAzure.Plugins.RemoteAccess.Enabled"]                  = "true";
                    settings["Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountUsername"]          = Username;
                    settings["Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountEncryptedPassword"] = encryptedPassword;
                    settings["Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountExpiration"]        = (DateTime.Now + TimeSpan.FromDays(365)).ToString("o");

                    if (role.name == forwarderName)
                    {
                        settings["Microsoft.WindowsAzure.Plugins.RemoteForwarder.Enabled"] = "true";
                    }

                    role.ConfigurationSettings = settings.Select(pair => new ServiceConfigurationSchema.ConfigurationSetting {
                        name = pair.Key, value = pair.Value
                    }).ToArray();
                }
            }
        }
Beispiel #3
0
        public void EnableRemoteDesktop()
        {
            Validate.ValidateStringIsNullOrEmpty(Username, "Username");
            if (Password == null)
            {
                throw new ArgumentNullException("Password");
            }

            string plainPassword = GetPlainPassword();

            if (!IsPasswordComplex(plainPassword))
            {
                throw new ArgumentException(Properties.Resources.EnableAzureRemoteDesktopCommand_Enable_NeedComplexPassword);
            }

            AzureService service = new AzureService(GetServiceRootPath(), null);

            WebRole[]    webRoles    = service.Components.Definition.WebRole ?? new WebRole[0];
            WorkerRole[] workerRoles = service.Components.Definition.WorkerRole ?? new WorkerRole[0];

            string forwarderName = GetForwarderName(webRoles, workerRoles);

            RemoveOtherRemoteForwarders(webRoles, workerRoles, forwarderName);
            AddRemoteAccess(webRoles, workerRoles);

            X509Certificate2 cert = ChooseCertificate();

            ServiceConfigurationSchema.Certificate certElement = new ServiceConfigurationSchema.Certificate
            {
                name = "Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption",
                thumbprintAlgorithm = ThumbprintAlgorithmTypes.sha1,
                thumbprint          = cert.Thumbprint
            };
            string encryptedPassword = Encrypt(plainPassword, cert);

            UpdateServiceConfigurations(service, forwarderName, certElement, encryptedPassword);
            service.Components.Save(service.Paths);
        }
        public void EnableRemoteDesktop()
        {
            Validate.ValidateStringIsNullOrEmpty(Username, "Username");
            if (Password == null)
            {
                throw new ArgumentNullException("Password");
            }

            string plainPassword = GetPlainPassword();
            if (!IsPasswordComplex(plainPassword))
            {
                throw new ArgumentException(Properties.Resources.EnableAzureRemoteDesktopCommand_Enable_NeedComplexPassword);
            }

            AzureService service = new AzureService(GetServiceRootPath(), null);
            WebRole[] webRoles = service.Components.Definition.WebRole ?? new WebRole[0];
            WorkerRole[] workerRoles = service.Components.Definition.WorkerRole ?? new WorkerRole[0];

            string forwarderName = GetForwarderName(webRoles, workerRoles);
            RemoveOtherRemoteForwarders(webRoles, workerRoles, forwarderName);
            AddRemoteAccess(webRoles, workerRoles);

            X509Certificate2 cert = ChooseCertificate();
            ServiceConfigurationSchema.Certificate certElement = new ServiceConfigurationSchema.Certificate
            {
                name = "Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption",
                thumbprintAlgorithm = ThumbprintAlgorithmTypes.sha1,
                thumbprint = cert.Thumbprint
            };
            string encryptedPassword = Encrypt(plainPassword, cert);

            UpdateServiceConfigurations(service, forwarderName, certElement, encryptedPassword);
            service.Components.Save(service.Paths);
        }