public void LoginWithInvalidSubscriptionAndTenantThrowsCloudException()
        {
            var cmdlt = new ConnectAzureRmAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.Subscription   = "2c224e7e-3ef5-431d-a57b-e71f4662e3a5";
            cmdlt.MyInvocation.BoundParameters.Add(nameof(cmdlt.Subscription), "2c224e7e-3ef5-431d-a57b-e71f4662e3a5");
            cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
            cmdlt.MyInvocation.BoundParameters.Add(nameof(cmdlt.Tenant), "72f988bf-86f1-41af-91ab-2d7cd011db47");
            cmdlt.SetParameterSet("UserWithSubscriptionId");

            // Act

            cmdlt.InvokeBeginProcessing();
            Assert.Throws <PSInvalidOperationException>(() => cmdlt.ExecuteCmdlet());
            cmdlt.InvokeEndProcessing();
        }
        public void LoginWithSubscriptionAndTenant()
        {
            var cmdlt = new ConnectAzureRmAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.Subscription   = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
            cmdlt.TenantId       = "72f988bf-86f1-41af-91ab-2d7cd011db47";
            cmdlt.SetParameterSet("UserWithSubscriptionId");

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
            Assert.Equal("microsoft.com", AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Directory);
        }
        public void LoginWithEnvironementName()
        {
            var cmdlt = new ConnectAzureRmAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.Environment    = "AzureUSGovernment";
            cmdlt.SetParameterSet("UserWithSubscriptionId");

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext.Environment);
            Assert.Equal("AzureUSGovernment", AzureRmProfileProvider.Instance.Profile.DefaultContext.Environment.Name);
        }
        public void LoginWithRbacTenantOnly()
        {
            var cmdlt = new ConnectAzureRmAccountCommand();

            // Setup
            // NOTE: Use [email protected] credentials for this test case
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.Tenant         = "1449d5b7-8a83-47db-ae4c-9b03e888bad0";
            cmdlt.SetParameterSet("UserWithSubscriptionId");

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
            Assert.Equal(cmdlt.Tenant, AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Id.ToString());
            Assert.Null(AzureRmProfileProvider.Instance.Profile.DefaultContext.Subscription);
        }
        public void LoginWithNoSubscriptionAndTenant()
        {
            var cmdlt = new ConnectAzureRmAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            var tenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";

            cmdlt.Tenant = tenantId;
            cmdlt.MyInvocation.BoundParameters.Add(nameof(cmdlt.Tenant), tenantId);
            cmdlt.SetParameterSet("UserWithSubscriptionId");

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
        }
        public void LoginWithSubscriptionAndNoTenant()
        {
            var cmdlt = new ConnectAzureRmAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            var subscriptionId = "9e223dbe-3399-4e19-88eb-0975f02ac87f";

            cmdlt.Subscription = subscriptionId;
            cmdlt.MyInvocation.BoundParameters.Add(nameof(cmdlt.Subscription), subscriptionId);
            cmdlt.SetParameterSet("UserWithSubscriptionId");

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
        }
Exemple #7
0
        public void LoginWithAccessToken()
        {
            var cmdlt = new ConnectAzureRmAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;

            // Obtain an access token by using [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.TokenCache.ReadItems() in powershell after logging in.
            // Ensure you are using the token with Resource: https://management.core.windows.net/
            string accessTokenEnvironmentVariable = Environment.GetEnvironmentVariable("AZURE_TEST_ACCESS_TOKEN");

            cmdlt.AccessToken = accessTokenEnvironmentVariable;
            cmdlt.AccountId   = "testAccount";
            cmdlt.SetParameterSet("AccessTokenWithSubscriptionId");

            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
        }
Exemple #8
0
        public void GetMultipleTenantsOnLogin()
        {
            var cmdlt = new ConnectAzureRmAccountCommand();

            // Setup
            // NOTE: Use account that has at exactly two tenants
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.SetParameterSet("UserWithSubscriptionId");

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext.Account);
            var tenants = AzureRmProfileProvider.Instance.Profile.DefaultContext.Account.GetTenants();

            Assert.NotNull(tenants);
            Assert.Equal(2, tenants.Length);
        }
Exemple #9
0
        public void LoginWithCredentialParameterAndMSA()
        {
            var cmdlt = new ConnectAzureRmAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;

            // Example of environment variable: TEST_AZURE_CREDENTIALS=<subscription-id-value>;<*****@*****.**>;<email-password>"
            string credsEnvironmentVariable = Environment.GetEnvironmentVariable("TEST_AZURE_CREDENTIALS");

            string[] creds = credsEnvironmentVariable.Split(';');

            string userName = creds[1];
            string password = creds[2];

            var securePassword = new SecureString();

            Array.ForEach(password.ToCharArray(), securePassword.AppendChar);

            cmdlt.Credential = new PSCredential(userName, securePassword);
            cmdlt.SetParameterSet("UserWithSubscriptionId");

            // Act
            try
            {
                cmdlt.InvokeBeginProcessing();
                cmdlt.ExecuteCmdlet();
                cmdlt.InvokeEndProcessing();
            }
            catch (AadAuthenticationFailedException ex)
            {
                Assert.NotNull(ex);
                Assert.Equal("-Credential parameter can only be used with Organization ID credentials. " +
                             "For more information, please refer to http://go.microsoft.com/fwlink/?linkid=331007&clcid=0x409 " +
                             "for more information about the difference between an organizational account and a Microsoft account.",
                             ex.Message);
            }
        }
Exemple #10
0
        public void LoginPopulatesContextList()
        {
            // Before running this test, make sure to clear the contexts on your machine by removing the following two files:
            // - %APPDATA%/Windows Azure Powershell/AzureRmContext.json
            // - %APPDATA%/Windows Azure Powershell/AzureRmContextSettings.json
            // This will clear all existing contexts on your machine so that this test can re-populate the list with a context for each subscription

            var cmdlt = new ConnectAzureRmAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            var profile = AzureRmProfileProvider.Instance.Profile as AzureRmProfile;

            Assert.NotNull(profile);
            Assert.NotNull(profile.Contexts);
            Assert.NotNull(profile.Subscriptions);
            Assert.True(profile.Contexts.Count > 1);
            Assert.True(profile.Subscriptions.Count() > 1);
            Assert.Equal(profile.Subscriptions.Count(), profile.Contexts.Count);

            foreach (var sub in profile.Subscriptions)
            {
                var contextName = string.Format("{0} - {1}", sub.Name, sub.Id);
                Assert.True(profile.Contexts.ContainsKey(contextName));
                var context = profile.Contexts[contextName];
                Assert.NotNull(context);
                Assert.Equal(sub.Id, context.Subscription.Id);
                Assert.Equal(sub.GetTenant(), context.Tenant.Id);
            }
        }
        //Verify https://github.com/Azure/azure-powershell/issues/13340
        public void LoginUseInteractiveThruNonDesktop()
        {
            var cmdlt = new ConnectAzureRmAccountCommand();

            // Setup
            CommonUtilities commonUtilities;

            AzureSession.Instance.TryGetComponent(nameof(CommonUtilities), out commonUtilities);
            try
            {
                var mockUtilities = new Mock <CommonUtilities>();
                mockUtilities.Setup(u => u.IsDesktopSession()).Returns(false);
                AzureSession.Instance.RegisterComponent(nameof(CommonUtilities),
                                                        () => mockUtilities.Object, true);
                cmdlt.CommandRuntime = commandRuntimeMock;
                cmdlt.SetParameterSet("UserWithSubscriptionId");

                // Act
                cmdlt.InvokeBeginProcessing();
                cmdlt.ExecuteCmdlet();
                cmdlt.InvokeEndProcessing();

                //Verify
                Assert.Single(commandRuntimeMock.WarningStream);
                Assert.Equal("Interactive authentication is not supported in this session, please run cmdlet 'Connect-AzAccount -UseDeviceAuthentication'.", commandRuntimeMock.WarningStream[0]);
                Assert.Null(AzureRmProfileProvider.Instance.Profile.DefaultContext);
            }
            finally
            {
                if (commonUtilities != null)
                {
                    AzureSession.Instance.RegisterComponent(nameof(CommonUtilities),
                                                            () => commonUtilities, true);
                }
            }
        }
Exemple #12
0
        public void AddEnvironmentUpdatesContext()
        {
            var cmdlet = new AddAzureRMEnvironmentCommand()
            {
                CommandRuntime         = commandRuntimeMock,
                Name                   = "Katal",
                ARMEndpoint            = "https://management.azure.com/",
                AzureKeyVaultDnsSuffix = "vault.local.azurestack.external",
                AzureKeyVaultServiceEndpointResourceId = "https://vault.local.azurestack.external"
            };
            var dict = new Dictionary <string, object>
            {
                { "ARMEndpoint", "https://management.azure.com/" },
                { "AzureKeyVaultDnsSuffix", "vault.local.azurestack.external" },
                { "AzureKeyVaultServiceEndpointResourceId", "https://vault.local.azurestack.external" }
            };

            cmdlet.SetBoundParameters(dict);
            cmdlet.SetParameterSet("ARMEndpoint");
            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            commandRuntimeMock = new MockCommandRuntime();
            var profileClient     = new RMProfileClient(AzureRmProfileProvider.Instance.GetProfile <AzureRmProfile>());
            IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.Environments.First((e) => string.Equals(e.Name, "KaTaL", StringComparison.OrdinalIgnoreCase));

            Assert.Equal(env.Name, cmdlet.Name);
            Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix), dict["AzureKeyVaultDnsSuffix"]);
            Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId), dict["AzureKeyVaultServiceEndpointResourceId"]);

            var cmdlet1 = new ConnectAzureRmAccountCommand();

            cmdlet1.CommandRuntime = commandRuntimeMock;
            cmdlet1.Environment    = "Katal";

            dict.Clear();
            dict = new Dictionary <string, object>
            {
                { "Environment", cmdlet1.Environment }
            };

            cmdlet1.SetBoundParameters(dict);
            cmdlet1.InvokeBeginProcessing();
            cmdlet1.ExecuteCmdlet();
            cmdlet1.InvokeEndProcessing();
            commandRuntimeMock = new MockCommandRuntime();

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
            Assert.Equal(AzureRmProfileProvider.Instance.Profile.DefaultContext.Environment.Name, cmdlet1.Environment);

            var cmdlet2 = new AddAzureRMEnvironmentCommand()
            {
                CommandRuntime         = commandRuntimeMock,
                Name                   = "Katal",
                ARMEndpoint            = "https://management.azure.com/",
                AzureKeyVaultDnsSuffix = "adminvault.local.azurestack.external",
                AzureKeyVaultServiceEndpointResourceId = "https://adminvault.local.azurestack.external"
            };

            dict.Clear();
            dict = new Dictionary <string, object>
            {
                { "ARMEndpoint", "https://management.azure.com/" },
                { "AzureKeyVaultDnsSuffix", "adminvault.local.azurestack.external" },
                { "AzureKeyVaultServiceEndpointResourceId", "https://adminvault.local.azurestack.external" }
            };

            cmdlet2.SetBoundParameters(dict);
            cmdlet2.SetParameterSet("ARMEndpoint");
            cmdlet2.InvokeBeginProcessing();
            cmdlet2.ExecuteCmdlet();
            cmdlet2.InvokeEndProcessing();

            profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.GetProfile <AzureRmProfile>());
            env           = AzureRmProfileProvider.Instance.Profile.Environments.First((e) => string.Equals(e.Name, "KaTaL", StringComparison.OrdinalIgnoreCase));
            Assert.Equal(env.Name, cmdlet.Name);
            Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix), dict["AzureKeyVaultDnsSuffix"]);
            Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId), dict["AzureKeyVaultServiceEndpointResourceId"]);

            var context = AzureRmProfileProvider.Instance.Profile.DefaultContext;

            Assert.NotNull(context);
            Assert.NotNull(context.Environment);
            Assert.Equal(context.Environment.Name, env.Name);
            Assert.Equal(context.Environment.AzureKeyVaultDnsSuffix, env.AzureKeyVaultDnsSuffix);
            Assert.Equal(context.Environment.AzureKeyVaultServiceEndpointResourceId, env.AzureKeyVaultServiceEndpointResourceId);
        }