public static SqlVirtualMachineGroup CreateSqlVirtualMachineGroup(SqlVirtualMachineTestContext context, StorageAccount storageAccount, string groupName = null, WsfcDomainProfile profile = null)
        {
            ISqlVirtualMachineGroupsOperations sqlOperations      = context.getSqlClient().SqlVirtualMachineGroups;
            StorageAccountListKeysResult       storageAccountKeys = context.client.storageClient.StorageAccounts.ListKeys(context.resourceGroup.Name, storageAccount.Name);
            IEnumerator <StorageAccountKey>    iter = storageAccountKeys.Keys.GetEnumerator();

            iter.MoveNext();
            string key         = iter.Current.Value;
            string blobAccount = storageAccount.PrimaryEndpoints.Blob;

            if (groupName == null)
            {
                groupName = context.generateResourceName();
            }
            SqlVirtualMachineGroup group = sqlOperations.CreateOrUpdate(context.resourceGroup.Name, groupName, new SqlVirtualMachineGroup
            {
                Location          = context.location,
                SqlImageOffer     = Constants.imageOffer,
                SqlImageSku       = Constants.imageSku,
                WsfcDomainProfile = (profile != null)? profile : new WsfcDomainProfile
                {
                    SqlServiceAccount        = getUsername(Constants.sqlService, Constants.domainName),
                    ClusterOperatorAccount   = getUsername(Constants.adminLogin, Constants.domainName),
                    DomainFqdn               = Constants.domainName + ".com",
                    StorageAccountUrl        = blobAccount,
                    StorageAccountPrimaryKey = key
                }
            });

            group.Validate();
            return(group);
        }
Beispiel #2
0
        public async Task StorageAccountRegenerateKey()
        {
            Sanitizer.AddJsonPathSanitizer("$.keys.[*].value");
            //create storage account and get keys
            string accountName = await CreateValidAccountNameAsync(namePrefix);

            _resourceGroup = await CreateResourceGroupAsync();

            StorageAccountContainer storageAccountContainer = _resourceGroup.GetStorageAccounts();
            StorageAccount          account1 = (await storageAccountContainer.CreateOrUpdateAsync(accountName, GetDefaultStorageAccountParameters())).Value;

            VerifyAccountProperties(account1, true);
            StorageAccountListKeysResult keys = await account1.GetKeysAsync();

            Assert.NotNull(keys);
            StorageAccountKey key2 = keys.Keys.First(
                t => StringComparer.OrdinalIgnoreCase.Equals(t.KeyName, "key2"));

            Assert.NotNull(key2);

            //regenerate key and verify the key's change
            StorageAccountRegenerateKeyParameters keyParameters = new StorageAccountRegenerateKeyParameters("key2");
            StorageAccountListKeysResult          regenKeys     = await account1.RegenerateKeyAsync(keyParameters);

            StorageAccountKey regenKey2 = regenKeys.Keys.First(
                t => StringComparer.OrdinalIgnoreCase.Equals(t.KeyName, "key2"));

            Assert.NotNull(regenKey2);

            //validate the key is different from origin one
            if (Mode != RecordedTestMode.Playback)
            {
                Assert.AreNotEqual(key2.Value, regenKey2.Value);
            }
        }
Beispiel #3
0
        public static async Task <CreateStorageAccountResult> CreateStorageAccount(
            [ActivityTrigger] CreateStorageAccountRequest storageParams,
            ILogger log)
        {
            log.LogWarning("Creating Storage Account in Resource Group: {resourceGroupName}", storageParams.ResourceGroupName);

            var storageClientProvider = Container.GetService <IAzureClientProvider <IStorageManagementClient> >();
            var storageClient         = await storageClientProvider.Get(storageParams.TenantID, storageParams.SubscriptionID);

            var saParams = new StorageAccountCreateParameters
            {
                Sku        = new Microsoft.Azure.Management.Storage.Models.Sku(Microsoft.Azure.Management.Storage.Models.SkuName.StandardGRS),
                Location   = storageParams.AzureResourceLocation,
                Kind       = Kind.Storage,
                Encryption = new Encryption("Microsoft.Storage", new EncryptionServices(blob: new EncryptionService(enabled: true))),
                Tags       = GenerateTags(storageParams)
            };

            StorageAccount sa = await storageClient.StorageAccounts.CreateAsync(storageParams.ResourceGroupName, storageParams.StorageAccountName, saParams);

            log.LogWarning($"Created Storage Account {sa.Name} ({sa.Sku.Name}) in Resource Group: {storageParams.ResourceGroupName}");

            StorageAccountListKeysResult keys = await storageClient.StorageAccounts.ListKeysAsync(storageParams.ResourceGroupName, storageParams.StorageAccountName);

            return(new CreateStorageAccountResult()
            {
                StorageAccountName = storageParams.StorageAccountName,
                PrimaryKey = keys.Keys.FirstOrDefault(),
                BlobUrl = sa.PrimaryEndpoints.Blob
            });
        }
        private async Task <StorageContainerInfo> CreateStorageContainer(SqlManagementTestContext context, ResourceGroup resourceGroup)
        {
            StorageManagementClient storageClient  = context.GetClient <StorageManagementClient>();
            StorageAccount          storageAccount = await storageClient.StorageAccounts.CreateAsync(
                resourceGroup.Name,
                accountName : SqlManagementTestUtilities.GenerateName(prefix: "sqlvatest"),
                parameters : new StorageAccountCreateParameters(
                    new Microsoft.Azure.Management.Storage.Models.Sku(SkuName.StandardLRS, SkuTier.Standard),
                    Kind.BlobStorage,
                    resourceGroup.Location,
                    accessTier: AccessTier.Cool));

            StorageAccountListKeysResult keys =
                storageClient.StorageAccounts.ListKeys(resourceGroup.Name, storageAccount.Name);
            string key = keys.Keys.First().Value;

            string containerName = "vulnerability-assessment";
            var    sasToken      = string.Empty;

            // Create container
            // Since this is a data-plane client and not an ARM client it's harder to inject
            // HttpMockServer into it to record/playback, but that's fine because we don't need
            // any of the response data to continue with the test.
            if (HttpMockServer.Mode == HttpRecorderMode.Record)
            {
                CloudStorageAccount storageAccountClient = new CloudStorageAccount(
                    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                        storageAccount.Name,
                        key),
                    useHttps: true);
                CloudBlobClient    blobClient         = storageAccountClient.CreateCloudBlobClient();
                CloudBlobContainer containerReference = blobClient.GetContainerReference(containerName);
                await containerReference.CreateIfNotExistsAsync();

                SharedAccessBlobPolicy sharedAccessPolicy = new SharedAccessBlobPolicy
                {
                    SharedAccessExpiryTime = new DateTimeOffset(DateTime.UtcNow.Add(TimeSpan.FromHours(1))),
                    Permissions            = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.List
                };

                // Generate the SAS Token
                sasToken = containerReference.GetSharedAccessSignature(sharedAccessPolicy);
            }

            return(new StorageContainerInfo
            {
                StorageAccountSasKey = sasToken,
                StorageContainerPath = new Uri(storageAccount.PrimaryEndpoints.Blob + containerName)
            });
        }
        private async Task <StorageContainerInfo> CreateStorageContainer(SqlManagementTestContext context, ResourceGroup resourceGroup)
        {
            StorageManagementClient storageClient  = context.GetClient <StorageManagementClient>();
            StorageAccount          storageAccount = await storageClient.StorageAccounts.CreateAsync(
                resourceGroup.Name,
                accountName : SqlManagementTestUtilities.GenerateName(prefix: "sqlcrudtest"), // '-' is not allowed
                parameters : new StorageAccountCreateParameters(
                    new Microsoft.Azure.Management.Storage.Models.Sku(SkuName.StandardLRS, SkuTier.Standard),
                    Kind.BlobStorage,
                    resourceGroup.Location,
                    accessTier: AccessTier.Cool));

            StorageAccountListKeysResult keys =
                storageClient.StorageAccounts.ListKeys(resourceGroup.Name, storageAccount.Name);
            string key = keys.Keys.First().Value;

            string containerName = "container";

            // Create container
            // Since this is a data-plane client and not an ARM client it's harder to inject
            // HttpMockServer into it to record/playback, but that's fine because we don't need
            // any of the response data to continue with the test.
            if (HttpMockServer.Mode == HttpRecorderMode.Record)
            {
                CloudStorageAccount storageAccountClient = new CloudStorageAccount(
                    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                        storageAccount.Name,
                        key),
                    useHttps: true);
                CloudBlobClient    blobClient         = storageAccountClient.CreateCloudBlobClient();
                CloudBlobContainer containerReference = blobClient.GetContainerReference(containerName);
                await containerReference.CreateIfNotExistsAsync();
            }

            return(new StorageContainerInfo
            {
                StorageAccountKey = key,
                StorageContainerUri = new Uri(storageAccount.PrimaryEndpoints.Blob + containerName)
            });
        }
Beispiel #6
0
        private async Task <StorageAccountInformation> CreateStorageAccountAsync(SqlManagementTestContext context, ResourceGroup resourceGroup)
        {
            string accountName                     = SqlManagementTestUtilities.GenerateName(prefix: StorageNamePrefix);
            StorageManagementClient client         = context.GetClient <StorageManagementClient>();
            StorageAccount          storageAccount = await client.StorageAccounts.CreateAsync(
                resourceGroup.Name,
                accountName : accountName,
                parameters : new StorageAccountCreateParameters(
                    new Microsoft.Azure.Management.Storage.Models.Sku(SkuName.StandardLRS, SkuTier.Standard),
                    Kind.BlobStorage,
                    resourceGroup.Location,
                    accessTier: AccessTier.Cool));

            StorageAccountListKeysResult keys =
                client.StorageAccounts.ListKeys(resourceGroup.Name, storageAccount.Name);

            return(new StorageAccountInformation
            {
                Name = accountName,
                Endpoint = storageAccount.PrimaryEndpoints.Blob,
                PrimaryKey = keys.Keys.First().Value
            });
        }
Beispiel #7
0
        /// <inheritdoc/>
        public async Task <BlobContainerClient> GetBlobClient(string org, string environment)
        {
            string key = $"{org}-{environment}";

            if (_clients.TryGetValue(key, out BlobContainerClient client))
            {
                return(client);
            }

            try
            {
                _accountConfig.TryGetValue(key, out StorageAccountConfig config);

                if (string.IsNullOrEmpty(config.AccountKey))
                {
                    StorageManagementClient      storageManagementClient = new StorageManagementClient(config.SubscriptionId, _accessTokenService.GetCredential());
                    StorageAccountListKeysResult res = await storageManagementClient.StorageAccounts.ListKeysAsync(config.ResourceGroup, config.AccountName);

                    config.AccountKey = res.Keys.Where(k => k.KeyName == "key1").FirstOrDefault().Value;
                    _accountConfig[key].AccountKey = config.AccountKey;
                }

                string blobEndpoint = $"https://{config.AccountName}.blob.core.windows.net/";

                BlobServiceClient commonBlobClient = new BlobServiceClient(new Uri(blobEndpoint), new StorageSharedKeyCredential(config.AccountName, config.AccountKey));
                client = commonBlobClient.GetBlobContainerClient(config.Container);

                _clients.TryAdd(key, client);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return(client);
        }
Beispiel #8
0
        public static async Task <StorageContainerInfo> CreateStorageContainer(SqlManagementTestContext context, Microsoft.Azure.Management.ResourceManager.Models.ResourceGroup resourceGroup)
        {
            StorageManagementClient storageClient  = context.GetClient <StorageManagementClient>();
            StorageAccount          storageAccount = await storageClient.StorageAccounts.CreateAsync(
                resourceGroup.Name,
                accountName : SqlManagementTestUtilities.GenerateName(prefix: "sqlvatest"),
                parameters : new StorageAccountCreateParameters(
                    new Sku(SkuName.StandardLRS, SkuTier.Standard),
                    Kind.BlobStorage,
                    resourceGroup.Location,
                    accessTier: AccessTier.Cool));

            StorageAccountListKeysResult keys =
                storageClient.StorageAccounts.ListKeys(resourceGroup.Name, storageAccount.Name);
            string key = keys.Keys.First().Value;

            string containerName = "vulnerability-assessment";

            return(new StorageContainerInfo
            {
                StorageAccountAccessKey = key,
                StorageContainerPath = new Uri(storageAccount.PrimaryEndpoints.Blob + containerName)
            });
        }
 public static string GetKey2(this StorageAccountListKeysResult listKeyResult)
 {
     return(listKeyResult.Keys[1].Value);
 }
 public static string GetFirstAvailableKey(this StorageAccountListKeysResult listKeyResult)
 {
     return(listKeyResult.Keys[0].Value);
 }
Beispiel #11
0
        public void ManagedDatabaseExternalBackupRestoreTest()
        {
            using (SqlManagementTestContext Context = new SqlManagementTestContext(this))
            {
                SqlManagementClient     sqlClient     = Context.GetClient <SqlManagementClient>();
                StorageManagementClient storageClient = Context.GetClient <StorageManagementClient>();
                const string            AccountName   = "backupscxteam";

                string testStorageContainerUri      = "https://backupscxteam.blob.core.windows.net/clients";
                string testStorageContainerSasToken = string.Empty;
                StorageAccountListKeysResult keys   =
                    storageClient.StorageAccounts.ListKeys(ManagedInstanceTestUtilities.ResourceGroupName, AccountName);
                string key = keys.Keys.First().Value;

                if (HttpMockServer.Mode == HttpRecorderMode.Record)
                {
                    CloudBlobContainer container = new CloudBlobContainer(
                        new Uri(testStorageContainerUri),
                        new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(AccountName, key));
                    SharedAccessBlobPolicy sharedAccessPolicy = new SharedAccessBlobPolicy
                    {
                        SharedAccessExpiryTime = new DateTimeOffset(DateTime.UtcNow.Add(TimeSpan.FromHours(1))),
                        Permissions            = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.List
                    };
                    testStorageContainerSasToken = container.GetSharedAccessSignature(sharedAccessPolicy);
                }

                ResourceGroup   resourceGroup   = Context.CreateResourceGroup();
                ManagedInstance managedInstance = Context.CreateManagedInstance(resourceGroup);
                string          databaseName    = SqlManagementTestUtilities.GenerateName(_testPrefix);

                // Start external backup restore.
                var db = sqlClient.ManagedDatabases.BeginCreateOrUpdateAsync(
                    resourceGroup.Name,
                    managedInstance.Name,
                    databaseName,
                    new ManagedDatabase()
                {
                    CreateMode               = "RestoreExternalBackup",
                    Location                 = managedInstance.Location,
                    Collation                = managedInstance.Collation,
                    StorageContainerUri      = testStorageContainerUri,
                    StorageContainerSasToken = testStorageContainerSasToken
                }).GetAwaiter().GetResult();

                RetryAction(() =>
                {
                    var success = false;
                    try
                    {
                        var dbs = sqlClient.ManagedDatabases.Get(resourceGroup.Name, managedInstance.Name, databaseName);

                        if (dbs != null)
                        {
                            success = true;
                        }
                    }
                    catch (Exception e)
                    {
                        success = false;
                    }

                    // Sleep if we are running live to avoid hammering the server.
                    // No need to sleep if we are playing back the recording.
                    if (HttpMockServer.Mode == HttpRecorderMode.Record)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(5));
                    }

                    return(success);
                });

                // Wait until restore state is Waiting - this means that all files have been restored from storage container.
                //
                RetryAction(() =>
                {
                    var restoreDetails = sqlClient.ManagedDatabaseRestoreDetails.Get(resourceGroup.Name, managedInstance.Name, databaseName);

                    if (restoreDetails.Status == "Waiting")
                    {
                        return(true);
                    }

                    // Sleep if we are running live to avoid hammering the server.
                    // No need to sleep if we are playing back the recording.
                    if (HttpMockServer.Mode == HttpRecorderMode.Record)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(60));
                    }

                    return(false);
                });

                // Initiate restore complete request.
                //
                sqlClient.ManagedDatabases.BeginCompleteRestore(resourceGroup.Name, managedInstance.Name, databaseName, new CompleteDatabaseRestoreDefinition()
                {
                    LastBackupName = "log2_0"
                });

                // Wait until restore state is Completed.
                //
                RetryAction(() =>
                {
                    var restoreDetails = sqlClient.ManagedDatabaseRestoreDetails.Get(resourceGroup.Name, managedInstance.Name, databaseName);

                    if (restoreDetails.Status == "Completed")
                    {
                        return(true);
                    }

                    // Sleep if we are running live to avoid hammering the server.
                    // No need to sleep if we are playing back the recording.
                    if (HttpMockServer.Mode == HttpRecorderMode.Record)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(30));
                    }

                    return(false);
                });
            }
        }
        public static AvailabilityGroupListener CreateAGListener(SqlVirtualMachineTestContext context, string agListenerName, string groupName)
        {
            MockClient client = context.client;

            string domainName    = Constants.domainName;
            string adminLogin    = Constants.adminLogin;
            string adminPassword = Constants.adminPassword;

            // Create domain
            NetworkSecurityGroup    nsg    = CreateNsg(context);
            VirtualNetwork          vnet   = CreateVirtualNetwork(context, networkSecurityGroup: nsg);
            NetworkInterface        nic    = CreateNetworkInterface(context, virtualNetwork: vnet, networkSecurityGroup: nsg);
            VirtualMachine          vm     = CreateVM(context, nic: nic);
            VirtualMachineExtension domain = CreateDomain(context, vm, domainName, adminLogin, adminPassword);

            Assert.NotNull(domain);

            // Update DNS
            Subnet subnet = vnet.Subnets[0];

            UpdateVnetDNS(context, vnet, nsg, nic, subnet);

            // Create SqlVirtualMachineGroup
            StorageAccount storageAccount = CreateStorageAccount(context);
            StorageAccountListKeysResult    storageAccountKeys = client.storageClient.StorageAccounts.ListKeys(context.resourceGroup.Name, storageAccount.Name);
            IEnumerator <StorageAccountKey> iter = storageAccountKeys.Keys.GetEnumerator();

            iter.MoveNext();
            string            key     = iter.Current.Value;
            WsfcDomainProfile profile = new WsfcDomainProfile()
            {
                ClusterBootstrapAccount  = getUsername(adminLogin, domainName),
                ClusterOperatorAccount   = getUsername(adminLogin, domainName),
                SqlServiceAccount        = getUsername(Constants.sqlService, domainName),
                StorageAccountUrl        = "https://" + storageAccount.Name + ".blob.core.windows.net/",
                StorageAccountPrimaryKey = key,
                DomainFqdn = domainName + ".com",
                OuPath     = ""
            };
            SqlVirtualMachineGroup group = CreateSqlVirtualMachineGroup(context, storageAccount, groupName: groupName, profile: profile);

            // Create availability set
            AvailabilitySet availabilitySet = client.computeClient.AvailabilitySets.CreateOrUpdate(context.resourceGroup.Name, context.generateResourceName(), new AvailabilitySet()
            {
                Location = context.location,
                PlatformFaultDomainCount  = 3,
                PlatformUpdateDomainCount = 2,
                Sku = new Microsoft.Azure.Management.Compute.Models.Sku
                {
                    Name = "Aligned"
                }
            });

            // Create two sql virtual machines
            NetworkInterface nic1 = CreateNetworkInterface(context, virtualNetwork: vnet);
            NetworkInterface nic2 = CreateNetworkInterface(context, virtualNetwork: vnet);

            VirtualMachine vm1 = CreateVM(context, nic: nic1, availabilitySet: availabilitySet);
            VirtualMachine vm2 = CreateVM(context, nic: nic2, availabilitySet: availabilitySet);

            SqlVirtualMachineModel sqlVM1 = prepareMachine(context, group, vm1, domainName, adminLogin, adminPassword);
            SqlVirtualMachineModel sqlVM2 = prepareMachine(context, group, vm2, domainName, adminLogin, adminPassword);

            // Create load balancer
            LoadBalancer loadBalancer = client.networkClient.LoadBalancers.CreateOrUpdate(context.resourceGroup.Name, context.generateResourceName(), new LoadBalancer()
            {
                Location = context.location,
                Sku      = new LoadBalancerSku("Basic"),
                FrontendIPConfigurations = new List <FrontendIPConfiguration> (new FrontendIPConfiguration[]
                {
                    new FrontendIPConfiguration()
                    {
                        Name = "LoadBalancerFrontEnd",
                        PrivateIPAllocationMethod = "Dynamic",
                        Subnet = subnet
                    }
                })
            });

            // Run deployment to create an availability group with the two machines created
            string AgName = "AvGroup";
            VirtualMachineExtension availabilityGroup = context.client.computeClient.VirtualMachineExtensions.CreateOrUpdate(context.resourceGroup.Name, vm1.Name, "agCreation", new VirtualMachineExtension(context.location, name: AgName)
            {
                VirtualMachineExtensionType = "CustomScriptExtension",

                Publisher               = "Microsoft.Compute",
                TypeHandlerVersion      = "1.9",
                AutoUpgradeMinorVersion = true,

                Settings = new CustomScriptExtensionSettings
                {
                    FileUris = new List <string>(new string[] {
                        "https://agtemplatestorage.blob.core.windows.net/templates/Deploy.ps1",
                        "https://agtemplatestorage.blob.core.windows.net/templates/sqlvm6.sql",
                        "https://agtemplatestorage.blob.core.windows.net/templates/sqlvm7.sql",
                        "https://agtemplatestorage.blob.core.windows.net/templates/sqlvm8.sql",
                        "https://agtemplatestorage.blob.core.windows.net/test/sqlvm9.sql",
                        "https://agtemplatestorage.blob.core.windows.net/templates/sqlvm10.sql"
                    }),
                    CommandToExecute = "powershell -ExecutionPolicy Unrestricted -File Deploy.ps1 " + AgName + " " + vm1.Name + " " + vm2.Name + " " + Constants.sqlLogin + " " + adminPassword + " " + domainName + "\\" + Constants.sqlService,
                    ContentVersion   = "1.0.0.0"
                }
            });

            // Create availability group listener
            return(context.getSqlClient().AvailabilityGroupListeners.CreateOrUpdate(context.resourceGroup.Name, group.Name, agListenerName, new AvailabilityGroupListener()
            {
                LoadBalancerConfigurations = new List <LoadBalancerConfiguration>(new LoadBalancerConfiguration[]
                {
                    new LoadBalancerConfiguration()
                    {
                        PrivateIpAddress = new PrivateIPAddress(ipAddress: "10.0.0.11", subnetResourceId: subnet.Id),
                        ProbePort = 59999,
                        LoadBalancerResourceId = loadBalancer.Id,
                        SqlVirtualMachineInstances = new List <string>()
                        {
                            sqlVM1.Id, sqlVM2.Id
                        }
                    }
                }),
                AvailabilityGroupName = AgName,
                Port = 1433
            }));
        }
Beispiel #13
0
 public static string GetKey2(this StorageAccountListKeysResult listKeyResult)
 {
     return(listKeyResult.Keys.ElementAt(1).Value);
 }
Beispiel #14
0
 public static string GetFirstAvailableKey(this StorageAccountListKeysResult listKeyResult)
 {
     return(!string.IsNullOrEmpty(listKeyResult.Keys[0].Value) ? listKeyResult.Keys[0].Value : listKeyResult.Keys[1].Value);
 }