void ClearContext(AzureRmProfile profile, RMProfileClient client)
        {
            bool result = false;

            if (profile != null)
            {
                var contexts = profile.Contexts.Values;
                foreach (var context in contexts)
                {
                    client.TryRemoveContext(context);
                }

                PowerShellTokenCacheProvider tokenCacheProvider;
                if (!AzureSession.Instance.TryGetComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, out tokenCacheProvider))
                {
                    WriteWarning(Resources.ClientFactoryNotRegisteredClear);
                }
                else
                {
                    tokenCacheProvider.ClearCache();
                    var defaultContext = new AzureContext();
                    profile.TrySetDefaultContext(defaultContext);
                    result = true;
                }
            }

            AzureSession.Instance.RaiseContextClearedEvent();

            if (PassThru.IsPresent)
            {
                WriteObject(result);
            }
        }
        void ClearContext(AzureRmProfile profile, RMProfileClient client)
        {
            bool result = false;

            if (profile != null)
            {
                var contexts = profile.Contexts.Values;
                foreach (var context in contexts)
                {
                    client.TryRemoveContext(context);
                }

                var defaultContext = new AzureContext();
                var cache          = AzureSession.Instance.TokenCache;
                if (GetContextModificationScope() == ContextModificationScope.CurrentUser)
                {
                    var fileCache = cache as ProtectedFileTokenCache;
                    if (fileCache == null)
                    {
                        try
                        {
                            var session = AzureSession.Instance;
                            fileCache = new ProtectedFileTokenCache(Path.Combine(session.TokenCacheDirectory, session.TokenCacheFile), session.DataStore);
                            fileCache.Clear();
                        }
                        catch
                        {
                            // ignore exceptions from creating a token cache
                        }
                    }

                    cache.Clear();
                }
                else
                {
                    var localCache = cache as AuthenticationStoreTokenCache;
                    if (localCache != null)
                    {
                        localCache.Clear();
                    }
                }

                defaultContext.TokenCache = cache;
                profile.TrySetDefaultContext(defaultContext);
                result = true;
            }

            if (PassThru.IsPresent)
            {
                WriteObject(result);
            }
        }
Ejemplo n.º 3
0
        AzureRmProfile CreateMultipleContextProfile()
        {
            var    profile = new AzureRmProfile();
            string contextName1;
            var    context1 = (new AzureContext {
                Environment = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud]
            })
                              .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 = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureChinaCloud]
            })
                              .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);
            string contextName3;
            var    context3 = (new AzureContext {
                Environment = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureGermanCloud]
            })
                              .WithAccount(new AzureAccount {
                Id = "*****@*****.**"
            })
                              .WithTenant(new AzureTenant {
                Id = Guid.NewGuid().ToString(), Directory = "contoso.de"
            })
                              .WithSubscription(new AzureSubscription {
                Id = Guid.NewGuid().ToString(), Name = "Contoso Subscription 3"
            });

            profile.TryAddContext(context3, out contextName3);
            profile.TrySetDefaultContext(context1);
            return(profile);
        }
        public void SetDefaultContextCopiesAccountProperties()
        {
            TestExecutionHelpers.SetUpSessionAndProfile();
            var context    = new AzureContext();
            var newContext = new AzureContext();

            context.CopyFrom(context1);
            newContext.CopyFrom(context1);
            var token = Guid.NewGuid().ToString();

            newContext.Account.SetProperty(AzureAccount.Property.AccessToken, token);
            var profile = new AzureRmProfile();

            profile.DefaultContext = context;
            Assert.True(profile.TrySetDefaultContext(newContext));
            Assert.True(profile.DefaultContext.Account.IsPropertySet(AzureAccount.Property.AccessToken));
            Assert.Equal(token, profile.DefaultContext.Account.GetAccessToken());
        }
Ejemplo n.º 5
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);
            }
        }