Esempio n. 1
0
        public void SetupEnvironment()
        {
            base.SetupEnvironment(AzureModule.AzureResourceManager);

            TestEnvironment csmEnvironment = new CSMTestEnvironmentFactory().GetTestEnvironment();

            if (csmEnvironment.SubscriptionId != null)
            {
                //Overwrite the default subscription and default account
                //with ones using user ID and tenant ID from auth context
                var user     = GetUser(csmEnvironment);
                var tenantId = GetTenantId(csmEnvironment);

                var testSubscription = new AzureSubscription()
                {
                    Id   = csmEnvironment.SubscriptionId,
                    Name = AzureRmProfileProvider.Instance.Profile.DefaultContext.Subscription.Name,
                };
                testSubscription.SetAccount(user);
                testSubscription.SetEnvironment(AzureRmProfileProvider.Instance.Profile.DefaultContext.Environment.Name);
                testSubscription.SetDefault();
                testSubscription.SetStorageAccount(Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT"));
                testSubscription.SetTenant(tenantId);
                var testAccount = new AzureAccount()
                {
                    Id   = user,
                    Type = AzureAccount.AccountType.User,
                };
                testAccount.SetSubscriptions(csmEnvironment.SubscriptionId);
                AzureRmProfileProvider.Instance.Profile.DefaultContext = new AzureContext(testSubscription, testAccount, AzureRmProfileProvider.Instance.Profile.DefaultContext.Environment, new AzureTenant {
                    Id = tenantId
                });
            }
        }
Esempio n. 2
0
        public void CanGetJobSubmissionAccessTokenCredentialFromCurrentSubscription()
        {
            var getClustersCommand = new GetAzureHDInsightJobCommand();
            var waSubscription     = new AzureSubscription()
            {
                Id = IntegrationTestBase.TestCredentials.SubscriptionId.ToString(),
            };

            waSubscription.SetAccount("test");
            ProfileClient profileClient = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.ProfileFile)));
            var           waAccount     = new AzureAccount
            {
                Id   = "test",
                Type = AzureAccount.AccountType.User,
            };

            waAccount.SetSubscriptions(IntegrationTestBase.TestCredentials.SubscriptionId.ToString());
            profileClient.Profile.AccountTable["test"] = waAccount;
            profileClient.Profile.Save();
            var accessTokenCreds = getClustersCommand.GetJobSubmissionClientCredentials(
                waSubscription,
                profileClient.Profile.Context.Environment,
                IntegrationTestBase.TestCredentials.WellKnownCluster.DnsName,
                profileClient.Profile);

            Assert.IsInstanceOfType(accessTokenCreds, typeof(HDInsightAccessTokenCredential));
            var asTokenCreds = accessTokenCreds as HDInsightAccessTokenCredential;

            Assert.IsNotNull(asTokenCreds);
            Assert.AreEqual("abc", asTokenCreds.AccessToken);
        }
        public void BaseSetup()
        {
            AzureSessionInitializer.InitializeAzureSession();
            ServiceManagementProfileProvider.InitializeServiceManagementProfile();
            if (AzureSession.Instance.DataStore == null || (AzureSession.Instance.DataStore != null && !(AzureSession.Instance.DataStore is MemoryDataStore)))
            {
                AzureSession.Instance.DataStore = new MemoryDataStore();
            }
            currentProfile = new AzureSMProfile();

            if (currentProfile.Context.Subscription == null)
            {
                var newGuid = Guid.NewGuid();
                var client  = new ProfileClient(currentProfile);
                var account = new AzureAccount
                {
                    Id   = "test",
                    Type = AzureAccount.AccountType.User,
                };
                account.SetSubscriptions(newGuid.ToString());
                client.AddOrSetAccount(account);
                var sub = new AzureSubscription {
                    Id = newGuid.ToString(), Name = "test"
                };
                sub.SetEnvironment(EnvironmentName.AzureCloud);
                sub.SetAccount("test");
                client.AddOrSetSubscription(sub);
                client.SetSubscriptionAsDefault(newGuid, "test");
            }
            AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
        }
Esempio n. 4
0
        internal AzureHDInsightSubscriptionResolverSimulator()
        {
            var certificate = new X509Certificate2(Convert.FromBase64String(IntegrationTestBase.TestCredentials.Certificate), string.Empty);

            AzureSession.Instance.DataStore.AddCertificate(certificate);
            ProfileClient profileClient = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.ProfileFile)));
            var           newAccount    = new AzureAccount
            {
                Id   = certificate.Thumbprint,
                Type = AzureAccount.AccountType.Certificate,
            };

            newAccount.SetSubscriptions(IntegrationTestBase.TestCredentials.SubscriptionId.ToString());
            profileClient.Profile.AccountTable[certificate.Thumbprint] = newAccount;
            profileClient.Profile.Save();
            var sub1 = new AzureSubscription()
            {
                Id = IntegrationTestBase.TestCredentials.SubscriptionId.ToString(),
            };

            sub1.SetAccount(certificate.Thumbprint);
            sub1.SetEnvironment(EnvironmentName.AzureCloud);
            this.knownSubscriptions = new AzureSubscription[]
            {
                sub1
            };
        }
Esempio n. 5
0
        IAzureAccount GetDefaultAccount()
        {
            var account = new AzureAccount
            {
                Credential = Guid.NewGuid().ToString(),
                Id         = "*****@*****.**",
                Type       = AzureAccount.AccountType.User,
            };

            account.TenantMap.Add(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
            account.SetSubscriptions(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
            return(account);
        }
Esempio n. 6
0
        /// <summary>
        /// This overrides the default subscription and default account. This allows the
        /// test to get the tenant id in the test.
        /// </summary>
        public void SetupEnvironment()
        {
            base.SetupEnvironment(AzureModule.AzureResourceManager);

            TestEnvironment csmEnvironment = new CSMTestEnvironmentFactory().GetTestEnvironment();

            if (csmEnvironment.SubscriptionId != null)
            {
                //Overwrite the default subscription and default account
                //with ones using user ID and tenant ID from auth context
                var user     = GetUser(csmEnvironment);
                var tenantId = GetTenantId(csmEnvironment);

                // Existing test will not have a user or tenant id set
                if (tenantId != null && user != null)
                {
                    var testSubscription = new AzureSubscription()
                    {
                        Id   = csmEnvironment.SubscriptionId,
                        Name = AzureRmProfileProvider.Instance.Profile.DefaultContext.Subscription.Name,
                    };

                    testSubscription.SetAccount(user);
                    testSubscription.SetEnvironment(AzureRmProfileProvider.Instance.Profile.DefaultContext.Subscription.GetEnvironment());
                    testSubscription.SetDefault();
                    testSubscription.SetStorageAccount(Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT"));
                    testSubscription.SetTenant(tenantId);
                    var testAccount = new AzureAccount()
                    {
                        Id   = user,
                        Type = AzureAccount.AccountType.User,
                    };

                    testAccount.SetSubscriptions(csmEnvironment.SubscriptionId);

                    AzureRmProfileProvider.Instance.Profile.DefaultContext.Subscription.Name = testSubscription.Name;
                    AzureRmProfileProvider.Instance.Profile.DefaultContext.Subscription.Id   = testSubscription.Id;
                    AzureRmProfileProvider.Instance.Profile.DefaultContext.Subscription.SetAccount(testSubscription.GetAccount());

                    var environment = AzureRmProfileProvider.Instance.Profile.GetEnvironment(AzureRmProfileProvider.Instance.Profile.DefaultContext.Subscription.GetEnvironment());
                    environment.SetEndpoint(AzureEnvironment.Endpoint.Graph, csmEnvironment.Endpoints.GraphUri.AbsoluteUri);
                    environment.SetEndpoint(AzureEnvironment.Endpoint.StorageEndpointSuffix, "core.windows.net");
                    AzureRmProfileProvider.Instance.GetProfile <AzureRmProfile>().Save();
                }
            }
        }
        /// <summary>
        /// Initializes AzureSMProfile using passed in access token.
        /// </summary>
        /// <param name="environment">Environment object.</param>
        /// <param name="subscriptionId">Subscription Id</param>
        /// <param name="accessToken">AccessToken to use with profile.</param>
        /// <param name="accountId">AccountId for the new account.</param>
        /// <param name="storageAccount">Storage account name (optional).</param>
        /// <returns></returns>
        public void InitializeProfile(AzureEnvironment environment, Guid subscriptionId, string accessToken,
                                      string accountId, string storageAccount)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }
            if (accessToken == null)
            {
                throw new ArgumentNullException("accessToken");
            }

            // Add environment if not public
            if (!AzureEnvironment.PublicEnvironments.ContainsKey(environment.Name))
            {
                AddOrSetEnvironment(environment);
            }

            // Add account
            var azureAccount = new AzureAccount
            {
                Id   = accountId,
                Type = AzureAccount.AccountType.AccessToken
            };

            azureAccount.SetSubscriptions(subscriptionId.ToString());
            azureAccount.SetAccessToken(accessToken);
            AddOrSetAccount(azureAccount);

            // Add subscription
            var azureSubscription = new AzureSubscription
            {
                Id   = subscriptionId.ToString(),
                Name = subscriptionId.ToString(),
            };

            azureSubscription.SetEnvironment(environment.Name);
            if (!string.IsNullOrEmpty(storageAccount))
            {
                azureSubscription.SetStorageAccount(storageAccount);
            }
            azureSubscription.SetDefault();
            azureSubscription.SetAccount(accountId);
            AddOrSetSubscription(azureSubscription);
        }
        /// <summary>
        /// Initializes AzureSMProfile using passed in certificate. The certificate
        /// is imported into a certificate store.
        /// </summary>
        /// <param name="environment">Environment object.</param>
        /// <param name="subscriptionId">Subscription Id</param>
        /// <param name="certificate">Certificate to use with profile.</param>
        /// <param name="storageAccount">Storage account name (optional).</param>
        /// <returns></returns>
        public void InitializeProfile(AzureEnvironment environment, Guid subscriptionId, X509Certificate2 certificate,
                                      string storageAccount)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }
            if (certificate == null)
            {
                throw new ArgumentNullException("certificate");
            }

            // Add environment if not public
            if (!AzureEnvironment.PublicEnvironments.ContainsKey(environment.Name))
            {
                AddOrSetEnvironment(environment);
            }

            // Add account
            var azureAccount = new AzureAccount
            {
                Id   = certificate.Thumbprint,
                Type = AzureAccount.AccountType.Certificate
            };

            azureAccount.SetSubscriptions(subscriptionId.ToString());
            ImportCertificate(certificate);
            AddOrSetAccount(azureAccount);

            // Add subscription
            var azureSubscription = new AzureSubscription
            {
                Id   = subscriptionId.ToString(),
                Name = subscriptionId.ToString(),
            };

            azureSubscription.SetEnvironment(environment.Name);
            if (!string.IsNullOrEmpty(storageAccount))
            {
                azureSubscription.SetStorageAccount(storageAccount);
            }
            azureSubscription.SetDefault();
            azureSubscription.SetAccount(certificate.Thumbprint);
            AddOrSetSubscription(azureSubscription);
        }
Esempio n. 9
0
        /// <summary>
        /// Initialize the necessary environment for the tests.
        /// </summary>
        public void BaseSetup()
        {
            TestExecutionHelpers.SetUpSessionAndProfile();
            currentProfile = new AzureRmProfile();
            var newGuid = Guid.NewGuid();
            var account = new AzureAccount
            {
                Id   = "test",
                Type = AzureAccount.AccountType.User,
            };

            account.SetSubscriptions(newGuid.ToString());
            var subscription = new AzureSubscription {
                Id = newGuid.ToString(), Name = "test"
            };

            subscription.SetAccount("test");
            subscription.SetEnvironment(EnvironmentName.AzureCloud);
            currentProfile.DefaultContext = new AzureContext(
                subscription,
                account,
                AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
                new AzureTenant {
                Id = Guid.NewGuid().ToString(), Directory = "testdomain.onmicrosoft.com"
            });
            AzureRmProfileProvider.Instance.Profile = currentProfile;

            // Now override AzureSession.Instance.DataStore to use the MemoryDataStore
            if (AzureSession.Instance.DataStore != null && !(AzureSession.Instance.DataStore is MemoryDataStore))
            {
                AzureSession.Instance.DataStore = new MemoryDataStore();
            }

            AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
            TestMockSupport.RunningMocked = true;
            //This is needed for AutoRest Authentication
            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
        }
        private void SetupAzureEnvironmentFromEnvironmentVariables(AzureModule mode)
        {
            TestEnvironment currentEnvironment = null;

            if (mode == AzureModule.AzureResourceManager)
            {
#if !NETSTANDARD
                currentEnvironment = new CSMTestEnvironmentFactory().GetTestEnvironment();
#else
                currentEnvironment = TestEnvironmentFactory.GetTestEnvironment();
#endif
            }
            else
            {
#if !NETSTANDARD
                currentEnvironment = new RDFETestEnvironmentFactory().GetTestEnvironment();
#else
                throw new NotSupportedException("RDFE environment is not supported in .Net Core");
#endif
            }

            if (currentEnvironment.UserName == null)
            {
                currentEnvironment.UserName = "******";
            }

            SetAuthenticationFactory(mode, currentEnvironment);

            AzureEnvironment environment = new AzureEnvironment {
                Name = testEnvironmentName
            };

            Debug.Assert(currentEnvironment != null);
            environment.ActiveDirectoryAuthority = currentEnvironment.Endpoints.AADAuthUri.AbsoluteUri;
            environment.GalleryUrl           = currentEnvironment.Endpoints.GalleryUri.AbsoluteUri;
            environment.ServiceManagementUrl = currentEnvironment.BaseUri.AbsoluteUri;
            environment.ResourceManagerUrl   = currentEnvironment.Endpoints.ResourceManagementUri.AbsoluteUri;
            environment.GraphUrl             = currentEnvironment.Endpoints.GraphUri.AbsoluteUri;
            environment.AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix = currentEnvironment.Endpoints.DataLakeAnalyticsJobAndCatalogServiceUri.OriginalString.Replace("https://", ""); // because it is just a sufix
            environment.AzureDataLakeStoreFileSystemEndpointSuffix        = currentEnvironment.Endpoints.DataLakeStoreServiceUri.OriginalString.Replace("https://", "");                  // because it is just a sufix
#if !NETSTANDARD
            if (!ProfileClient.Profile.EnvironmentTable.ContainsKey(testEnvironmentName))
            {
                ProfileClient.AddOrSetEnvironment(environment);
            }
#endif
            if (!AzureRmProfileProvider.Instance.GetProfile <AzureRmProfile>().EnvironmentTable.ContainsKey(testEnvironmentName))
            {
                AzureRmProfileProvider.Instance.GetProfile <AzureRmProfile>().EnvironmentTable[testEnvironmentName] = environment;
            }

            if (currentEnvironment.SubscriptionId != null)
            {
                testSubscription = new AzureSubscription()
                {
                    Id   = currentEnvironment.SubscriptionId,
                    Name = testSubscriptionName,
                };

                testSubscription.SetEnvironment(testEnvironmentName);
                testSubscription.SetAccount(currentEnvironment.UserName);
                testSubscription.SetDefault();
                testSubscription.SetStorageAccount(Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT"));

                testAccount = new AzureAccount()
                {
                    Id   = currentEnvironment.UserName,
                    Type = AzureAccount.AccountType.User,
                };

                testAccount.SetSubscriptions(currentEnvironment.SubscriptionId);
#if !NETSTANDARD
                ProfileClient.Profile.SubscriptionTable[testSubscription.GetId()] = testSubscription;
                ProfileClient.Profile.AccountTable[testAccount.Id] = testAccount;
                ProfileClient.SetSubscriptionAsDefault(testSubscription.Name, testSubscription.GetAccount());
#endif
                var testTenant = new AzureTenant()
                {
                    Id = Guid.NewGuid().ToString()
                };
                if (!string.IsNullOrEmpty(currentEnvironment.Tenant))
                {
                    Guid tenant;
                    if (Guid.TryParse(currentEnvironment.Tenant, out tenant))
                    {
                        testTenant.Id = currentEnvironment.Tenant;
                    }
                }
                AzureRmProfileProvider.Instance.Profile.DefaultContext = new AzureContext(testSubscription, testAccount, environment, testTenant);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Setup certificates required for the unit tests
        /// </summary>
        public static void SetupCertificates()
        {
            PSTestTracingInterceptor.AddToContext();
            var newGuid = Guid.NewGuid();
            var profile = new AzureSMProfile(Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.ProfileFile));

            AzureSMCmdlet.CurrentProfile                = profile;
            AzureSession.Instance.DataStore             = new MemoryDataStore();
            AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
            ProfileClient client = new ProfileClient(profile);
            var           acct2  = new AzureAccount
            {
                Id   = "test",
                Type = AzureAccount.AccountType.User
            };

            acct2.SetSubscriptions(newGuid.ToString());
            client.AddOrSetAccount(acct2);
            var acct3 = new AzureAccount
            {
                Id   = UnitTestHelper.GetUnitTestClientCertificate().Thumbprint,
                Type = AzureAccount.AccountType.Certificate
            };

            acct3.SetSubscriptions(newGuid.ToString());
            client.AddOrSetAccount(acct3);
            var acct = new AzureAccount
            {
                Id   = UnitTestHelper.GetUnitTestSSLCertificate().Thumbprint,
                Type = AzureAccount.AccountType.Certificate,
            };

            acct.SetSubscriptions(newGuid.ToString());
            client.AddOrSetAccount(acct);
            var sub1 = new AzureSubscription
            {
                Id   = newGuid.ToString(),
                Name = "test",
            };

            sub1.SetEnvironment(EnvironmentName.AzureCloud);
            sub1.SetAccount("test");
            client.AddOrSetSubscription(sub1);

            client.SetSubscriptionAsDefault(newGuid, "test");
            client.Profile.Save();


            // Check if the cert has been installed
            Process proc = ExecuteProcess(
                "netsh",
                string.Format(
                    CultureInfo.InvariantCulture,
                    "http show sslcert ipport=0.0.0.0:{0}",
                    DefaultHttpsServerPrefixUri.Port));

            if (proc.ExitCode != 0)
            {
                // Install the SSL and client certificates to the LocalMachine store
                X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadWrite);
                store.Add(UnitTestHelper.GetUnitTestSSLCertificate());
                store.Add(UnitTestHelper.GetUnitTestClientCertificate());
                store.Close();
                store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadWrite);
                store.Add(UnitTestHelper.GetUnitTestSSLCertificate());
                store.Add(UnitTestHelper.GetUnitTestClientCertificate());
                store.Close();

                // Remove any existing certs on the default port
                proc = ExecuteProcess(
                    "netsh",
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "http delete sslcert ipport=0.0.0.0:{0}",
                        DefaultHttpsServerPrefixUri.Port));

                // Install the ssl cert on the default port
                proc = ExecuteProcess(
                    "netsh",
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "http add sslcert ipport=0.0.0.0:{0} certhash={1} appid={2:B}",
                        DefaultHttpsServerPrefixUri.Port,
                        UnitTestHelper.GetUnitTestSSLCertificate().Thumbprint,
                        MockHttpServer.HttpsAppId));

                if (proc.ExitCode != 0)
                {
                    throw new InvalidOperationException(string.Format(
                                                            CultureInfo.InvariantCulture,
                                                            "Unable to add ssl certificate: {0}",
                                                            proc.StandardOutput.ReadToEnd()));
                }
            }
        }
Esempio n. 12
0
        private void SetupAzureEnvironmentFromEnvironmentVariables(AzureModule mode)
        {
            TestEnvironment rdfeEnvironment    = new RDFETestEnvironmentFactory().GetTestEnvironment();
            TestEnvironment csmEnvironment     = new CSMTestEnvironmentFactory().GetTestEnvironment();
            TestEnvironment currentEnvironment = (mode == AzureModule.AzureResourceManager ? csmEnvironment : rdfeEnvironment);

            if (currentEnvironment.UserName == null)
            {
                currentEnvironment.UserName = "******";
            }

            SetAuthenticationFactory(mode, rdfeEnvironment, csmEnvironment);

            AzureEnvironment environment = new AzureEnvironment {
                Name = testEnvironmentName
            };

            Debug.Assert(currentEnvironment != null);
            environment.ActiveDirectoryAuthority = currentEnvironment.Endpoints.AADAuthUri.AbsoluteUri;
            environment.GalleryUrl = currentEnvironment.Endpoints.GalleryUri.AbsoluteUri;

            if (csmEnvironment != null)
            {
                environment.ResourceManagerUrl = csmEnvironment.BaseUri.AbsoluteUri;
            }

            if (rdfeEnvironment != null)
            {
                environment.ServiceManagementUrl = rdfeEnvironment.BaseUri.AbsoluteUri;
            }

            if (!ProfileClient.Profile.EnvironmentTable.ContainsKey(testEnvironmentName))
            {
                ProfileClient.AddOrSetEnvironment(environment);
            }

            if (currentEnvironment.SubscriptionId != null)
            {
                testSubscription = new AzureSubscription()
                {
                    Id   = currentEnvironment.SubscriptionId,
                    Name = testSubscriptionName,
                };
                testSubscription.SetEnvironment(testEnvironmentName);
                testSubscription.SetAccount(currentEnvironment.UserName);
                testSubscription.SetStorageAccount(Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT"));
                testSubscription.SetDefault();

                testAccount = new AzureAccount()
                {
                    Id   = currentEnvironment.UserName,
                    Type = AzureAccount.AccountType.User,
                };

                testAccount.SetSubscriptions(currentEnvironment.SubscriptionId);

                ProfileClient.Profile.SubscriptionTable[testSubscription.GetId()] = testSubscription;
                ProfileClient.Profile.AccountTable[testAccount.Id] = testAccount;
                ProfileClient.SetSubscriptionAsDefault(testSubscription.Name, testSubscription.GetAccount());
            }
        }