-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRDSusingIAMauthWithNhibernate.cs
96 lines (81 loc) · 3.41 KB
/
RDSusingIAMauthWithNhibernate.cs
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
using System;
using System.IO;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Npgsql;
using System.Collections.Generic;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
;
using NHibernate.Tool.hbm2ddl;
using Amazon.RDS.Util;
namespace Aws.RDS.IAM.PostGreSql.Authentication.With.Nhibernate.Demo
{
public class RDSusingIAMauthWithNhibernate : IHealthCheck
{
private readonly ILogger Logger;
public RDSusingIAMauthWithNhibernate(ILogger logger)
{
Logger = logger;
}
public static void CheckPostgreSQLDatabase(ILogger Logger)
{
try
{
var configurationFileName =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Settings.Default.ConfigurationFile);
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure(configurationFileName);
var connectionString = cfg.GetProperty(NHibernate.Cfg.Environment.ConnectionString);
var dialect = cfg.GetProperty(NHibernate.Cfg.Environment.Dialect);
GlobalConstant.DB_PostgreSQL = dialect.IndexOf("PostgreSQL") >= 0;
if (GlobalConstant.DB_PostgreSQL)
{
var ctalogConnectionString = new NpgsqlConnectionStringBuilder(connectionString);
var configuration = Fluently.Configure()
.Database(PostgreSQLConfiguration.Standard
.ConnectionString(c => c
.Host(ctalogConnectionString.Host)
.Port(ctalogConnectionString.Port)
.Database(ctalogConnectionString.Database)
.Username(ctalogConnectionString.Username)
.Password(GenerateAwsIamAuthToken(ctalogConnectionString.Host, ctalogConnectionString.Port, ctalogConnectionString.Database, ctalogConnectionString.Username)))
)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<VersionInfo>())
.ExposeConfiguration(config => new SchemaExport(config).Create(false, true))
.BuildSessionFactory();
using (var session = configuration.OpenSession())
{
// Query all objects
Logger.Info("PostgreSQL Database is ready....");
var completeList = session.CreateCriteria<Object>().List();
Console.ReadLine();
}
}
}
catch (Exception)
{
throw;
}
}
private static string GenerateAwsIamAuthToken(string host, int port, string database, string username)
{
try
{
if (host.EndsWith("rds.amazonaws.com"))
{
return Amazon.RDS.Util.RDSAuthTokenGenerator.GenerateAuthToken(host, port, username);
}
else
{
return $"{username}";
}
}
catch
{
throw;
}
}
}
}