Exemple #1
0
        /// <summary>
        /// Load global aliases for ARM
        /// </summary>
        public void OnImport()
        {
#if DEBUG
            try
            {
#endif
            AzureSessionInitializer.InitializeAzureSession();
            ResourceManagerProfileProvider.InitializeResourceManagerProfile();
#if DEBUG
            if (!TestMockSupport.RunningMocked)
            {
#endif
            AzureSession.Instance.DataStore = new DiskDataStore();
#if DEBUG
        }
#endif
            System.Management.Automation.PowerShell invoker = null;
            invoker = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace);
            invoker.AddScript(File.ReadAllText(FileUtilities.GetContentFilePath(
                                                   Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                                                   "AzureRmProfileStartup.ps1")));
            invoker.Invoke();
#if DEBUG
        }
        catch (Exception) when(TestMockSupport.RunningMocked)
        {
            // This will throw exception for tests, ignore.
        }
#endif
        }
Exemple #2
0
        public ArgumentCompleterTests(ITestOutputHelper output)
        {
            TestExecutionHelpers.SetUpSessionAndProfile();
            ResourceManagerProfileProvider.InitializeResourceManagerProfile(true);

            xunitLogger = new XunitTracingInterceptor(output);
        }
 public override void ExecuteCmdlet()
 {
     if (MyInvocation.BoundParameters.ContainsKey(nameof(Scope)) && Scope == ContextModificationScope.Process)
     {
         ConfirmAction("Do not autosave the context in the current session", "Current session", () =>
         {
             ModifyContext((profile, client) =>
             {
                 ContextAutosaveSettings settings = null;
                 AzureSession.Modify((session) => DisableAutosave(session, false, out settings));
                 ResourceManagerProfileProvider.InitializeResourceManagerProfile(true);
                 AzureRmProfileProvider.Instance.Profile = profile;
                 WriteObject(settings);
             });
         });
     }
     else
     {
         ConfirmAction("Never autosave the context for the current user", "Current user",
                       () =>
         {
             ModifyContext((profile, client) =>
             {
                 ContextAutosaveSettings settings = null;
                 AzureSession.Modify((session) => DisableAutosave(session, true, out settings));
                 ResourceManagerProfileProvider.InitializeResourceManagerProfile(true);
                 AzureRmProfileProvider.Instance.Profile = profile;
                 WriteObject(settings);
             });
         });
     }
 }
Exemple #4
0
        /// <summary>
        /// Initialize the profile provider based on the autosave setting
        /// </summary>
        internal void InitializeProfileProvider(bool useAutoSaveProfile = false)
        {
#if DEBUG
            if (!TestMockSupport.RunningMocked)
            {
#endif
            if (useAutoSaveProfile)
            {
                ProtectedProfileProvider.InitializeResourceManagerProfile();
            }
            else
            {
                switch (GetContextModificationScope())
                {
                case ContextModificationScope.Process:
                    ResourceManagerProfileProvider.InitializeResourceManagerProfile();
                    break;

                case ContextModificationScope.CurrentUser:
                    ProtectedProfileProvider.InitializeResourceManagerProfile();
                    break;
                }
            }
#if DEBUG
        }

        else
        {
            ResourceManagerProfileProvider.InitializeResourceManagerProfile();
        }
#endif
        }
 protected void SetupSessionAndProfile()
 {
     AzureSessionInitializer.InitializeAzureSession();
     AzureSession.Instance.ARMContextSaveMode = ContextSaveMode.Process;
     ResourceManagerProfileProvider.InitializeResourceManagerProfile();
     if (!(AzureSession.Instance?.DataStore is MemoryDataStore))
     {
         AzureSession.Instance.DataStore = new MemoryDataStore();
     }
 }
Exemple #6
0
 void ResetState()
 {
     TestExecutionHelpers.SetUpSessionAndProfile();
     ResourceManagerProfileProvider.InitializeResourceManagerProfile(true);
     AzureSession.Instance.DataStore             = dataStore;
     AzureSession.Instance.ARMContextSaveMode    = ContextSaveMode.Process;
     AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
     AzureSession.Instance.TokenCache            = new AuthenticationStoreTokenCache(new AzureTokenCache());
     Environment.SetEnvironmentVariable("Azure_PS_Data_Collection", "");
 }
Exemple #7
0
        void ResetState()
        {
            TestExecutionHelpers.SetUpSessionAndProfile();
            ResourceManagerProfileProvider.InitializeResourceManagerProfile(true);
            // prevent token acquisition
            AzureRmProfileProvider.Instance.GetProfile <AzureRmProfile>().ShouldRefreshContextsFromCache = false;
            AzureSession.Instance.DataStore             = dataStore;
            AzureSession.Instance.ARMContextSaveMode    = ContextSaveMode.Process;
            AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
            Environment.SetEnvironmentVariable("Azure_PS_Data_Collection", "false");
            PowerShellTokenCacheProvider tokenProvider = new InMemoryTokenCacheProvider();

            AzureSession.Instance.RegisterComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, () => tokenProvider, true);
        }
        /// <summary>
        /// Initialize the profile provider based on the autosave setting
        /// </summary>
        internal bool InitializeProfileProvider(bool useAutoSaveProfile = false)
        {
#if DEBUG
            if (!TestMockSupport.RunningMocked)
            {
#endif
            if (useAutoSaveProfile)
            {
                try
                {
                    ProtectedProfileProvider.InitializeResourceManagerProfile();
                }
                catch (SystemException e)
                {
                    if (!(e is IOException) && !(e is UnauthorizedAccessException))
                    {
                        throw e;
                    }
                    WriteInitializationWarnings(string.Format(Resources.ProfileFileNotAccessible, e.Message));
                    ResourceManagerProfileProvider.InitializeResourceManagerProfile(true);
                }
            }

            if (null == AzureRmProfileProvider.Instance)
            {
                switch (GetContextModificationScope())
                {
                case ContextModificationScope.Process:
                    ResourceManagerProfileProvider.InitializeResourceManagerProfile();
                    break;

                case ContextModificationScope.CurrentUser:
                    ProtectedProfileProvider.InitializeResourceManagerProfile();
                    break;
                }
            }
#if DEBUG
        }

        else
        {
            ResourceManagerProfileProvider.InitializeResourceManagerProfile();
        }
#endif
            return(AzureRmProfileProvider.Instance is ProtectedProfileProvider);
        }
        public override void ExecuteCmdlet()
        {
            SecureString password = null;

            if (_credential != null)
            {
                Account.Id = _credential.UserName;
                password   = _credential.Password;
            }

            if (!string.IsNullOrEmpty(ApplicationId))
            {
                Account.Id = ApplicationId;
            }

            if (!string.IsNullOrEmpty(CertificateThumbprint))
            {
                Account.SetThumbprint(CertificateThumbprint);
            }

            if (!string.IsNullOrEmpty(TenantId))
            {
                Account.SetProperty(AzureAccount.Property.Tenants, new[] { TenantId });
            }

#if NETSTANDARD
            if (!string.IsNullOrEmpty(Password))
            {
                Account.SetProperty(AzureAccount.Property.ServicePrincipalSecret, Password);
            }
#endif

            if (AzureRmProfileProvider.Instance.Profile == null)
            {
                ResourceManagerProfileProvider.InitializeResourceManagerProfile(true);
            }

            Login(
                Account,
                _environment,
                TenantId,
                SubscriptionId,
                SubscriptionName,
                password,
                (s) => Console.WriteLine(s));
        }
Exemple #10
0
        public SilentReAuthByTenantCmdletTest(ITestOutputHelper output)
        {
            ResourceManagerProfileProvider.InitializeResourceManagerProfile();
            xunitLogger = new XunitTracingInterceptor(output);
            XunitTracingInterceptor.AddToContext(xunitLogger);

            commandRuntimeMock.Setup(f => f.WriteObject(It.IsAny <object>(), It.IsAny <bool>())).Callback(
                (Object o, bool enumerateCollection) =>
            {
                if (enumerateCollection)
                {
                    IEnumerable <object> objects = o as IEnumerable <object>;
                    objects?.ForEach(e => OutputPipeline.Add(e));
                }
                else
                {
                    OutputPipeline.Add(o);
                }
            });

            commandRuntimeMock.Setup(f => f.WriteDebug(It.IsAny <string>())).Callback((string message) => DebugPipeline.Add(message));

            commandRuntimeMock.Setup(f => f.WriteWarning(It.IsAny <string>())).Callback((string message) => WarnPipeline.Add(message));

            cmdlet = new GetAzureRMTenantCommandMock()
            {
                CommandRuntime = commandRuntimeMock.Object,
            };

            var sub = new AzureSubscription()
            {
                Id   = Guid.NewGuid().ToString(),
                Name = "Test subscription"
            };

            defaultContext = new AzureContext(sub,
                                              new AzureAccount()
            {
                Id = "*****@*****.**", Type = AzureAccount.AccountType.User
            },
                                              AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
                                              new AzureTenant()
            {
                Id = Guid.NewGuid().ToString()
            });
        }
        /// <summary>
        /// Initialize the profile provider based on the autosave setting
        /// </summary>
        internal bool InitializeProfileProvider(bool useAutoSaveProfile = false)
        {
#if DEBUG
            if (!TestMockSupport.RunningMocked)
            {
#endif
            if (useAutoSaveProfile)
            {
                try
                {
                    ProtectedProfileProvider.InitializeResourceManagerProfile();
                }
                catch (Exception e)
                {
                    //Likely the exception is related to IO or permission, fallback to Process save mode
                    WriteInitializationWarnings(string.Format(Resources.ProfileFileNotAccessible, e.Message));
                    ResourceManagerProfileProvider.InitializeResourceManagerProfile(true);
                }
            }

            if (null == AzureRmProfileProvider.Instance)
            {
                switch (GetContextModificationScope())
                {
                case ContextModificationScope.Process:
                    ResourceManagerProfileProvider.InitializeResourceManagerProfile();
                    break;

                case ContextModificationScope.CurrentUser:
                    ProtectedProfileProvider.InitializeResourceManagerProfile();
                    break;
                }
            }
#if DEBUG
        }

        else
        {
            ResourceManagerProfileProvider.InitializeResourceManagerProfile();
        }
#endif
            return(AzureRmProfileProvider.Instance is ProtectedProfileProvider);
        }
Exemple #12
0
        /// <summary>
        ///     Add Site Recovery aliases
        /// </summary>
        public void OnImport()
        {
            try
            {
                AzureSessionInitializer.InitializeAzureSession();
                ResourceManagerProfileProvider.InitializeResourceManagerProfile();

                System.Management.Automation.PowerShell invoker = null;
                invoker = System.Management.Automation.PowerShell.Create(
                    RunspaceMode.CurrentRunspace);
                invoker.AddScript(
                    File.ReadAllText(
                        FileUtilities.GetContentFilePath(
                            Path.GetDirectoryName(
                                Assembly.GetExecutingAssembly()
                                .Location),
                            "RecoveryServicesAsrStartup.ps1")));
                invoker.Invoke();
            }
            catch
            {
                // This may throw exception for tests, ignore.
            }
        }
Exemple #13
0
 /// <summary>
 /// Set up the session and profile for a test
 /// </summary>
 public static void SetUpSessionAndProfile()
 {
     AzureSessionInitializer.InitializeAzureSession();
     ResourceManagerProfileProvider.InitializeResourceManagerProfile();
 }
 /// <summary>
 /// Set up the session and profile for a test
 /// </summary>
 public static void SetUpSessionAndProfile()
 {
     AzureSessionInitializer.InitializeAzureSession();
     AzureSession.Instance.ARMContextSaveMode = ContextSaveMode.Process;
     ResourceManagerProfileProvider.InitializeResourceManagerProfile();
 }