Skip to content

Commit 34c1143

Browse files
authored
Merge pull request #1104 from SteeltoeOSS/backport_1103
Backport CNB override of ConnectionString in Connectors
2 parents 5f8ef70 + 8f6ab5a commit 34c1143

24 files changed

+180
-24
lines changed

src/Connectors/Connectors.sln

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio Version 16
3-
VisualStudioVersion = 16.0.28729.10
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.4.33213.308
44
MinimumVisualStudioVersion = 15.0.26124.0
55
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{9BB0395E-B595-4B95-89ED-F55AF12B4EE7}"
66
EndProject
@@ -59,7 +59,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "External.Connector.Test", "
5959
EndProject
6060
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Connector.ConnectorBase", "src\ConnectorBase\Steeltoe.Connector.ConnectorBase.csproj", "{EDB042A3-A6DB-4400-84C4-DF467FA01B8C}"
6161
EndProject
62-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Steeltoe.Common.Utils", "..\Common\src\Common.Utils\Steeltoe.Common.Utils.csproj", "{B0A47904-1C27-4380-9327-986F762B0250}"
62+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Common.Utils", "..\Common\src\Common.Utils\Steeltoe.Common.Utils.csproj", "{B0A47904-1C27-4380-9327-986F762B0250}"
63+
EndProject
64+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Extensions.Configuration.Kubernetes.ServiceBinding", "..\Configuration\src\Kubernetes.ServiceBinding\Steeltoe.Extensions.Configuration.Kubernetes.ServiceBinding.csproj", "{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}"
6365
EndProject
6466
Global
6567
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -299,6 +301,18 @@ Global
299301
{B0A47904-1C27-4380-9327-986F762B0250}.Release|x64.Build.0 = Release|Any CPU
300302
{B0A47904-1C27-4380-9327-986F762B0250}.Release|x86.ActiveCfg = Release|Any CPU
301303
{B0A47904-1C27-4380-9327-986F762B0250}.Release|x86.Build.0 = Release|Any CPU
304+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
305+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
306+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}.Debug|x64.ActiveCfg = Debug|Any CPU
307+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}.Debug|x64.Build.0 = Debug|Any CPU
308+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}.Debug|x86.ActiveCfg = Debug|Any CPU
309+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}.Debug|x86.Build.0 = Debug|Any CPU
310+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
311+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}.Release|Any CPU.Build.0 = Release|Any CPU
312+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}.Release|x64.ActiveCfg = Release|Any CPU
313+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}.Release|x64.Build.0 = Release|Any CPU
314+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}.Release|x86.ActiveCfg = Release|Any CPU
315+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9}.Release|x86.Build.0 = Release|Any CPU
302316
EndGlobalSection
303317
GlobalSection(SolutionProperties) = preSolution
304318
HideSolutionNode = FALSE
@@ -323,6 +337,7 @@ Global
323337
{0D1C5F38-D7E4-4F14-BD51-90064CE89C95} = {1CEE3AF7-DC06-4BBE-B28D-62FB1BD4A7A5}
324338
{EDB042A3-A6DB-4400-84C4-DF467FA01B8C} = {9BB0395E-B595-4B95-89ED-F55AF12B4EE7}
325339
{B0A47904-1C27-4380-9327-986F762B0250} = {4F6282E5-97B9-4655-8E67-7DF32320FE0C}
340+
{A3329B3B-F377-4F5F-B7E0-571DA3C511F9} = {4F6282E5-97B9-4655-8E67-7DF32320FE0C}
326341
EndGlobalSection
327342
GlobalSection(ExtensibilityGlobals) = postSolution
328343
SolutionGuid = {112EBBFA-82B9-483E-B258-8611B70B3A3A}

src/Connectors/src/ConnectorBase/IConfigurationExtensions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,22 @@ public static TServiceInfo GetRequiredServiceInfo<TServiceInfo>(this IConfigurat
9999
/// Evaluate whether an IConfiguration contains services bound by Cloud Foundry
100100
/// </summary>
101101
/// <param name="configuration">Application Configuration</param>
102-
/// <returns>true if vcap:services found in config, othwerwise false</returns>
102+
/// <returns>true if vcap:services found in config, otherwise false</returns>
103103
public static bool HasCloudFoundryServiceConfigurations(this IConfiguration configuration)
104104
=> configuration.GetSection("vcap:services").GetChildren().Any();
105105

106+
/// <summary>
107+
/// Evaluate whether an IConfiguration contains Kubernetes service bindings.
108+
/// </summary>
109+
/// <param name="configuration">
110+
/// Application Configuration.
111+
/// </param>
112+
/// <returns>
113+
/// true if k8s:bindings found in configuration, otherwise false.
114+
/// </returns>
115+
public static bool HasKubernetesServiceBindings(this IConfiguration configuration)
116+
=> configuration.GetSection("k8s:bindings").GetChildren().Any();
117+
106118
/// <summary>
107119
/// Adds a configuration provider that uses Connector logic to fulfill requests for GetConnectionString("serviceType") or GetConnectionString("serviceBindingName")
108120
/// </summary>

src/Connectors/src/ConnectorBase/MongoDb/MongoDbConnectorOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class MongoDbConnectorOptions : AbstractServiceConnectorOptions
1515
public const string Default_Server = "localhost";
1616
public const int Default_Port = 27017;
1717
private const string MONGODB_CLIENT_SECTION_PREFIX = "mongodb:client";
18-
private readonly bool _cloudFoundryConfigFound = false;
18+
private readonly bool _bindingsFound;
1919

2020
public MongoDbConnectorOptions()
2121
{
@@ -32,7 +32,7 @@ public MongoDbConnectorOptions(IConfiguration config)
3232
var section = config.GetSection(MONGODB_CLIENT_SECTION_PREFIX);
3333
section.Bind(this);
3434

35-
_cloudFoundryConfigFound = config.HasCloudFoundryServiceConfigurations();
35+
_bindingsFound = config.HasCloudFoundryServiceConfigurations() || config.HasKubernetesServiceBindings();
3636
}
3737

3838
public string ConnectionString { get; set; }
@@ -53,7 +53,7 @@ public MongoDbConnectorOptions(IConfiguration config)
5353

5454
public override string ToString()
5555
{
56-
if (!string.IsNullOrEmpty(ConnectionString) && !_cloudFoundryConfigFound)
56+
if (!string.IsNullOrEmpty(ConnectionString) && !_bindingsFound)
5757
{
5858
// Connection string was provided and VCAP_SERVICES wasn't found, just use the connectionstring
5959
return ConnectionString;

src/Connectors/src/ConnectorBase/MySql/MySqlProviderConnectorOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class MySqlProviderConnectorOptions : AbstractServiceConnectorOptions
1818
public const string Default_Server = "localhost";
1919
public const int Default_Port = 3306;
2020
private const string MYSQL_CLIENT_SECTION_PREFIX = "mysql:client";
21-
private readonly bool _cloudFoundryConfigFound = false;
21+
private readonly bool _bindingsFound;
2222

2323
public MySqlProviderConnectorOptions()
2424
{
@@ -35,7 +35,7 @@ public MySqlProviderConnectorOptions(IConfiguration config)
3535
var section = config.GetSection(MYSQL_CLIENT_SECTION_PREFIX);
3636
section.Bind(this);
3737

38-
_cloudFoundryConfigFound = config.HasCloudFoundryServiceConfigurations();
38+
_bindingsFound = config.HasCloudFoundryServiceConfigurations() || config.HasKubernetesServiceBindings();
3939
}
4040

4141
public string ConnectionString { get; set; }
@@ -86,7 +86,7 @@ public MySqlProviderConnectorOptions(IConfiguration config)
8686

8787
public override string ToString()
8888
{
89-
if (!string.IsNullOrEmpty(ConnectionString) && !_cloudFoundryConfigFound)
89+
if (!string.IsNullOrEmpty(ConnectionString) && !_bindingsFound)
9090
{
9191
return ConnectionString;
9292
}

src/Connectors/src/ConnectorBase/Oracle/OracleProviderConnectorOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class OracleProviderConnectorOptions : AbstractServiceConnectorOptions
1313
public const string Default_Server = "localhost";
1414
public const int Default_Port = 1521;
1515
private const string ORACLE_CLIENT_SECTION_PREFIX = "oracle:client";
16-
private readonly bool _cloudFoundryConfigFound = false;
16+
private readonly bool _bindingsFound;
1717

1818
public OracleProviderConnectorOptions()
1919
{
@@ -31,7 +31,7 @@ public OracleProviderConnectorOptions(IConfiguration config)
3131

3232
section.Bind(this);
3333

34-
_cloudFoundryConfigFound = config.HasCloudFoundryServiceConfigurations();
34+
_bindingsFound = config.HasCloudFoundryServiceConfigurations() || config.HasKubernetesServiceBindings();
3535
}
3636

3737
public string ConnectionString { get; set; }
@@ -52,7 +52,7 @@ public OracleProviderConnectorOptions(IConfiguration config)
5252

5353
public override string ToString()
5454
{
55-
if (!string.IsNullOrEmpty(ConnectionString) && !_cloudFoundryConfigFound)
55+
if (!string.IsNullOrEmpty(ConnectionString) && !_bindingsFound)
5656
{
5757
return ConnectionString;
5858
}

src/Connectors/src/ConnectorBase/PostgreSQL/PostgresProviderConnectorOptions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using Microsoft.Extensions.Configuration;
6-
using Steeltoe.Common.Util;
76
using System;
87
using System.Collections.Generic;
98
using System.Linq;
@@ -16,7 +15,7 @@ public class PostgresProviderConnectorOptions : AbstractServiceConnectorOptions
1615
public const string Default_Host = "localhost";
1716
public const int Default_Port = 5432;
1817
private const string POSTGRES_CLIENT_SECTION_PREFIX = "postgres:client";
19-
private readonly bool _cloudFoundryConfigFound = false;
18+
private readonly bool _bindingsFound = false;
2019

2120
public PostgresProviderConnectorOptions()
2221
{
@@ -33,7 +32,7 @@ public PostgresProviderConnectorOptions(IConfiguration config)
3332
var section = config.GetSection(POSTGRES_CLIENT_SECTION_PREFIX);
3433
section.Bind(this);
3534

36-
_cloudFoundryConfigFound = config.HasCloudFoundryServiceConfigurations();
35+
_bindingsFound = config.HasCloudFoundryServiceConfigurations() || config.HasKubernetesServiceBindings();
3736
}
3837

3938
public string ConnectionString { get; set; }
@@ -70,7 +69,7 @@ public override string ToString()
7069
{
7170
StringBuilder sb;
7271

73-
if (!string.IsNullOrEmpty(ConnectionString) && !_cloudFoundryConfigFound)
72+
if (!string.IsNullOrEmpty(ConnectionString) && !_bindingsFound)
7473
{
7574
sb = new StringBuilder(ConnectionString);
7675
}

src/Connectors/src/ConnectorBase/Redis/RedisCacheConnectorOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class RedisCacheConnectorOptions : AbstractServiceConnectorOptions
1616
private const string Default_Host = "localhost";
1717
private const int Default_Port = 6379;
1818
private const string RedisClientSectionPrefix = "redis:client";
19-
private readonly bool _cloudFoundryConfigFound = false;
19+
private readonly bool _bindingsFound;
2020

2121
public RedisCacheConnectorOptions()
2222
: base(',', Default_Separator)
@@ -34,7 +34,7 @@ public RedisCacheConnectorOptions(IConfiguration config)
3434
var section = config.GetSection(RedisClientSectionPrefix);
3535
section.Bind(this);
3636

37-
_cloudFoundryConfigFound = config.HasCloudFoundryServiceConfigurations();
37+
_bindingsFound = config.HasCloudFoundryServiceConfigurations() || config.HasKubernetesServiceBindings();
3838
}
3939

4040
// Configure either a single Host/Port or optionaly provide
@@ -87,7 +87,7 @@ public RedisCacheConnectorOptions(IConfiguration config)
8787
// public int? DefaultDatabase { get; set; }
8888
public override string ToString()
8989
{
90-
if (!string.IsNullOrEmpty(ConnectionString) && !_cloudFoundryConfigFound)
90+
if (!string.IsNullOrEmpty(ConnectionString) && !_bindingsFound)
9191
{
9292
return ConnectionString;
9393
}

src/Connectors/src/ConnectorBase/SqlServer/SqlServerProviderConnectorOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class SqlServerProviderConnectorOptions : AbstractServiceConnectorOptions
1515
public const string Default_Server = "localhost";
1616
public const int Default_Port = 1433;
1717
private const string SQL_CLIENT_SECTION_PREFIX = "sqlserver:credentials";
18-
private readonly bool _cloudFoundryConfigFound = false;
18+
private readonly bool _bindingsFound;
1919

2020
public SqlServerProviderConnectorOptions()
2121
{
@@ -44,7 +44,7 @@ public SqlServerProviderConnectorOptions(IConfiguration config)
4444

4545
section.Bind(this);
4646

47-
_cloudFoundryConfigFound = config.HasCloudFoundryServiceConfigurations();
47+
_bindingsFound = config.HasCloudFoundryServiceConfigurations() || config.HasKubernetesServiceBindings();
4848
}
4949

5050
public string ConnectionString { get; set; }
@@ -84,7 +84,7 @@ public SqlServerProviderConnectorOptions(IConfiguration config)
8484

8585
public override string ToString()
8686
{
87-
if (!string.IsNullOrEmpty(ConnectionString) && !_cloudFoundryConfigFound)
87+
if (!string.IsNullOrEmpty(ConnectionString) && !_bindingsFound)
8888
{
8989
return ConnectionString;
9090
}

src/Connectors/test/Connector.EFCore.Test/PostgresDbContextOptionsExtensionsTest.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5+
using FluentAssertions;
56
using Microsoft.EntityFrameworkCore;
67
using Microsoft.Extensions.Configuration;
78
using Microsoft.Extensions.DependencyInjection;
89
using Npgsql;
910
using Steeltoe.Connector.EFCore.Test;
1011
using Steeltoe.Extensions.Configuration.CloudFoundry;
12+
#if NET6_0_OR_GREATER
13+
using Steeltoe.Extensions.Configuration.Kubernetes.ServiceBinding;
14+
#endif
1115
using System;
1216
using System.Collections.Generic;
17+
using System.IO;
1318
using Xunit;
1419

1520
namespace Steeltoe.Connector.PostgreSql.EFCore.Test;
@@ -229,4 +234,47 @@ public void AddDbContexts_WithEncodedCrunchyVCAPs_AddsDbContexts()
229234
Assert.Contains("Password=Qp!1mB1$Zk2T!$!D85_E", connString);
230235
Assert.Contains("Database=steeltoe", connString);
231236
}
237+
238+
#if NET6_0_OR_GREATER
239+
[Fact]
240+
public void AddDbContext_WithK8sBinding_AddsDbContext()
241+
{
242+
var rootDir = Path.Combine(Environment.CurrentDirectory, "..", "..", "..", "resources", "bindings");
243+
Environment.SetEnvironmentVariable("SERVICE_BINDING_ROOT", rootDir);
244+
245+
try
246+
{
247+
IServiceCollection services = new ServiceCollection();
248+
249+
var appsettings = new Dictionary<string, string>
250+
{
251+
{ "steeltoe:kubernetes:bindings:enable", "true" }
252+
};
253+
254+
var builder = new ConfigurationBuilder();
255+
builder.AddInMemoryCollection(appsettings);
256+
builder.AddKubernetesServiceBindings(false);
257+
var configurationRoot = builder.Build();
258+
259+
services.AddDbContext<GoodDbContext>(options => options.UseNpgsql(configurationRoot));
260+
261+
using var built = services.BuildServiceProvider();
262+
using var dbContext = built.GetService<GoodDbContext>();
263+
dbContext.Should().NotBeNull();
264+
265+
using var connection = dbContext.Database.GetDbConnection();
266+
connection.Should().BeOfType<NpgsqlConnection>().And.Subject.Should().NotBeNull();
267+
268+
connection.ConnectionString.Should().Contain("Host=10.194.59.205");
269+
connection.ConnectionString.Should().Contain("Port=5432");
270+
connection.ConnectionString.Should().Contain("Username=testrolee93ccf859894dc60dcd53218492b37b4");
271+
connection.ConnectionString.Should().Contain("Password=Qp!1mB1$Zk2T!$!D85_E");
272+
connection.ConnectionString.Should().Contain("Database=steeltoe");
273+
}
274+
finally
275+
{
276+
Environment.SetEnvironmentVariable("SERVICE_BINDING_ROOT", null);
277+
}
278+
}
279+
#endif
232280
}

src/Connectors/test/Connector.EFCore.Test/Steeltoe.Connector.EFCore.Test.csproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,32 @@
2323
</ItemGroup>
2424
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
2525
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="$(PomeloEFCoreVersion)" />
26+
<ProjectReference Include="..\..\..\Configuration\src\Kubernetes.ServiceBinding\Steeltoe.Extensions.Configuration.Kubernetes.ServiceBinding.csproj" />
2627
</ItemGroup>
2728

2829
<ItemGroup>
2930
<ProjectReference Include="..\..\src\CloudFoundry\Steeltoe.Connector.CloudFoundry.csproj" />
3031
<ProjectReference Include="..\..\src\Connector.EFCore\Steeltoe.Connector.EFCore.csproj" />
3132
</ItemGroup>
33+
34+
<ItemGroup>
35+
<None Update="resources\bindings\customer-profiles\database">
36+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
37+
</None>
38+
<None Update="resources\bindings\customer-profiles\password">
39+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
40+
</None>
41+
<None Update="resources\bindings\customer-profiles\username">
42+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
43+
</None>
44+
<None Update="resources\bindings\customer-profiles\provider">
45+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
46+
</None>
47+
<None Update="resources\bindings\customer-profiles\host">
48+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
49+
</None>
50+
<None Update="resources\bindings\customer-profiles\type">
51+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
52+
</None>
53+
</ItemGroup>
3254
</Project>

0 commit comments

Comments
 (0)