Esempio n. 1
0
        private VirtualMachineDscExtensionContext GetDscExtensionContext(PSVirtualMachineExtension extension)
        {
            var context = new VirtualMachineDscExtensionContext
            {
                ResourceGroupName = extension.ResourceGroupName,
                Name               = extension.Name,
                Location           = extension.Location,
                Etag               = extension.Etag,
                Publisher          = extension.Publisher,
                ExtensionType      = extension.ExtensionType,
                TypeHandlerVersion = extension.TypeHandlerVersion,
                Id                = extension.Id,
                PublicSettings    = extension.PublicSettings,
                ProtectedSettings = extension.ProtectedSettings,
                ProvisioningState = extension.ProvisioningState,
                Statuses          = extension.Statuses
            };

            DscExtensionPublicSettings extensionPublicSettings = null;

            try
            {
                extensionPublicSettings = DscExtensionSettingsSerializer.DeserializePublicSettings(extension.PublicSettings);
            }
            catch (JsonException e)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new JsonException(
                            String.Format(
                                CultureInfo.CurrentUICulture,
                                Microsoft.Azure.Commands.Compute.Properties.Resources.AzureVMDscWrongSettingsFormat,
                                extension.PublicSettings),
                            e),
                        string.Empty,
                        ErrorCategory.ParserError,
                        null));
            }

            if (extensionPublicSettings == null)
            {
                context.ModulesUrl            = string.Empty;
                context.ConfigurationFunction = string.Empty;
                context.Properties            = null;
            }
            else
            {
                context.ModulesUrl            = extensionPublicSettings.ModulesUrl;
                context.ConfigurationFunction = extensionPublicSettings.ConfigurationFunction;
                if (extensionPublicSettings.Properties != null)
                {
                    context.Properties =
                        new Hashtable(extensionPublicSettings.Properties.ToDictionary(x => x.Name, x => x.Value));
                }
            }

            return(context);
        }
        private bool IsExtensionInstalled(OSType currentOSType)
        {
            if (OSType.Windows.Equals(currentOSType))
            {
                this.Name = this.Name ?? AzureDiskEncryptionExtensionContext.ExtensionDefaultName;
            }
            else if (OSType.Linux.Equals(currentOSType))
            {
                this.Name = this.Name ?? AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultName;
            }


            AzureOperationResponse <VirtualMachineExtension> extensionResult = this.VirtualMachineExtensionClient.GetWithInstanceView(this.ResourceGroupName, this.VMName, this.Name);

            if (extensionResult == null)
            {
                return(false);
            }

            PSVirtualMachineExtension returnedExtension = extensionResult.ToPSVirtualMachineExtension(
                this.ResourceGroupName, this.VMName);

            if ((returnedExtension == null) ||
                (string.IsNullOrWhiteSpace(returnedExtension.Publisher)) ||
                (string.IsNullOrWhiteSpace(returnedExtension.ExtensionType)))
            {
                return(false);
            }
            bool publisherMatch = false;

            if (OSType.Linux.Equals(currentOSType))
            {
                if (returnedExtension.Publisher.Equals(this.ExtensionPublisherName ?? AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultPublisher,
                                                       StringComparison.InvariantCultureIgnoreCase) &&
                    returnedExtension.ExtensionType.Equals(this.ExtensionType ?? AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultType,
                                                           StringComparison.InvariantCultureIgnoreCase))
                {
                    publisherMatch = true;
                }
            }
            else if (OSType.Windows.Equals(currentOSType))
            {
                if (returnedExtension.Publisher.Equals(this.ExtensionPublisherName ?? AzureDiskEncryptionExtensionContext.ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
                    returnedExtension.ExtensionType.Equals(this.ExtensionType ?? AzureDiskEncryptionExtensionContext.ExtensionDefaultType, StringComparison.InvariantCultureIgnoreCase))
                {
                    publisherMatch = true;
                }
            }
            if (publisherMatch)
            {
                return(true);
            }

            return(false);
        }
        private VirtualMachineDscExtensionContext GetDscExtensionContext(PSVirtualMachineExtension extension)
        {
            var context = new VirtualMachineDscExtensionContext
            {
                ResourceGroupName = extension.ResourceGroupName,
                Name               = extension.Name,
                Location           = extension.Location,
                Etag               = extension.Etag,
                Publisher          = extension.Publisher,
                ExtensionType      = extension.ExtensionType,
                TypeHandlerVersion = extension.TypeHandlerVersion,
                Id                = extension.Id,
                PublicSettings    = extension.PublicSettings,
                ProtectedSettings = extension.ProtectedSettings,
                ProvisioningState = extension.ProvisioningState,
                Statuses          = extension.Statuses
            };

            DscExtensionPublicSettings publicSettings;

            try
            {
                publicSettings = string.IsNullOrEmpty(extension.PublicSettings) ? null
                                    : JsonConvert.DeserializeObject <DscExtensionPublicSettings>(extension.PublicSettings);
            }
            catch (Exception)
            {
                // Try deserialize as version 1.0
                try
                {
                    var publicSettingsV1 =
                        JsonConvert.DeserializeObject <DscExtensionPublicSettings.Version1>(extension.PublicSettings);
                    publicSettings = publicSettingsV1.ToCurrentVersion();
                }
                catch (JsonException)
                {
                    throw;
                }
            }

            if (publicSettings == null)
            {
                context.ModulesUrl            = string.Empty;
                context.ConfigurationFunction = string.Empty;
                context.Properties            = null;
            }
            else
            {
                context.ModulesUrl            = publicSettings.ModulesUrl;
                context.ConfigurationFunction = publicSettings.ConfigurationFunction;
                context.Properties            = new Hashtable(publicSettings.Properties.ToDictionary(x => x.Name, x => x.Value));
            }

            return(context);
        }
Esempio n. 4
0
        private bool ExtensionProvisioningSucceeded(AzureDiskEncryptionExtensionContext adeExtension)
        {
            VirtualMachineExtensionGetResponse  extensionStatusViewresult = this.VirtualMachineExtensionClient.GetWithInstanceView(this.ResourceGroupName, this.VMName, adeExtension.Name);
            PSVirtualMachineExtension           extensionStatusView       = extensionStatusViewresult.ToPSVirtualMachineExtension(this.ResourceGroupName);
            AzureDiskEncryptionExtensionContext adeExtensionWithStatus    = new AzureDiskEncryptionExtensionContext(extensionStatusView);

            if (adeExtensionWithStatus.ProvisioningState.Equals(AzureDiskEncryptionExtensionContext.StatusSucceeded, StringComparison.InvariantCultureIgnoreCase))
            {
                return(true);
            }

            return(false);
        }
        private VirtualMachineSqlServerExtensionContext GetSqlServerExtensionContext(PSVirtualMachineExtension extension)
        {
            SqlServerPublicSettings extensionPublicSettings = null;
            VirtualMachineSqlServerExtensionContext context = null;

            try
            {
                extensionPublicSettings = string.IsNullOrEmpty(extension.PublicSettings) ? null
                                  : JsonConvert.DeserializeObject <SqlServerPublicSettings>(extension.PublicSettings);

                // #$ISSUE- extension.Statuses is always null, follow up with Azure team
                context = new VirtualMachineSqlServerExtensionContext
                {
                    ResourceGroupName = extension.ResourceGroupName,
                    Name               = extension.Name,
                    Location           = extension.Location,
                    Etag               = extension.Etag,
                    Publisher          = extension.Publisher,
                    ExtensionType      = extension.ExtensionType,
                    TypeHandlerVersion = extension.TypeHandlerVersion,
                    Id                         = extension.Id,
                    PublicSettings             = JsonConvert.SerializeObject(extensionPublicSettings),
                    ProtectedSettings          = extension.ProtectedSettings,
                    ProvisioningState          = extension.ProvisioningState,
                    AutoBackupSettings         = extensionPublicSettings.AutoBackupSettings,
                    AutoPatchingSettings       = extensionPublicSettings.AutoPatchingSettings,
                    KeyVaultCredentialSettings = extensionPublicSettings.KeyVaultCredentialSettings,
                    Statuses                   = extension.Statuses
                };
            }
            catch (JsonException e)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new JsonException(
                            String.Format(
                                CultureInfo.CurrentUICulture,
                                Properties.Resources.AzureVMSqlServerWrongSettingsFormat,
                                extension.PublicSettings),
                            e),
                        string.Empty,
                        ErrorCategory.ParserError,
                        null));
            }

            return(context);
        }
Esempio n. 6
0
        public AzureDiskEncryptionExtensionContext(PSVirtualMachineExtension psExt)
        {
            ResourceGroupName = psExt.ResourceGroupName;
            Name               = psExt.Name;
            Location           = psExt.Location;
            Etag               = psExt.Etag;
            Publisher          = psExt.Publisher;
            ExtensionType      = psExt.ExtensionType;
            TypeHandlerVersion = psExt.TypeHandlerVersion;
            Id                = psExt.Id;
            PublicSettings    = psExt.PublicSettings;
            ProtectedSettings = psExt.ProtectedSettings;
            ProvisioningState = psExt.ProvisioningState;
            Statuses          = psExt.Statuses;

            InitializeAzureDiskEncryptionMembers(psExt);
        }
Esempio n. 7
0
        private void InitializeAzureDiskEncryptionMembers(PSVirtualMachineExtension psExt)
        {
            AzureDiskEncryptionExtensionPublicSettings publicSettings = string.IsNullOrEmpty(psExt.PublicSettings) ? null
                                    : JsonConvert.DeserializeObject <AzureDiskEncryptionExtensionPublicSettings>(psExt.PublicSettings);

            AzureDiskEncryptionExtensionProtectedSettings protectedSettings = string.IsNullOrEmpty(psExt.ProtectedSettings) ? null
                                    : JsonConvert.DeserializeObject <AzureDiskEncryptionExtensionProtectedSettings>(psExt.ProtectedSettings);

            AadClientID             = (publicSettings == null) ? null : publicSettings.AadClientID;
            KeyVaultURL             = (publicSettings == null) ? null : publicSettings.KeyVaultURL;
            KeyEncryptionKeyURL     = (publicSettings == null) ? null : publicSettings.KeyEncryptionKeyURL;
            KeyEncryptionAlgorithm  = (publicSettings == null) ? null : publicSettings.KeyEncryptionAlgorithm;
            VolumeType              = (publicSettings == null) ? null : publicSettings.VolumeType;
            AadClientCertThumbprint = (publicSettings == null) ? null : publicSettings.AadClientCertThumbprint;
            SequenceVersion         = (publicSettings == null) ? null : publicSettings.SequenceVersion;
            AadClientSecret         = (protectedSettings == null) ? null : ConvertStringToSecureString(protectedSettings.AadClientSecret);
            Passphrase              = (protectedSettings == null) ? null : ConvertStringToSecureString(protectedSettings.Passphrase);
        }
Esempio n. 8
0
        private VirtualMachineSqlServerExtensionContext GetSqlServerExtensionContext(PSVirtualMachineExtension extension)
        {
            SqlServerPublicSettings extensionPublicSettings = null;
            VirtualMachineSqlServerExtensionContext context = null;

            // Extract sql configuration information from one of the sub statuses
            if (extension.SubStatuses == null ||
                extension.SubStatuses.FirstOrDefault(s =>
                                                     s.Code.Equals(SqlConfigurationSubStatusCode, StringComparison.InvariantCultureIgnoreCase)) == null)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new Exception(
                            String.Format(
                                CultureInfo.CurrentUICulture,
                                Properties.Resources.AzureVMSqlServerSqlConfigurationNotFound,
                                extension.SubStatuses)),
                        string.Empty,
                        ErrorCategory.ParserError,
                        null));
            }

            string sqlConfiguration = extension.SubStatuses.First(s => s.Code.Equals(SqlConfigurationSubStatusCode, StringComparison.InvariantCultureIgnoreCase)).Message;

            try
            {
                AzureVMSqlServerConfiguration settings = JsonConvert.DeserializeObject <AzureVMSqlServerConfiguration>(sqlConfiguration);

                // #$ISSUE- extension.Statuses is always null, follow up with Azure team
                context = new VirtualMachineSqlServerExtensionContext
                {
                    ResourceGroupName = extension.ResourceGroupName,
                    Name               = extension.Name,
                    Location           = extension.Location,
                    Etag               = extension.Etag,
                    Publisher          = extension.Publisher,
                    ExtensionType      = extension.ExtensionType,
                    TypeHandlerVersion = extension.TypeHandlerVersion,
                    Id                 = extension.Id,
                    PublicSettings     = JsonConvert.SerializeObject(extensionPublicSettings),
                    ProtectedSettings  = extension.ProtectedSettings,
                    ProvisioningState  = extension.ProvisioningState,
                    AutoBackupSettings = settings.AutoBackup == null ? null : new AutoBackupSettings()
                    {
                        Enable                = settings.AutoBackup.Enable,
                        EnableEncryption      = settings.AutoBackup.EnableEncryption,
                        RetentionPeriod       = settings.AutoBackup.RetentionPeriod,
                        StorageUrl            = settings.AutoBackup.StorageAccountUrl,
                        BackupSystemDbs       = settings.AutoBackup.BackupSystemDbs,
                        BackupScheduleType    = string.IsNullOrEmpty(settings.AutoBackup.BackupScheduleType) ? null : AutoBackupScheduleTypeMap[settings.AutoBackup.BackupScheduleType],
                        FullBackupFrequency   = settings.AutoBackup.FullBackupFrequency,
                        FullBackupStartTime   = settings.AutoBackup.FullBackupStartTime,
                        FullBackupWindowHours = settings.AutoBackup.FullBackupWindowHours,
                        LogBackupFrequency    = settings.AutoBackup.LogBackupFrequency
                    },
                    AutoPatchingSettings = settings.AutoPatching == null ? null : new AutoPatchingSettings()
                    {
                        Enable    = settings.AutoPatching.Enable,
                        DayOfWeek = settings.AutoPatching.DayOfWeek,
                        MaintenanceWindowDuration     = settings.AutoPatching.MaintenanceWindowDuration,
                        MaintenanceWindowStartingHour = settings.AutoPatching.MaintenanceWindowStartingHour,
                        PatchCategory = string.IsNullOrEmpty(settings.AutoPatching.PatchCategory) ? null : AutoPatchingCategoryMap[settings.AutoPatching.PatchCategory]
                    },
                    KeyVaultCredentialSettings = settings.AzureKeyVault == null ? null : new KeyVaultCredentialSettings()
                    {
                        Enable      = settings.AzureKeyVault.Enable,
                        Credentials = settings.AzureKeyVault.CredentialsList
                    },
                    AutoTelemetrySettings = settings.AutoTelemetryReport == null ? null : new AutoTelemetrySettings()
                    {
                        Region = settings.AutoTelemetryReport.Location,
                    },
                    Statuses = extension.Statuses
                };
            }
            catch (JsonException e)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new JsonException(
                            String.Format(
                                CultureInfo.CurrentUICulture,
                                Properties.Resources.AzureVMSqlServerWrongConfigFormat,
                                sqlConfiguration),
                            e),
                        string.Empty,
                        ErrorCategory.ParserError,
                        null));
            }

            return(context);
        }
        private string GetExtensionStatusMessage()
        {
            VirtualMachineExtensionGetResponse extensionResult = this.VirtualMachineExtensionClient.GetWithInstanceView(this.ResourceGroupName, this.VMName, this.Name);

            if (extensionResult == null)
            {
                ThrowTerminatingError(new ErrorRecord(new ApplicationFailedException(string.Format(CultureInfo.CurrentUICulture, "Failed to retrieve extension status")),
                                                      "InvalidResult",
                                                      ErrorCategory.InvalidResult,
                                                      null));
            }
            PSVirtualMachineExtension returnedExtension = extensionResult.ToPSVirtualMachineExtension(this.ResourceGroupName);

            if ((returnedExtension == null) ||
                (string.IsNullOrWhiteSpace(returnedExtension.Publisher)) ||
                (string.IsNullOrWhiteSpace(returnedExtension.ExtensionType)))
            {
                ThrowTerminatingError(new ErrorRecord(new ApplicationFailedException(string.Format(CultureInfo.CurrentUICulture, "Missing extension publisher and type info")),
                                                      "InvalidResult",
                                                      ErrorCategory.InvalidResult,
                                                      null));
            }
            bool publisherMatch = false;

            if (string.Equals(currentOSType, "Linux", StringComparison.InvariantCultureIgnoreCase))
            {
                if (returnedExtension.Publisher.Equals(AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
                    returnedExtension.ExtensionType.Equals(AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultName, StringComparison.InvariantCultureIgnoreCase))
                {
                    publisherMatch = true;
                }
            }
            else if (string.Equals(currentOSType, "Windows", StringComparison.InvariantCultureIgnoreCase))
            {
                if (returnedExtension.Publisher.Equals(AzureDiskEncryptionExtensionContext.ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
                    returnedExtension.ExtensionType.Equals(AzureDiskEncryptionExtensionContext.ExtensionDefaultName, StringComparison.InvariantCultureIgnoreCase))
                {
                    publisherMatch = true;
                }
            }
            if (publisherMatch)
            {
                AzureDiskEncryptionExtensionContext context = new AzureDiskEncryptionExtensionContext(returnedExtension);
                if ((context == null) ||
                    (context.Statuses == null) ||
                    (context.Statuses.Count < 1) ||
                    (string.IsNullOrWhiteSpace(context.Statuses[0].Message)))
                {
                    ThrowTerminatingError(new ErrorRecord(new ApplicationFailedException(string.Format(CultureInfo.CurrentUICulture, "Invalid extension status")),
                                                          "InvalidResult",
                                                          ErrorCategory.InvalidResult,
                                                          null));
                }
                return(context.Statuses[0].Message);
            }
            else
            {
                ThrowTerminatingError(new ErrorRecord(new ApplicationFailedException(string.Format(CultureInfo.CurrentUICulture, "Extension publisher and type mismatched")),
                                                      "InvalidResult",
                                                      ErrorCategory.InvalidResult,
                                                      null));
            }
            return(null);
        }
        private string GetExtensionStatusMessage(OSType currentOSType, bool returnSubstatusMessage = false)
        {
            AzureOperationResponse <VirtualMachineExtension> extensionResult = this.VirtualMachineExtensionClient.GetWithInstanceView(this.ResourceGroupName, this.VMName, this.Name);

            if (extensionResult == null)
            {
                ThrowTerminatingError(new ErrorRecord(new ApplicationFailedException(string.Format(CultureInfo.CurrentUICulture, "Failed to retrieve extension status")),
                                                      "InvalidResult",
                                                      ErrorCategory.InvalidResult,
                                                      null));
            }

            PSVirtualMachineExtension returnedExtension = extensionResult.ToPSVirtualMachineExtension(
                this.ResourceGroupName, this.VMName);

            if ((returnedExtension == null) ||
                (string.IsNullOrWhiteSpace(returnedExtension.Publisher)) ||
                (string.IsNullOrWhiteSpace(returnedExtension.ExtensionType)))
            {
                ThrowTerminatingError(new ErrorRecord(new ApplicationFailedException(string.Format(CultureInfo.CurrentUICulture, "Missing extension publisher and type info")),
                                                      "InvalidResult",
                                                      ErrorCategory.InvalidResult,
                                                      null));
            }
            bool publisherMatch = false;

            if (OSType.Linux.Equals(currentOSType))
            {
                if (returnedExtension.Publisher.Equals(this.ExtensionPublisherName ?? AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
                    returnedExtension.ExtensionType.Equals(this.ExtensionType ?? AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultType, StringComparison.InvariantCultureIgnoreCase))
                {
                    publisherMatch = true;
                }
            }
            else if (OSType.Windows.Equals(currentOSType))
            {
                if (returnedExtension.Publisher.Equals(this.ExtensionPublisherName ?? AzureDiskEncryptionExtensionContext.ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
                    returnedExtension.ExtensionType.Equals(this.ExtensionType ?? AzureDiskEncryptionExtensionContext.ExtensionDefaultType, StringComparison.InvariantCultureIgnoreCase))
                {
                    publisherMatch = true;
                }
            }
            if (publisherMatch)
            {
                AzureDiskEncryptionExtensionContext context = new AzureDiskEncryptionExtensionContext(returnedExtension);
                if ((context == null) || (context.Statuses == null) || (context.Statuses.Count < 1))
                {
                    throw new KeyNotFoundException(string.Format(CultureInfo.CurrentUICulture, "Invalid extension status"));
                }

                if (returnSubstatusMessage)
                {
                    if ((context == null) || (context.SubStatuses == null) || (context.SubStatuses.Count < 1))
                    {
                        throw new KeyNotFoundException(string.Format(CultureInfo.CurrentUICulture, "Invalid extension substatus"));
                    }
                    else
                    {
                        return(context.SubStatuses[0].Message);
                    }
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(context.Statuses[0].Message))
                    {
                        return(context.Statuses[0].Message);
                    }
                    else
                    {
                        // if message is empty, fall back to display status
                        return(context.Statuses[0].DisplayStatus);
                    }
                }
            }
            else
            {
                ThrowTerminatingError(new ErrorRecord(new ApplicationFailedException(string.Format(CultureInfo.CurrentUICulture, "Extension publisher and type mismatched")),
                                                      "InvalidResult",
                                                      ErrorCategory.InvalidResult,
                                                      null));
            }
            return(null);
        }