Skip to content

Commit 0921d55

Browse files
eryilmaz0Eren Yilmaz
andauthored
Add database tbp authentication (#20)
* added tbp authentication support * added appName * use options pattern for config * increase version * change feature name as UseDbCredentialsFile * add changelog * change version date * change example config * add additional parameters config --------- Co-authored-by: Eren Yilmaz <eren.yilmaz@trendyol.com>
1 parent a46c133 commit 0921d55

File tree

6 files changed

+113
-6
lines changed

6 files changed

+113
-6
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
## v1.6.0 (July 2, 2025)
2+
3+
### Added:
4+
- Db Credentials file feature.
5+
- This feature allows a database connection to be created in cases where database credentials need to be read from a file. Currently only active on PostgreSQL.
6+
- It can be activated with the new config value `UseDbCredentialsFile`.
7+
- This feature requires a new config section called `DbCredentialsFileSettings` with the following options:
8+
- `FileName`: File name containing database credentials (should be full path)
9+
- `Host`: Host information of the database
10+
- `Database`: Db name of the database
11+
- `ApplicationName`: App name information on database connection
12+
- `Port`: Port of the database
13+
- `AdditionalParameters`: Additional parameters to be added on Connection. Flags such as `Pooling`, `TrustServerCertificate` can be added.
14+
15+
116
## v1.5.0 (March 6, 2025)
217

318
### Added:

example/Postgres/config/config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,14 @@
6363
"Threshold": 3,
6464
"DurationSc": 60,
6565
"HalfOpenMaxAttempts": 1
66+
},
67+
"UseDbCredentialsFile": true,
68+
"DbCredentialsFileSettings": {
69+
"FileName": "config/postgresql-test-cluster.json",
70+
"Host": "10.85.240.242",
71+
"Database": "test",
72+
"ApplicationName": "PollingOutboxPublisher",
73+
"Port": 5432,
74+
"AdditionalParameters": "Pooling=true;TrustServerCertificate=true;"
6675
}
6776
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace PollingOutboxPublisher.ConfigOptions;
4+
5+
[ExcludeFromCodeCoverage]
6+
public class DbCredentialsFileSettings
7+
{
8+
public string FileName { get; set; }
9+
public string Host { get; set; }
10+
public string Database { get; set; }
11+
public int Port { get; set; }
12+
public string ApplicationName { get; set; }
13+
public string AdditionalParameters { get; set; }
14+
}
Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System.Data;
22
using System.Diagnostics.CodeAnalysis;
3+
using System.IO;
34
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.Options;
46
using Npgsql;
7+
using PollingOutboxPublisher.ConfigOptions;
58
using PollingOutboxPublisher.Database.Providers.Interfaces;
69
using PollingOutboxPublisher.Exceptions;
710

@@ -12,15 +15,25 @@ public class PostgresConnectionProvider : IPostgresConnectionProvider
1215
{
1316
private readonly string _connectionString;
1417

15-
public PostgresConnectionProvider(IConfiguration configuration)
18+
public PostgresConnectionProvider(IConfiguration configuration, IOptions<DbCredentialsFileSettings> databaseTbpAuthenticationCredentials)
1619
{
17-
var connectionString = configuration.GetValue<string>("ConnectionString");
18-
if (string.IsNullOrWhiteSpace(connectionString))
20+
if (configuration.GetValue<bool>("UseDbCredentialsFile") is true)
1921
{
20-
throw new MissingConfigurationException("ConnectionString");
22+
ValidateTbpAuthenticationCredentials(databaseTbpAuthenticationCredentials.Value);
23+
var dbCredentials = databaseTbpAuthenticationCredentials.Value;
24+
var postgresqlUsernamePassword = GetPostgresqlUsernameAndPassword(dbCredentials.FileName);
25+
_connectionString = GenerateConnectionString(postgresqlUsernamePassword.userName, postgresqlUsernamePassword.password, dbCredentials.Host, dbCredentials.Database, dbCredentials.Port, dbCredentials.ApplicationName,dbCredentials.AdditionalParameters);
2126
}
27+
else
28+
{
29+
var connectionString = configuration.GetValue<string>("ConnectionString");
30+
if (string.IsNullOrWhiteSpace(connectionString))
31+
{
32+
throw new MissingConfigurationException("ConnectionString");
33+
}
2234

23-
_connectionString = connectionString;
35+
_connectionString = connectionString;
36+
}
2437
}
2538

2639
public IDbConnection CreateConnection()
@@ -29,4 +42,58 @@ public IDbConnection CreateConnection()
2942
public IDbConnection CreateConnectionForReadOnly()
3043
=> new NpgsqlConnection(_connectionString);
3144

45+
46+
private static (string userName, string password) GetPostgresqlUsernameAndPassword(string fileName)
47+
{
48+
if (!File.Exists(fileName))
49+
{
50+
throw new FileNotFoundException($"Postgresql credentials file not found: {fileName}");
51+
}
52+
53+
var postgresqlCredentials= new ConfigurationBuilder()
54+
.AddJsonFile(fileName)
55+
.Build();
56+
57+
var username = postgresqlCredentials["username"];
58+
var password = postgresqlCredentials["password"];
59+
return (username, password);
60+
}
61+
62+
private static void ValidateTbpAuthenticationCredentials(DbCredentialsFileSettings credentialses)
63+
{
64+
if (string.IsNullOrWhiteSpace(credentialses.FileName))
65+
{
66+
throw new MissingConfigurationException("DatabaseTbpAuthenticationCredentials:FileName");
67+
}
68+
69+
if (string.IsNullOrWhiteSpace(credentialses.Host))
70+
{
71+
throw new MissingConfigurationException("DatabaseTbpAuthenticationCredentials:Host");
72+
}
73+
74+
if (string.IsNullOrWhiteSpace(credentialses.Database))
75+
{
76+
throw new MissingConfigurationException("DatabaseTbpAuthenticationCredentials:Database");
77+
}
78+
79+
if (string.IsNullOrWhiteSpace(credentialses.ApplicationName))
80+
{
81+
throw new MissingConfigurationException("DatabaseTbpAuthenticationCredentials:ApplicationName");
82+
}
83+
84+
if (credentialses.Port == 0)
85+
{
86+
throw new MissingConfigurationException("DatabaseTbpAuthenticationCredentials:Port");
87+
}
88+
}
89+
90+
private static string GenerateConnectionString(string userName, string password, string host, string database, int port, string appName, string additionalParameters)
91+
{
92+
var connectionString = $"User ID={userName};Password={password};Server={host};Port={port};Database={database};ApplicationName={appName};";
93+
94+
if (!string.IsNullOrEmpty(additionalParameters))
95+
connectionString = string.Concat(connectionString, additionalParameters);;
96+
97+
return connectionString;
98+
}
3299
}

src/PollingOutboxPublisher.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
66
<IsPackable>false</IsPackable>
7-
<Version>1.5.0</Version>
7+
<Version>1.6.0</Version>
88
<RootNamespace>PollingOutboxPublisher</RootNamespace>
99
</PropertyGroup>
1010

src/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ private static void RegisterConfigs(IServiceCollection services, HostBuilderCont
103103
hostContext.Configuration.GetSection(nameof(ConfigOptions.Redis)));
104104
services.Configure<CircuitBreakerSettings>(
105105
hostContext.Configuration.GetSection(nameof(CircuitBreakerSettings)));
106+
services.Configure<DbCredentialsFileSettings>(
107+
hostContext.Configuration.GetSection(nameof(DbCredentialsFileSettings)));
106108
}
107109

108110
private static void RegisterSerilog(IServiceCollection services)

0 commit comments

Comments
 (0)