public void AddsAzureEnvironment()
        {
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            SetupConfirmation(commandRuntimeMock);
            var cmdlet = new AddAzureRMEnvironmentCommand()
            {
                CommandRuntime = commandRuntimeMock.Object,
                Name           = "Katal",
            };
            var dict = new Dictionary <string, object>
            {
                { "PublishSettingsFileUrl", "http://microsoft.com" },
                { "ServiceEndpoint", "https://endpoint.net" },
                { "ManagementPortalUrl", "http://management.portal.url" },
                { "StorageEndpoint", "http://endpoint.net" },
                { "GalleryEndpoint", "http://galleryendpoint.com" },
            };

            cmdlet.SetBoundParameters(dict);
            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Once());
            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.PublishSettingsFileUrl), dict["PublishSettingsFileUrl"]);
            Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.ServiceManagement), dict["ServiceEndpoint"]);
            Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.ManagementPortalUrl), dict["ManagementPortalUrl"]);
            Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.Gallery), "http://galleryendpoint.com");
        }
        public void AddsAzureEnvironmentUsingAPublicRMEndpoint()
        {
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            SetupConfirmation(commandRuntimeMock);
            var cmdlet = new AddAzureRMEnvironmentCommand()
            {
                CommandRuntime = commandRuntimeMock.Object,
                Name           = "Katal",
                ARMEndpoint    = "https://management.azure.com/"
            };

            Mock <EnvironmentHelper> envHelperMock = new Mock <EnvironmentHelper>();

            envHelperMock.Setup(f => f.RetrieveMetaDataEndpoints(It.IsAny <string>())).ReturnsAsync(null);
            envHelperMock.Setup(f => f.RetrieveDomain(It.IsAny <string>())).Returns("domain");
            cmdlet.EnvHelper = envHelperMock.Object;
            cmdlet.SetParameterSet("ARMEndpoint");
            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Once());
            IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.GetEnvironment("Katal");
            var oracle            = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud];

            Assert.Equal(env.Name, cmdlet.Name);
            Assert.Equal(oracle.ResourceManagerUrl, env.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager));
            Assert.Equal(oracle.ActiveDirectoryAuthority, env.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectory));
            Assert.Equal(oracle.ActiveDirectoryServiceEndpointResourceId, env.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId));
            envHelperMock.Verify(f => f.RetrieveDomain(It.IsAny <string>()), Times.Never);
            envHelperMock.Verify(f => f.RetrieveMetaDataEndpoints(It.IsAny <string>()), Times.Never);
        }
        private string GetFunctionsResourceId(string resourceIdOrEndpointName, IAzureEnvironment environment)
        {
            var resourceId = environment.GetEndpoint(resourceIdOrEndpointName) ?? resourceIdOrEndpointName;

            if (string.Equals(
                    environment.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId),
                    resourceId, StringComparison.OrdinalIgnoreCase))
            {
                resourceId = environment.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager);
            }

            return(resourceId);
        }
Beispiel #4
0
 private void CheckEndpoint(string endpoint, IAzureEnvironment environment, string valueToCheck)
 {
     if (environment.IsEndpointSet(endpoint))
     {
         Assert.Equal(environment.GetEndpoint(endpoint), valueToCheck);
     }
 }
        public void AddsEnvironmentWithMinimumInformation()
        {
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            SetupConfirmation(commandRuntimeMock);
            var cmdlet = new AddAzureRMEnvironmentCommand()
            {
                CommandRuntime           = commandRuntimeMock.Object,
                Name                     = "Katal",
                EnableAdfsAuthentication = true
            };

            var dict = new Dictionary <string, object>();

            dict["EnableAdfsAuthentication"] = true;
            dict["PublishSettingsFileUrl"]   = "http://microsoft.com";
            cmdlet.SetBoundParameters(dict);
            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Once());
            IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.GetEnvironment("KaTaL");

            Assert.Equal(env.Name, cmdlet.Name);
            Assert.True(env.OnPremise);
            Assert.Equal("http://microsoft.com", env.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl));
        }
        public void AddsEnvironmentWithStorageEndpoint()
        {
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            SetupConfirmation(commandRuntimeMock);
            PSAzureEnvironment actual = null;

            commandRuntimeMock.Setup(f => f.WriteObject(It.IsAny <object>()))
            .Callback((object output) => actual = (PSAzureEnvironment)output);
            var cmdlet = new AddAzureRMEnvironmentCommand()
            {
                CommandRuntime         = commandRuntimeMock.Object,
                Name                   = "Katal",
                PublishSettingsFileUrl = "http://microsoft.com",
                StorageEndpoint        = "core.windows.net",
            };

            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Once());
            IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.GetEnvironment("KaTaL");

            Assert.Equal(env.Name, cmdlet.Name);
            Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.StorageEndpointSuffix), actual.StorageEndpointSuffix);
        }
        public void AddsAzureEnvironmentUsingARMEndpoint()
        {
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            SetupConfirmation(commandRuntimeMock);
            var cmdlet = new AddAzureRMEnvironmentCommand()
            {
                CommandRuntime = commandRuntimeMock.Object,
                Name           = "Stack",
                ARMEndpoint    = "https://management.local.azurestack.external/"
            };

            Mock <EnvironmentHelper> envHelperMock     = new Mock <EnvironmentHelper>();
            MetadataResponse         metadataEndpoints = new MetadataResponse
            {
                GalleryEndpoint = "https://galleryendpoint",
                GraphEndpoint   = "https://graphendpoint",
                PortalEndpoint  = "https://portalendpoint",
                authentication  = new Authentication
                {
                    Audiences     = new[] { "audience1", "audience2" },
                    LoginEndpoint = "https://loginendpoint"
                }
            };

            envHelperMock.Setup(f => f.RetrieveMetaDataEndpoints(It.IsAny <string>())).ReturnsAsync(metadataEndpoints);
            envHelperMock.Setup(f => f.RetrieveDomain(It.IsAny <string>())).Returns("domain");
            cmdlet.EnvHelper = envHelperMock.Object;
            cmdlet.SetParameterSet("ARMEndpoint");
            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Once());
            var profileClient     = new RMProfileClient(AzureRmProfileProvider.Instance.GetProfile <AzureRmProfile>());
            IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.GetEnvironment("Stack");

            Assert.Equal(env.Name, cmdlet.Name);
            Assert.Equal(cmdlet.ARMEndpoint, env.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager));
            Assert.Equal("https://loginendpoint/", env.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectory));
            Assert.Equal("audience1", env.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId));
            Assert.Equal("https://graphendpoint", env.GetEndpoint(AzureEnvironment.Endpoint.GraphEndpointResourceId));
            envHelperMock.Verify(f => f.RetrieveDomain(It.IsAny <string>()), Times.Once);
            envHelperMock.Verify(f => f.RetrieveMetaDataEndpoints(It.IsAny <string>()), Times.Once);
        }
Beispiel #8
0
        public override Task <IAccessToken> Authenticate(IAzureAccount account, IAzureEnvironment environment, string tenant, SecureString password, string promptBehavior, Task <Action <string> > promptAction, IAzureTokenCache tokenCache, string resourceId)
        {
            var audience = environment.GetEndpoint(resourceId);
            var context  = new AuthenticationContext(
                AuthenticationHelpers.GetAuthority(environment, tenant),
                environment?.OnPremise ?? true,
                tokenCache as TokenCache ?? TokenCache.DefaultShared);
            var result = context.AcquireTokenAsync(audience, AuthenticationHelpers.PowerShellClientId, new UserPasswordCredential(account.Id, password));

            return(AuthenticationResultToken.GetAccessTokenAsync(result));
        }
Beispiel #9
0
        private AdalConfiguration GetAdalConfiguration(IAzureEnvironment environment, string tenantId,
                                                       string resourceId, TokenCache tokenCache)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }

            var adEndpoint = environment.ActiveDirectoryAuthority;

            if (null == adEndpoint)
            {
                throw new ArgumentOutOfRangeException(
                          "environment",
                          string.Format("No Active Directory endpoint specified for environment '{0}'", environment.Name));
            }

            var audience = environment.GetEndpoint(resourceId) ?? resourceId;

            if (string.IsNullOrWhiteSpace(audience))
            {
                string message = Resources.InvalidManagementTokenAudience;
                if (resourceId == AzureEnvironment.Endpoint.GraphEndpointResourceId)
                {
                    message = Resources.InvalidGraphTokenAudience;
                }

                throw new ArgumentOutOfRangeException("environment", string.Format(message, environment.Name));
            }

            return(new AdalConfiguration
            {
                AdEndpoint = adEndpoint.ToString(),
                ResourceClientUri = environment.GetEndpoint(resourceId),
                AdDomain = tenantId,
                ValidateAuthority = !environment.OnPremise,
                TokenCache = tokenCache
            });
        }
        public async override Task <IAccessToken> Authenticate(IAzureAccount account, IAzureEnvironment environment, string tenant, SecureString password, string promptBehavior, Task <Action <string> > promptAction, IAzureTokenCache tokenCache, string resourceId)
        {
            var auth     = new AuthenticationContext(AuthenticationHelpers.GetAuthority(environment, tenant), environment?.OnPremise ?? true, tokenCache as TokenCache ?? TokenCache.DefaultShared);
            var response = await auth.AcquireTokenAsync(
                environment.GetEndpoint(resourceId),
                AuthenticationHelpers.PowerShellClientId,
                new Uri(AuthenticationHelpers.PowerShellRedirectUri),
                new PlatformParameters(AuthenticationHelpers.GetPromptBehavior(promptBehavior), new ConsoleParentWindow()),
                UserIdentifier.AnyUser,
                AuthenticationHelpers.EnableEbdMagicCookie);

            account.Id = response?.UserInfo?.DisplayableId;
            return(AuthenticationResultToken.GetAccessToken(response));
        }
        /// <summary>
        /// Determine if the given endpoint is set to the provided value
        /// </summary>
        /// <param name="environment">The environment to search</param>
        /// <param name="endpoint">The endpoint to check</param>
        /// <param name="url">The value to check for</param>
        /// <returns>True if the endpoint is set to the proviuded value, otherwise false</returns>
        public static bool IsEndpointSetToValue(this IAzureEnvironment environment, string endpoint, string url)
        {
            if (url == null && !environment.IsEndpointSet(endpoint))
            {
                return(true);
            }
            if (url != null && environment.IsEndpointSet(endpoint))
            {
                return(environment.GetEndpoint(endpoint)
                       .Trim(new[] { '/' })
                       .Equals(url.Trim(new[] { '/' }), StringComparison.InvariantCultureIgnoreCase));
            }

            return(false);
        }
        public void AddsEnvironmentMultipleTimes()
        {
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            SetupConfirmation(commandRuntimeMock);
            var cmdlet = new AddAzureRMEnvironmentCommand()
            {
                CommandRuntime           = commandRuntimeMock.Object,
                Name                     = "Katal",
                PublishSettingsFileUrl   = "http://microsoft.com",
                EnableAdfsAuthentication = true,
            };

            var dict = new Dictionary <string, object>();

            dict["PublishSettingsFileUrl"]   = "http://microsoft.com";
            dict["EnableAdfsAuthentication"] = true;
            cmdlet.SetBoundParameters(dict);

            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Once());
            IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.GetEnvironment("KaTaL");

            Assert.Equal(env.Name, cmdlet.Name);
            Assert.True(env.OnPremise);
            Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);

            // Execute the same without PublishSettingsFileUrl and make sure the first value is preserved
            var cmdlet2 = new AddAzureRMEnvironmentCommand()
            {
                CommandRuntime           = commandRuntimeMock.Object,
                Name                     = "Katal",
                EnableAdfsAuthentication = true,
            };

            dict.Clear();
            dict["EnableAdfsAuthentication"] = true;
            cmdlet2.SetBoundParameters(dict);

            cmdlet2.InvokeBeginProcessing();
            cmdlet2.ExecuteCmdlet();
            cmdlet2.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Exactly(2));
            IAzureEnvironment env2 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("KaTaL");

            Assert.Equal(env2.Name, cmdlet2.Name);
            Assert.True(env2.OnPremise);
            Assert.Equal(env2.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);

            var cmdlet3 = new AddAzureRMEnvironmentCommand()
            {
                CommandRuntime = commandRuntimeMock.Object,
                Name           = "Katal",
            };

            dict.Clear();
            cmdlet3.SetBoundParameters(dict);

            cmdlet3.InvokeBeginProcessing();
            cmdlet3.ExecuteCmdlet();
            cmdlet3.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Exactly(3));
            IAzureEnvironment env3 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("KaTaL");

            Assert.Equal(env3.Name, cmdlet3.Name);
            Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
        }
 private string GetResourceId(string resourceIdorEndpointName, IAzureEnvironment environment)
 {
     return(environment.GetEndpoint(resourceIdorEndpointName) ?? resourceIdorEndpointName);
 }
 private static bool NeedTenantArmPermission(IAzureEnvironment environment, string tenantId, string resourceId)
 {
     return(!string.IsNullOrEmpty(tenantId) && !string.IsNullOrEmpty(resourceId) &&
            string.Equals(environment.GetEndpoint(resourceId), environment.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId)));
 }
 static string GetResource(string endpointOrResource, IAzureEnvironment environment)
 {
     return(environment.GetEndpoint(endpointOrResource) ?? endpointOrResource);
 }
        public void SetEnvironmentForStack()
        {
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            SetupConfirmation(commandRuntimeMock);
            var cmdlet = new SetAzureRMEnvironmentCommand()
            {
                CommandRuntime = commandRuntimeMock.Object,
                Name           = "Stack",
                ARMEndpoint    = "https://management.local.azurestack.external/"
            };

            Mock <EnvironmentHelper> envHelperMock     = new Mock <EnvironmentHelper>();
            MetadataResponse         metadataEndpoints = new MetadataResponse
            {
                GalleryEndpoint = "https://galleryendpoint",
                GraphEndpoint   = "https://graphendpoint",
                PortalEndpoint  = "https://portalendpoint",
                authentication  = new Authentication
                {
                    Audiences     = new[] { "audience1", "audience2" },
                    LoginEndpoint = "https://loginendpoint"
                }
            };

            envHelperMock.Setup(f => f.RetrieveMetaDataEndpoints(It.IsAny <string>())).ReturnsAsync(metadataEndpoints);
            envHelperMock.Setup(f => f.RetrieveDomain(It.IsAny <string>())).Returns("domain");
            cmdlet.EnvHelper = envHelperMock.Object;
            cmdlet.SetParameterSet("ARMEndpoint");
            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Once());
            IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.GetEnvironment("Stack");

            Assert.Equal(env.Name, cmdlet.Name);
            Assert.Equal(cmdlet.ARMEndpoint, env.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager));
            Assert.Equal("https://loginendpoint/", env.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectory));
            Assert.Equal("audience1", env.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId));
            Assert.Equal("https://graphendpoint", env.GetEndpoint(AzureEnvironment.Endpoint.GraphEndpointResourceId));
            envHelperMock.Verify(f => f.RetrieveDomain(It.IsAny <string>()), Times.Once);
            envHelperMock.Verify(f => f.RetrieveMetaDataEndpoints(It.IsAny <string>()), Times.Once);

            // Update onpremise to true
            var cmdlet2 = new SetAzureRMEnvironmentCommand()
            {
                CommandRuntime           = commandRuntimeMock.Object,
                Name                     = "Stack",
                EnableAdfsAuthentication = true
            };

            cmdlet2.MyInvocation.BoundParameters.Add("Name", "Stack");
            cmdlet2.MyInvocation.BoundParameters.Add("EnableAdfsAuthentication", true);

            cmdlet2.InvokeBeginProcessing();
            cmdlet2.ExecuteCmdlet();
            cmdlet2.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Exactly(2));
            IAzureEnvironment env2 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("stack");

            Assert.Equal(env2.Name, cmdlet2.Name);
            Assert.Equal(env2.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
            Assert.True(env2.OnPremise);

            // Update gallery endpoint
            var cmdlet3 = new SetAzureRMEnvironmentCommand()
            {
                CommandRuntime  = commandRuntimeMock.Object,
                Name            = "Stack",
                GalleryEndpoint = "http://galleryendpoint.com",
            };

            cmdlet3.MyInvocation.BoundParameters.Add("Name", "stack");
            cmdlet3.MyInvocation.BoundParameters.Add("GalleryEndpoint", "http://galleryendpoint.com");

            cmdlet3.InvokeBeginProcessing();
            cmdlet3.ExecuteCmdlet();
            cmdlet3.InvokeEndProcessing();

            // Ensure gallery endpoint is updated and OnPremise value is preserved
            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Exactly(3));
            IAzureEnvironment env3 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("stack");

            Assert.Equal(env3.Name, cmdlet3.Name);
            Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
            Assert.True(env3.OnPremise);
            Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.Gallery), cmdlet3.GalleryEndpoint);
        }
        public void SetEnvironmentWithOnPremise()
        {
            // Setup a new environment
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            SetupConfirmation(commandRuntimeMock);

            var cmdlet = new SetAzureRMEnvironmentCommand()
            {
                CommandRuntime           = commandRuntimeMock.Object,
                Name                     = "Katal",
                PublishSettingsFileUrl   = "http://microsoft.com",
                EnableAdfsAuthentication = false
            };

            cmdlet.MyInvocation.BoundParameters.Add("Name", "Katal");
            cmdlet.MyInvocation.BoundParameters.Add("PublishSettingsFileUrl", "http://microsoft.com");
            cmdlet.MyInvocation.BoundParameters.Add("EnableAdfsAuthentication", false);

            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Once());
            IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.GetEnvironment("KaTaL");

            Assert.Equal(env.Name, cmdlet.Name);
            Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
            Assert.False(env.OnPremise);

            // Update onpremise to true
            var cmdlet2 = new SetAzureRMEnvironmentCommand()
            {
                CommandRuntime           = commandRuntimeMock.Object,
                Name                     = "Katal",
                EnableAdfsAuthentication = true
            };

            cmdlet2.MyInvocation.BoundParameters.Add("Name", "Katal");
            cmdlet2.MyInvocation.BoundParameters.Add("EnableAdfsAuthentication", true);

            cmdlet2.InvokeBeginProcessing();
            cmdlet2.ExecuteCmdlet();
            cmdlet2.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Exactly(2));
            IAzureEnvironment env2 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("KaTaL");

            Assert.Equal(env2.Name, cmdlet2.Name);
            Assert.Equal(env2.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
            Assert.True(env2.OnPremise);

            // Update gallery endpoint
            var cmdlet3 = new SetAzureRMEnvironmentCommand()
            {
                CommandRuntime  = commandRuntimeMock.Object,
                Name            = "Katal",
                GalleryEndpoint = "http://galleryendpoint.com",
            };

            cmdlet3.MyInvocation.BoundParameters.Add("Name", "Katal");
            cmdlet3.MyInvocation.BoundParameters.Add("GalleryEndpoint", "http://galleryendpoint.com");

            cmdlet3.InvokeBeginProcessing();
            cmdlet3.ExecuteCmdlet();
            cmdlet3.InvokeEndProcessing();

            // Ensure gallery endpoint is updated and OnPremise value is preserved
            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Exactly(3));
            IAzureEnvironment env3 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("KaTaL");

            Assert.Equal(env3.Name, cmdlet3.Name);
            Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
            Assert.True(env3.OnPremise);
            Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.Gallery), cmdlet3.GalleryEndpoint);
        }
Beispiel #18
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);
        }
Beispiel #19
0
 public override bool CanAuthenticate(IAzureAccount account, IAzureEnvironment environment, string tenant, SecureString password, string promptBehavior, Task <Action <string> > promptAction, IAzureTokenCache tokenCache, string resourceId)
 {
     return(account?.Type == AzureAccount.AccountType.User && environment != null && !string.IsNullOrEmpty(environment.GetEndpoint(resourceId)) && !string.IsNullOrWhiteSpace(tenant) && password != null && tokenCache != null);
 }
Beispiel #20
0
        public void SetEnvironmentForMultipleContexts()
        {
            // Add new environment
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            SetupConfirmation(commandRuntimeMock);
            var cmdlet = new AddAzureRMEnvironmentCommand()
            {
                CommandRuntime         = commandRuntimeMock.Object,
                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();
            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"]);

            // Create contexts using the new environment
            var    profile = new AzureRmProfile();
            string contextName1;
            var    context1 = (new AzureContext {
                Environment = env
            })
                              .WithAccount(new AzureAccount {
                Id = "*****@*****.**"
            })
                              .WithTenant(new AzureTenant {
                Id = Guid.NewGuid().ToString(), Directory = "contoso.com"
            })
                              .WithSubscription(new AzureSubscription {
                Id = Guid.NewGuid().ToString(), Name = "Contoso Subscription 1"
            });

            profile.TryAddContext(context1, out contextName1);
            string contextName2;
            var    context2 = (new AzureContext {
                Environment = env
            })
                              .WithAccount(new AzureAccount {
                Id = "*****@*****.**"
            })
                              .WithTenant(new AzureTenant {
                Id = Guid.NewGuid().ToString(), Directory = "contoso.cn"
            })
                              .WithSubscription(new AzureSubscription {
                Id = Guid.NewGuid().ToString(), Name = "Contoso Subscription 2"
            });

            profile.TryAddContext(context2, out contextName2);
            profile.TrySetDefaultContext(context1);
            AzureRmProfileProvider.Instance.Profile = profile;

            // Update the environment with new endpoints
            commandRuntimeMock = new Mock <ICommandRuntime>();
            SetupConfirmation(commandRuntimeMock);
            var cmdlet2 = new AddAzureRMEnvironmentCommand()
            {
                CommandRuntime         = commandRuntimeMock.Object,
                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"]);

            // Validate that the endpoints were updated in the contexts
            profile = (AzureRmProfile)AzureRmProfileProvider.Instance.Profile;
            Assert.NotNull(profile);
            Assert.NotNull(profile.Contexts);
            Assert.NotEmpty(profile.Contexts);
            foreach (var context in profile.Contexts.Values)
            {
                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);
            }
        }