Exemplo n.º 1
0
        [RunOnlyOnPlatforms(Windows = true, OSX = true)] // Comment this attribute to run this tests on Linux with Libsecret enabled
        public async Task DefaultAzureCredential_UseVisualStudioCodeCredential()
        {
            var options = Recording.InstrumentClientOptions(new DefaultAzureCredentialOptions
            {
                ExcludeEnvironmentCredential        = true,
                ExcludeInteractiveBrowserCredential = true,
                ExcludeSharedTokenCacheCredential   = true,
                VisualStudioCodeTenantId            = TestEnvironment.TestTenantId
            });

            var cloudName  = Guid.NewGuid().ToString();
            var fileSystem = CredentialTestHelpers.CreateFileSystemForVisualStudioCode(TestEnvironment, cloudName);
            var process    = new TestProcess {
                Error = "Error"
            };

            var factory    = new TestDefaultAzureCredentialFactory(options, fileSystem, new TestProcessService(process), default);
            var credential = InstrumentClient(new DefaultAzureCredential(factory, options));

            AccessToken token;

            using (await CredentialTestHelpers.CreateRefreshTokenFixtureAsync(TestEnvironment, Mode, ExpectedServiceName, cloudName))
            {
                token = await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://vault.azure.net/.default" }), CancellationToken.None);
            }

            Assert.IsNotNull(token.Token);
        }
        public async Task AuthenticateWithVscCredential()
        {
            var cloudName = Guid.NewGuid().ToString();

            var fileSystem = CredentialTestHelpers.CreateFileSystemForVisualStudioCode(TestEnvironment, cloudName);

            using IDisposable fixture = await CredentialTestHelpers.CreateRefreshTokenFixtureAsync(TestEnvironment, Mode, ExpectedServiceName, cloudName);

            var options = InstrumentClientOptions(new VisualStudioCodeCredentialOptions {
                TenantId = TestEnvironment.TestTenantId
            });
            VisualStudioCodeCredential credential = InstrumentClient(new VisualStudioCodeCredential(options, default, default, fileSystem, default));
Exemplo n.º 3
0
        public async Task DefaultAzureCredential_UseVisualStudioCodeCredential_ParallelCalls()
        {
            var options = InstrumentClientOptions(new DefaultAzureCredentialOptions
            {
                ExcludeEnvironmentCredential        = true,
                ExcludeInteractiveBrowserCredential = true,
                ExcludeSharedTokenCacheCredential   = true,
                ExcludeManagedIdentityCredential    = true,
                VisualStudioCodeTenantId            = TestEnvironment.TestTenantId
            });

            var cloudName      = Guid.NewGuid().ToString();
            var fileSystem     = CredentialTestHelpers.CreateFileSystemForVisualStudioCode(TestEnvironment, cloudName);
            var processService = new TestProcessService {
                CreateHandler = psi => new TestProcess {
                    Error = "Error"
                }
            };

            var factory = new TestDefaultAzureCredentialFactory(options, fileSystem, processService, default)
            {
                ManagedIdentitySourceFactory = () => default
            };
            var credential = InstrumentClient(new DefaultAzureCredential(factory, options));

            var tasks = new List <Task <AccessToken> >();

            using (await CredentialTestHelpers.CreateRefreshTokenFixtureAsync(TestEnvironment, Mode, ExpectedServiceName, cloudName))
            {
                for (int i = 0; i < 10; i++)
                {
                    tasks.Add(Task.Run(async() => await credential.GetTokenAsync(new TokenRequestContext(new[] { TestEnvironment.KeyvaultScope }), CancellationToken.None)));
                }

                await Task.WhenAll(tasks);
            }

            foreach (Task <AccessToken> task in tasks)
            {
                Assert.IsNotNull(task.Result.Token);
            }
        }
Exemplo n.º 4
0
        public async Task DefaultAzureCredential_UseVisualStudioCodeCredential()
        {
            var options = InstrumentClientOptions(new DefaultAzureCredentialOptions
            {
                ExcludeEnvironmentCredential        = true,
                ExcludeInteractiveBrowserCredential = true,
                ExcludeSharedTokenCacheCredential   = true,
                ExcludeManagedIdentityCredential    = true,
                ExcludeVisualStudioCredential       = true,
                VisualStudioCodeTenantId            = TestEnvironment.TestTenantId
            });

            var cloudName  = Guid.NewGuid().ToString();
            var fileSystem = CredentialTestHelpers.CreateFileSystemForVisualStudioCode(TestEnvironment, cloudName);
            var process    = new TestProcess {
                Error = "Error"
            };

            var factory = new TestDefaultAzureCredentialFactory(options, fileSystem, new TestProcessService(process), default)
            {
                ManagedIdentitySourceFactory = () => default
            };
            var credential = InstrumentClient(new DefaultAzureCredential(factory, options));

            AccessToken token;
            List <ClientDiagnosticListener.ProducedDiagnosticScope> scopes;

            using (await CredentialTestHelpers.CreateRefreshTokenFixtureAsync(TestEnvironment, Mode, ExpectedServiceName, cloudName))
                using (ClientDiagnosticListener diagnosticListener = new ClientDiagnosticListener(s => s.StartsWith("Azure.Identity")))
                {
                    token = await credential.GetTokenAsync(new TokenRequestContext(new[] { TestEnvironment.KeyvaultScope }), CancellationToken.None);

                    scopes = diagnosticListener.Scopes;
                }

            Assert.IsNotNull(token.Token);

            Assert.AreEqual(2, scopes.Count);
            Assert.AreEqual($"{nameof(DefaultAzureCredential)}.{nameof(DefaultAzureCredential.GetToken)}", scopes[0].Name);
            Assert.AreEqual($"{nameof(VisualStudioCodeCredential)}.{nameof(VisualStudioCodeCredential.GetToken)}", scopes[1].Name);
        }