private ServiceBusConnectionInformation ResolveConnectionInformation(string connection)
        {
            var connectionSetting = connection ?? Constants.DefaultConnectionStringName;
            IConfigurationSection connectionSection = _configuration.GetWebJobsConnectionStringSectionServiceBus(connectionSetting);

            if (!connectionSection.Exists())
            {
                // Not found
                throw new InvalidOperationException($"Service Bus account connection string '{connectionSetting}' does not exist. " +
                                                    $"Make sure that it is a defined App Setting.");
            }

            if (!string.IsNullOrWhiteSpace(connectionSection.Value))
            {
                return(new ServiceBusConnectionInformation(connectionSection.Value));
            }
            else
            {
                string fullyQualifiedNamespace = connectionSection["fullyQualifiedNamespace"];
                if (string.IsNullOrWhiteSpace(fullyQualifiedNamespace))
                {
                    // Not found
                    throw new InvalidOperationException($"Connection should have an 'fullyQualifiedNamespace' property or be a " +
                                                        $"string representing a connection string.");
                }

                TokenCredential credential = _componentFactory.CreateTokenCredential(connectionSection);
                return(new ServiceBusConnectionInformation(fullyQualifiedNamespace, credential));
            }
        }
Esempio n. 2
0
        public void UsesDefaultCredentialWhenNoneExists()
        {
            var configuration = GetConfiguration();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddAzureClients(builder => builder.UseCredential(new EnvironmentCredential()));

            ServiceProvider       provider   = serviceCollection.BuildServiceProvider();
            AzureComponentFactory factory    = provider.GetService <AzureComponentFactory>();
            TokenCredential       credential = factory.CreateTokenCredential(configuration);

            Assert.IsInstanceOf <EnvironmentCredential>(credential);
        }
        public static bool TryGetNamedEndpointFromIdentity(this IConfigurationSection section, AzureComponentFactory azureComponentFactory, out ServiceEndpoint endpoint)
        {
            var text = section["ServiceUri"];

            if (text != null)
            {
                var key        = section.Key;
                var value      = section.GetValue("Type", EndpointType.Primary);
                var credential = azureComponentFactory.CreateTokenCredential(section);
                endpoint = new ServiceEndpoint(new Uri(text), credential, value, key);
                return(true);
            }

            endpoint = null;
            return(false);
        }
Esempio n. 4
0
        public static bool TryGetEndpointFromIdentity(this IConfigurationSection section, AzureComponentFactory azureComponentFactory, out ServiceEndpoint endpoint, bool isNamed = true)
        {
            var text = section[ServiceUriKey];

            if (text != null)
            {
                var key            = section.Key;
                var name           = isNamed ? key : string.Empty;
                var value          = section.GetValue(TypeKey, EndpointType.Primary);
                var credential     = azureComponentFactory.CreateTokenCredential(section);
                var serverEndpoint = section.GetValue <Uri>(ServerEndpointKey);
                var clientEndpoint = section.GetValue <Uri>(ClientEndpointKey);
                endpoint = new ServiceEndpoint(new Uri(text), credential, value, name, serverEndpoint, clientEndpoint);
                return(true);
            }

            endpoint = null;
            return(false);
        }
        public virtual TClient Create(string name, IConfiguration configuration)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                name = ConnectionStringNames.Storage; // default
            }

            IConfigurationSection connectionSection = configuration?.GetWebJobsConnectionSection(name);

            if (connectionSection == null || !connectionSection.Exists())
            {
                // Not found
                throw new InvalidOperationException($"Storage account connection string '{IConfigurationExtensions.GetPrefixedConnectionStringName(name)}' does not exist. Make sure that it is a defined App Setting.");
            }

            var credential = _componentFactory.CreateTokenCredential(connectionSection);
            var options    = CreateClientOptions(connectionSection);

            return(CreateClient(connectionSection, credential, options));
        }
Esempio n. 6
0
        public void CanCreateCredential()
        {
            var configuration = GetConfiguration(
                new KeyValuePair <string, string>("TestClient:clientId", "ConfigurationClientId"),
                new KeyValuePair <string, string>("TestClient:clientSecret", "ConfigurationClientSecret"),
                new KeyValuePair <string, string>("TestClient:tenantId", "ConfigurationTenantId"));

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddAzureClientsCore();

            ServiceProvider       provider   = serviceCollection.BuildServiceProvider();
            AzureComponentFactory factory    = provider.GetService <AzureComponentFactory>();
            TokenCredential       credential = factory.CreateTokenCredential(configuration.GetSection("TestClient"));

            Assert.IsInstanceOf <ClientSecretCredential>(credential);
            var clientSecretCredential = (ClientSecretCredential)credential;

            Assert.AreEqual("ConfigurationClientId", clientSecretCredential.ClientId);
            Assert.AreEqual("ConfigurationClientSecret", clientSecretCredential.ClientSecret);
            Assert.AreEqual("ConfigurationTenantId", clientSecretCredential.TenantId);
        }
        public virtual TClient Get(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                name = ConnectionStringNames.Storage; // default
            }

            // $$$ Where does validation happen?
            IConfigurationSection connectionSection = _configuration.GetWebJobsConnectionStringSection(name);

            if (!connectionSection.Exists())
            {
                // Not found
                throw new InvalidOperationException($"Storage account connection string '{IConfigurationExtensions.GetPrefixedConnectionStringName(name)}' does not exist. Make sure that it is a defined App Setting.");
            }

            _logForwarder.Start();

            if (!string.IsNullOrWhiteSpace(connectionSection.Value))
            {
                return(CreateClientFromConnectionString(connectionSection.Value, CreateClientOptions(null)));
            }

            var endpoint = connectionSection["endpoint"];

            if (string.IsNullOrWhiteSpace(endpoint))
            {
                // Not found
                throw new InvalidOperationException($"Connection should have an 'endpoint' property or be a string representing a connection string.");
            }

            var credential  = _componentFactory.CreateTokenCredential(connectionSection);
            var endpointUri = new Uri(endpoint);

            return(CreateClientFromTokenCredential(endpointUri, credential, CreateClientOptions(connectionSection)));
        }
Esempio n. 8
0
            public Task <object> GetValueAsync()
            {
                Type clientOptionType = null;

                foreach (var constructor in _clientType.GetConstructors(BindingFlags.Public | BindingFlags.Instance))
                {
                    var lastParameter = constructor.GetParameters().LastOrDefault();
                    if (lastParameter != null && typeof(ClientOptions).IsAssignableFrom(lastParameter.ParameterType))
                    {
                        clientOptionType = lastParameter.ParameterType;
                        break;
                    }
                }

                if (clientOptionType == null)
                {
                    throw new InvalidOperationException("Unable to detect the client option type");
                }

                var credential = _componentFactory.CreateTokenCredential(_configuration);
                var options    = _componentFactory.CreateClientOptions(clientOptionType, null, _configuration);

                return(Task.FromResult(_componentFactory.CreateClient(_clientType, _configuration, credential, options)));
            }