/// <summary>
        /// Load global aliases for ARM
        /// </summary>
        public void OnImport()
        {
#if DEBUG
            try
            {
#endif
            AzureSessionInitializer.InitializeAzureSession();
#if DEBUG
            if (!TestMockSupport.RunningMocked)
            {
#endif
            AzureSession.Instance.DataStore = new DiskDataStore();
#if DEBUG
        }
#endif

            var autoSaveEnabled  = AzureSession.Instance.ARMContextSaveMode == ContextSaveMode.CurrentUser;
            var autosaveVariable = System.Environment.GetEnvironmentVariable(AzureProfileConstants.AzureAutosaveVariable);
            bool localAutosave;
            if (bool.TryParse(autosaveVariable, out localAutosave))
            {
                autoSaveEnabled = localAutosave;
            }

            if (!InitializeProfileProvider(autoSaveEnabled))
            {
                DisableAutosave(AzureSession.Instance);
            }

            IServicePrincipalKeyStore keyStore =
// TODO: Remove IfDef
#if NETSTANDARD
                new AzureRmServicePrincipalKeyStore(AzureRmProfileProvider.Instance.Profile);
#else
                new AzureRmServicePrincipalKeyStore();
#endif
            AzureSession.Instance.RegisterComponent(ServicePrincipalKeyStore.Name, () => keyStore);
#if DEBUG
        }
        catch (Exception) when(TestMockSupport.RunningMocked)
        {
            // This will throw exception for tests, ignore.
        }
#endif
        }
Пример #2
0
        public LoginTests()
        {
            AzureSessionInitializer.InitializeAzureSession();
            ProtectedProfileProvider.InitializeResourceManagerProfile();
            IServicePrincipalKeyStore keyStore = new AzureRmServicePrincipalKeyStore(AzureRmProfileProvider.Instance.Profile);

            AzureSession.Instance.RegisterComponent(ServicePrincipalKeyStore.Name, () => keyStore);

            ContextAutosaveSettings settings = null;

            AzureSession.Modify((session) => EnableAutosave(session, true, out settings));
            ProtectedProfileProvider.InitializeResourceManagerProfile(true);

            _cmdlet                       = new ConnectAccount();
            _cmdlet.TenantId              = _tenantId;
            _cmdlet.SubscriptionId        = _subscriptionId;
            _cmdlet.SubscriptionName      = _subscriptionName;
            _cmdlet.UserName              = _userName;
            _cmdlet.Password              = _password;
            _cmdlet.ApplicationId         = _applicationId;
            _cmdlet.CertificateThumbprint = _certificateThumbprint;
            _cmdlet.CommandRuntime        = new MockCommandRuntime();
        }
Пример #3
0
        /// <summary>
        /// Load global aliases for ARM
        /// </summary>
        public void OnImport()
        {
#if DEBUG
            try
            {
#endif
            AzureSessionInitializer.InitializeAzureSession();
            AzureSessionInitializer.MigrateAdalCache(AzureSession.Instance, GetAzureContextContainer, WriteInitializationWarnings);
#if DEBUG
            if (!TestMockSupport.RunningMocked)
            {
#endif
            AzureSession.Instance.DataStore = new DiskDataStore();
#if DEBUG
        }
#endif

            var autoSaveEnabled  = AzureSession.Instance.ARMContextSaveMode == ContextSaveMode.CurrentUser;
            var autosaveVariable = System.Environment.GetEnvironmentVariable(AzureProfileConstants.AzureAutosaveVariable);

            if (bool.TryParse(autosaveVariable, out bool localAutosave))
            {
                autoSaveEnabled = localAutosave;
            }

            try
            {
                if (autoSaveEnabled && !TokenCachePersistenceChecker.Verify())
                {
                    // If token cache persistence is not supported, fall back to plain text persistence, and print a warning
                    // We cannot just throw an exception here because this is called when importing the module
                    WriteInitializationWarnings(Resources.TokenCacheEncryptionNotSupportedWithFallback);
                }
            }
            catch (Exception ex)
            {
                //Likely the exception is related permission, fall back context save mode to process
                autoSaveEnabled = false;
                AzureSession.Instance.ARMContextSaveMode = ContextSaveMode.Process;
                WriteInitializationWarnings(Resources.FallbackContextSaveModeDueCacheCheckError.FormatInvariant(ex.Message));
            }

            if (!InitializeProfileProvider(autoSaveEnabled))
            {
                AzureSession.Instance.ARMContextSaveMode = ContextSaveMode.Process;
                autoSaveEnabled = false;
            }

            IServicePrincipalKeyStore keyStore =
                new AzureRmServicePrincipalKeyStore(AzureRmProfileProvider.Instance.Profile);
            AzureSession.Instance.RegisterComponent(ServicePrincipalKeyStore.Name, () => keyStore);

            IAuthenticatorBuilder builder = null;
            if (!AzureSession.Instance.TryGetComponent(AuthenticatorBuilder.AuthenticatorBuilderKey, out builder))
            {
                builder = new DefaultAuthenticatorBuilder();
                AzureSession.Instance.RegisterComponent(AuthenticatorBuilder.AuthenticatorBuilderKey, () => builder);
            }

            PowerShellTokenCacheProvider provider = null;
            if (autoSaveEnabled)
            {
                provider = new SharedTokenCacheProvider();
            }
            else     // if autosave is disabled, or the shared factory fails to initialize, we fallback to in memory
            {
                provider = new InMemoryTokenCacheProvider();
            }
            var tokenCache = provider.GetTokenCache();
            IAzureEventListenerFactory azureEventListenerFactory = new AzureEventListenerFactory();
            AzureSession.Instance.RegisterComponent(nameof(CommonUtilities), () => new CommonUtilities());
            AzureSession.Instance.RegisterComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, () => provider);
            AzureSession.Instance.RegisterComponent(nameof(IAzureEventListenerFactory), () => azureEventListenerFactory);
            AzureSession.Instance.RegisterComponent(nameof(PowerShellTokenCache), () => tokenCache);
            AzureSession.Instance.RegisterComponent(nameof(AzureCredentialFactory), () => new AzureCredentialFactory());
            AzureSession.Instance.RegisterComponent(nameof(MsalAccessTokenAcquirerFactory), () => new MsalAccessTokenAcquirerFactory());
#if DEBUG
        }
        catch (Exception) when(TestMockSupport.RunningMocked)
        {
            // This will throw exception for tests, ignore.
        }
#endif
        }