forked from Trendyol/PollingOutboxPublisher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresConnectionProvider.cs
More file actions
99 lines (81 loc) · 3.91 KB
/
PostgresConnectionProvider.cs
File metadata and controls
99 lines (81 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Npgsql;
using PollingOutboxPublisher.ConfigOptions;
using PollingOutboxPublisher.Database.Providers.Interfaces;
using PollingOutboxPublisher.Exceptions;
namespace PollingOutboxPublisher.Database.Providers;
[ExcludeFromCodeCoverage]
public class PostgresConnectionProvider : IPostgresConnectionProvider
{
private readonly string _connectionString;
public PostgresConnectionProvider(IConfiguration configuration, IOptions<DbCredentialsFileSettings> databaseTbpAuthenticationCredentials)
{
if (configuration.GetValue<bool>("UseDbCredentialsFile") is true)
{
ValidateTbpAuthenticationCredentials(databaseTbpAuthenticationCredentials.Value);
var dbCredentials = databaseTbpAuthenticationCredentials.Value;
var postgresqlUsernamePassword = GetPostgresqlUsernameAndPassword(dbCredentials.FileName);
_connectionString = GenerateConnectionString(postgresqlUsernamePassword.userName, postgresqlUsernamePassword.password, dbCredentials.Host, dbCredentials.Database, dbCredentials.Port, dbCredentials.ApplicationName,dbCredentials.AdditionalParameters);
}
else
{
var connectionString = configuration.GetValue<string>("ConnectionString");
if (string.IsNullOrWhiteSpace(connectionString))
{
throw new MissingConfigurationException("ConnectionString");
}
_connectionString = connectionString;
}
}
public IDbConnection CreateConnection()
=> new NpgsqlConnection(_connectionString);
public IDbConnection CreateConnectionForReadOnly()
=> new NpgsqlConnection(_connectionString);
private static (string userName, string password) GetPostgresqlUsernameAndPassword(string fileName)
{
if (!File.Exists(fileName))
{
throw new FileNotFoundException($"Postgresql credentials file not found: {fileName}");
}
var postgresqlCredentials= new ConfigurationBuilder()
.AddJsonFile(fileName)
.Build();
var username = postgresqlCredentials["username"];
var password = postgresqlCredentials["password"];
return (username, password);
}
private static void ValidateTbpAuthenticationCredentials(DbCredentialsFileSettings credentialses)
{
if (string.IsNullOrWhiteSpace(credentialses.FileName))
{
throw new MissingConfigurationException("DatabaseTbpAuthenticationCredentials:FileName");
}
if (string.IsNullOrWhiteSpace(credentialses.Host))
{
throw new MissingConfigurationException("DatabaseTbpAuthenticationCredentials:Host");
}
if (string.IsNullOrWhiteSpace(credentialses.Database))
{
throw new MissingConfigurationException("DatabaseTbpAuthenticationCredentials:Database");
}
if (string.IsNullOrWhiteSpace(credentialses.ApplicationName))
{
throw new MissingConfigurationException("DatabaseTbpAuthenticationCredentials:ApplicationName");
}
if (credentialses.Port == 0)
{
throw new MissingConfigurationException("DatabaseTbpAuthenticationCredentials:Port");
}
}
private static string GenerateConnectionString(string userName, string password, string host, string database, int port, string appName, string additionalParameters)
{
var connectionString = $"User ID={userName};Password={password};Server={host};Port={port};Database={database};ApplicationName={appName};";
if (!string.IsNullOrEmpty(additionalParameters))
connectionString = string.Concat(connectionString, additionalParameters);;
return connectionString;
}
}