|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Microsoft.Azure.Commands.Compute.Common; |
| 16 | +using Microsoft.Azure.Commands.Common.Authentication; |
| 17 | +using Microsoft.Azure.Commands.Common.Authentication.Models; |
| 18 | +using Microsoft.Azure.Commands.ResourceManager.Common; |
| 19 | +using Microsoft.Azure.Management.Storage; |
| 20 | +using Microsoft.WindowsAzure.Commands.Common.Storage; |
| 21 | +using System; |
| 22 | +using System.Management.Automation; |
| 23 | +using System.Runtime.InteropServices; |
| 24 | +using System.Security; |
| 25 | +using System.Security.Permissions; |
| 26 | + |
| 27 | +namespace Microsoft.Azure.Commands.Compute |
| 28 | +{ |
| 29 | + /// <summary> |
| 30 | + /// Helper cmdlet to construct instance of AutoBackup settings class |
| 31 | + /// </summary> |
| 32 | + [Cmdlet( |
| 33 | + VerbsCommon.New, |
| 34 | + ProfileNouns.VirtualMachineSqlServerAutoBackupConfig, |
| 35 | + DefaultParameterSetName = StorageUriParamSetName), |
| 36 | + OutputType( |
| 37 | + typeof(AutoBackupSettings))] |
| 38 | + public class NewAzureRmVMSqlServerAutoBackupConfigCommand : AzureRMCmdlet |
| 39 | + { |
| 40 | + protected const string StorageContextParamSetName = "StorageContextSqlServerAutoBackup"; |
| 41 | + protected const string StorageUriParamSetName = "StorageUriSqlServerAutoBackup"; |
| 42 | + |
| 43 | + [Parameter( |
| 44 | + Mandatory = true, |
| 45 | + Position = 0, |
| 46 | + ValueFromPipelineByPropertyName = true, |
| 47 | + HelpMessage = "The resource group name.")] |
| 48 | + [ValidateNotNullOrEmpty] |
| 49 | + public string ResourceGroupName { get; set; } |
| 50 | + |
| 51 | + [Parameter( |
| 52 | + Mandatory = false, |
| 53 | + Position = 1, |
| 54 | + ValueFromPipelineByPropertyName = true, |
| 55 | + HelpMessage = "Enable Automatic Backup.")] |
| 56 | + [ValidateNotNullOrEmpty] |
| 57 | + public SwitchParameter Enable { get; set; } |
| 58 | + |
| 59 | + [Parameter( |
| 60 | + Mandatory = false, |
| 61 | + Position = 2, |
| 62 | + ValueFromPipelineByPropertyName = true, |
| 63 | + HelpMessage = "Backup Retention period in days.")] |
| 64 | + [ValidateNotNullOrEmpty] |
| 65 | + public int RetentionPeriodInDays { get; set; } |
| 66 | + |
| 67 | + [Parameter( |
| 68 | + Mandatory = false, |
| 69 | + Position = 3, |
| 70 | + ValueFromPipelineByPropertyName = true, |
| 71 | + HelpMessage = "Enable Backup Encryption.")] |
| 72 | + [ValidateNotNullOrEmpty] |
| 73 | + public SwitchParameter EnableEncryption { get; set; } |
| 74 | + |
| 75 | + [Parameter( |
| 76 | + Mandatory = false, |
| 77 | + Position = 4, |
| 78 | + ValueFromPipelineByPropertyName = false, |
| 79 | + HelpMessage = "Certificate password.")] |
| 80 | + public SecureString CertificatePassword { get; set; } |
| 81 | + |
| 82 | + [Parameter( |
| 83 | + ParameterSetName = StorageContextParamSetName, |
| 84 | + Mandatory = false, |
| 85 | + Position = 5, |
| 86 | + ValueFromPipelineByPropertyName = true, |
| 87 | + HelpMessage = "The storage connection context")] |
| 88 | + [ValidateNotNullOrEmpty] |
| 89 | + public AzureStorageContext StorageContext |
| 90 | + { |
| 91 | + get; |
| 92 | + set; |
| 93 | + } |
| 94 | + |
| 95 | + [Parameter( |
| 96 | + Mandatory = false, |
| 97 | + Position = 4, |
| 98 | + ValueFromPipelineByPropertyName = true, |
| 99 | + HelpMessage = "The storage uri")] |
| 100 | + [ValidateNotNullOrEmpty] |
| 101 | + public Uri StorageUri |
| 102 | + { |
| 103 | + get; |
| 104 | + set; |
| 105 | + } |
| 106 | + |
| 107 | + [Parameter( |
| 108 | + Mandatory = false, |
| 109 | + Position = 5, |
| 110 | + ValueFromPipelineByPropertyName = true, |
| 111 | + HelpMessage = "The storage access key")] |
| 112 | + [ValidateNotNullOrEmpty] |
| 113 | + public SecureString StorageKey |
| 114 | + { |
| 115 | + get; |
| 116 | + set; |
| 117 | + } |
| 118 | + |
| 119 | + [Parameter( |
| 120 | + Mandatory = false, |
| 121 | + Position = 6, |
| 122 | + ValueFromPipelineByPropertyName = true, |
| 123 | + HelpMessage = "Backup system databases")] |
| 124 | + public SwitchParameter BackupSystemDbs |
| 125 | + { |
| 126 | + get; |
| 127 | + set; |
| 128 | + } |
| 129 | + |
| 130 | + [Parameter( |
| 131 | + Mandatory = false, |
| 132 | + Position = 7, |
| 133 | + ValueFromPipelineByPropertyName = true, |
| 134 | + HelpMessage = "Backup schedule type, manual or automated")] |
| 135 | + [ValidateSet(ValidateSetValues.Manual, ValidateSetValues.Automated, IgnoreCase = true)] |
| 136 | + public string BackupScheduleType |
| 137 | + { |
| 138 | + get; |
| 139 | + set; |
| 140 | + } |
| 141 | + |
| 142 | + [Parameter( |
| 143 | + Mandatory = false, |
| 144 | + Position = 8, |
| 145 | + ValueFromPipelineByPropertyName = true, |
| 146 | + HelpMessage = "Sql Server Full Backup frequency, daily or weekly")] |
| 147 | + [ValidateSet(ValidateSetValues.Daily, ValidateSetValues.Weekly, IgnoreCase = true)] |
| 148 | + public string FullBackupFrequency |
| 149 | + { |
| 150 | + get; |
| 151 | + set; |
| 152 | + } |
| 153 | + |
| 154 | + [Parameter( |
| 155 | + Mandatory = false, |
| 156 | + Position = 9, |
| 157 | + ValueFromPipelineByPropertyName = true, |
| 158 | + HelpMessage = "Hour of the day (0-23) when the Sql Server Full Backup should start")] |
| 159 | + [ValidateRange(0,23)] |
| 160 | + public int FullBackupStartHour |
| 161 | + { |
| 162 | + get; |
| 163 | + set; |
| 164 | + } |
| 165 | + |
| 166 | + [Parameter( |
| 167 | + Mandatory = false, |
| 168 | + Position = 10, |
| 169 | + ValueFromPipelineByPropertyName = true, |
| 170 | + HelpMessage = "Sql Server Full Backup window in hours")] |
| 171 | + [ValidateRange(1, 23)] |
| 172 | + public int FullBackupWindowInHours |
| 173 | + { |
| 174 | + get; |
| 175 | + set; |
| 176 | + } |
| 177 | + |
| 178 | + [Parameter( |
| 179 | + Mandatory = false, |
| 180 | + Position = 11, |
| 181 | + ValueFromPipelineByPropertyName = true, |
| 182 | + HelpMessage = "Sql Server Log Backup frequency, once every 1-60 minutes")] |
| 183 | + [ValidateRange(1, 60)] |
| 184 | + public int LogBackupFrequencyInMinutes |
| 185 | + { |
| 186 | + get; |
| 187 | + set; |
| 188 | + } |
| 189 | + |
| 190 | + /// <summary> |
| 191 | + /// Initialzies a new instance of the <see cref="NewAzureVMSqlServerAutoBackupConfigCommand"/> class. |
| 192 | + /// </summary> |
| 193 | + public NewAzureRmVMSqlServerAutoBackupConfigCommand() |
| 194 | + { |
| 195 | + } |
| 196 | + |
| 197 | + /// <summary> |
| 198 | + /// Creates and returns <see cref="AutoBackupSettings"/> object. |
| 199 | + /// </summary> |
| 200 | + protected override void ProcessRecord() |
| 201 | + { |
| 202 | + AutoBackupSettings autoBackupSettings = new AutoBackupSettings(); |
| 203 | + |
| 204 | + autoBackupSettings.Enable = (Enable.IsPresent) ? Enable.ToBool() : false; |
| 205 | + autoBackupSettings.EnableEncryption = (EnableEncryption.IsPresent) ? EnableEncryption.ToBool() : false; |
| 206 | + autoBackupSettings.RetentionPeriod = RetentionPeriodInDays; |
| 207 | + |
| 208 | + switch (ParameterSetName) |
| 209 | + { |
| 210 | + case StorageContextParamSetName: |
| 211 | + autoBackupSettings.StorageUrl = StorageContext.BlobEndPoint; |
| 212 | + autoBackupSettings.StorageAccessKey = this.GetStorageKey(); |
| 213 | + break; |
| 214 | + |
| 215 | + case StorageUriParamSetName: |
| 216 | + autoBackupSettings.StorageUrl = (StorageUri == null) ? null : StorageUri.ToString(); |
| 217 | + autoBackupSettings.StorageAccessKey = (StorageKey == null) ? null : ConvertToUnsecureString(StorageKey); |
| 218 | + break; |
| 219 | + } |
| 220 | + |
| 221 | + // Check if certificate password was set |
| 222 | + autoBackupSettings.Password = (CertificatePassword == null) ? null : ConvertToUnsecureString(CertificatePassword); |
| 223 | + |
| 224 | + autoBackupSettings.BackupSystemDbs = BackupSystemDbs.IsPresent ? BackupSystemDbs.ToBool() : false; |
| 225 | + autoBackupSettings.BackupScheduleType = BackupScheduleType; |
| 226 | + |
| 227 | + // Set other Backup schedule settings only if BackUpSchedule type is Manual. |
| 228 | + if (!string.IsNullOrEmpty(BackupScheduleType) && string.Equals(BackupScheduleType, ValidateSetValues.Manual, StringComparison.InvariantCultureIgnoreCase)) { |
| 229 | + autoBackupSettings.FullBackupFrequency = FullBackupFrequency; |
| 230 | + autoBackupSettings.FullBackupStartTime = FullBackupStartHour; |
| 231 | + autoBackupSettings.FullBackupWindowHours = FullBackupWindowInHours; |
| 232 | + autoBackupSettings.LogBackupFrequency = LogBackupFrequencyInMinutes; |
| 233 | + } |
| 234 | + |
| 235 | + WriteObject(autoBackupSettings); |
| 236 | + } |
| 237 | + |
| 238 | + protected string GetStorageKey() |
| 239 | + { |
| 240 | + string storageKey = string.Empty; |
| 241 | + string storageName = (this.StorageContext == null) ? null : this.StorageContext.StorageAccountName; |
| 242 | + |
| 243 | + if (!string.IsNullOrEmpty(storageName)) |
| 244 | + { |
| 245 | + var storageClient = AzureSession.ClientFactory.CreateArmClient<StorageManagementClient>(DefaultProfile.Context, |
| 246 | + AzureEnvironment.Endpoint.ResourceManager); |
| 247 | + |
| 248 | + var storageAccount = storageClient.StorageAccounts.GetProperties(this.ResourceGroupName, storageName); |
| 249 | + |
| 250 | + if (storageAccount != null) |
| 251 | + { |
| 252 | + var keys = storageClient.StorageAccounts.ListKeys(this.ResourceGroupName, storageName); |
| 253 | + |
| 254 | + if (keys != null) |
| 255 | + { |
| 256 | + storageKey = !string.IsNullOrEmpty(keys.Key1) ? |
| 257 | + keys.Key1 : |
| 258 | + keys.Key2; |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + return storageKey; |
| 264 | + } |
| 265 | + |
| 266 | + /// <summary> |
| 267 | + /// convert secure string to regular string |
| 268 | + /// $Issue - for ARM cmdlets, check if there is a similair helper class library like Microsoft.WindowsAzure.Commands.ServiceManagement.Helpers |
| 269 | + /// </summary> |
| 270 | + /// <param name="securePassword"></param> |
| 271 | + /// <returns></returns> |
| 272 | + [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)] |
| 273 | + private static string ConvertToUnsecureString(SecureString securePassword) |
| 274 | + { |
| 275 | + if (securePassword == null) |
| 276 | + { |
| 277 | + throw new ArgumentNullException("securePassword"); |
| 278 | + } |
| 279 | + |
| 280 | + IntPtr unmanagedString = IntPtr.Zero; |
| 281 | + try |
| 282 | + { |
| 283 | + unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(securePassword); |
| 284 | + return Marshal.PtrToStringUni(unmanagedString); |
| 285 | + } |
| 286 | + finally |
| 287 | + { |
| 288 | + Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString); |
| 289 | + } |
| 290 | + } |
| 291 | + } |
| 292 | +} |
0 commit comments