-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConnectionPoolConfiguration.cs
308 lines (277 loc) · 13.5 KB
/
ConnectionPoolConfiguration.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright 2017 Stanislav Muhametsin. All rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using CBAM.SQL.Implementation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using CBAM.SQL.PostgreSQL.Implementation;
using UtilPack;
using CBAM.Abstractions.Implementation;
using CBAM.SQL.PostgreSQL;
using IOUtils.Network.Configuration;
using FluentCryptography.SASL;
using FluentCryptography.SASL.SCRAM;
#if !NETSTANDARD1_0
using IOUtils.Network.ResourcePooling;
#endif
namespace CBAM.SQL.PostgreSQL
{
/// <summary>
/// This class represents the passive data related to creation of new <see cref="PgSQLConnection"/>.
/// Typically, this class holds the values written in some configuration file on a disk.
/// Use <c>Microsoft.Extensions.Configuration.Binder</c> NuGet package to automatize the creation of this class and population of its properties.
/// </summary>
/// <remarks>
/// See <see href="https://github.com/CometaSolutions/CBAM/tree/develop/Source/CBAM.SQL.PostgreSQL.Implementation"/> for small example on typical usecase of this class.
/// </remarks>
/// <seealso cref="PgSQLConnectionCreationInfo"/>
public class PgSQLConnectionCreationInfoData : NetworkConnectionCreationInfoData<PgSQLConnectionConfiguration, PgSQLInitializationConfiguration, PgSQLProtocolConfiguration, PgSQLPoolingConfiguration, PgSQLAuthenticationConfiguration>
{
}
/// <summary>
/// This class contains all passive configuration data related to opening a socket connection when initializing new <see cref="PgSQLConnection"/>.
/// </summary>
public class PgSQLConnectionConfiguration : NetworkConnectionConfiguration
{
}
/// <summary>
/// This class contains all passive configuration data related to initialization routine of new <see cref="PgSQLConnection"/> once the <see cref="Stream"/> used to communicate with backend has been initialized.
/// </summary>
public class PgSQLInitializationConfiguration : NetworkInitializationConfiguration<PgSQLProtocolConfiguration, PgSQLPoolingConfiguration, PgSQLAuthenticationConfiguration>
{
/// <summary>
/// Gets or sets the type containing passive configuration data about the database to connect to.
/// </summary>
/// <value>The type containing passive configuration data about the database to connect to.</value>
/// <seealso cref="PgSQLDatabaseConfiguration"/>
public PgSQLDatabaseConfiguration Database { get; set; }
}
/// <summary>
/// This class contains all passive configuration data related to behaviour of PgSQL connections when they are used within the connection pool.
/// </summary>
public class PgSQLPoolingConfiguration : NetworkPoolingConfiguration
{
}
/// <summary>
/// This class contains all passive configuration data related to authentication of new <see cref="PgSQLConnection"/>.
/// </summary>
public class PgSQLAuthenticationConfiguration
{
internal static readonly Encoding PasswordByteEncoding = new UTF8Encoding( false, true );
/// <summary>
/// Gets or sets the username to use when connecting to the database.
/// </summary>
/// <value>The username to use when connecting to the database.</value>
public String Username { get; set; }
/// <summary>
/// Gets the textual password as byte array, to use along with <see cref="Username"/> when connecting to the database.
/// </summary>
/// <value>The textual password as byte array, to use along with <see cref="Username"/> when connecting to the database.</value>
/// <seealso cref="PasswordDigest"/>
public Byte[] PasswordBytes { get; private set; }
/// <summary>
/// Gets or sets the digest of the cleartext password.
/// </summary>
/// <value>The digest of the cleartext password.</value>
/// <remarks>
/// This property will *only* be used in SCRAM authentication, if server chooses to perform it.
/// The SCRAM authentication allows to use the digest of the password instead of cleartext password.
/// </remarks>
public Byte[] PasswordDigest { get; set; }
/// <summary>
/// Gets the password, as <see cref="String"/>, to use along with <see cref="Username"/> when connecting to the database.
/// </summary>
/// <value>The password, as <see cref="String"/>, to use along with <see cref="Username"/> when connecting to the database.</value>
public String Password
{
get
{
var arr = this.PasswordBytes;
return arr == null ? null : PasswordByteEncoding.GetString( arr, 0, arr.Length );
}
set
{
this.PasswordBytes = value == null ? null : PasswordByteEncoding.GetBytes( value );
}
}
}
/// <summary>
/// This class contains all passive configuration data related to selecting which database the <see cref="PgSQLConnection"/> will be connected to, as well as authentication to that database.
/// </summary>
public class PgSQLDatabaseConfiguration
{
/// <summary>
/// Gets or sets the name of the database that the <see cref="PgSQLConnection"/> should be connected to.
/// </summary>
/// <value>The name of the database that the <see cref="PgSQLConnection"/> should be connected to.</value>
public String Name { get; set; }
/// <summary>
/// Gets the search path (<see href="https://www.postgresql.org/docs/current/static/runtime-config-client.html"/>) to use for the database.
/// </summary>
/// <value>The search path (<see href="https://www.postgresql.org/docs/current/static/runtime-config-client.html"/>) to use for the database.</value>
public String SearchPath { get; set; }
}
/// <summary>
/// This class contains all passive data configuration related to protocol used to communicate with backend process.
/// </summary>
public class PgSQLProtocolConfiguration
{
/// <summary>
/// Gets or sets the value indicating whether SQL type IDs should be re-read from database, even though they have been previously cached for the version of PostgreSQL backend connected to.
/// </summary>
/// <value>The value indicating whether SQL type IDs should be re-read from database</value>
public Boolean ForceTypeIDLoad { get; set; }
/// <summary>
/// Gets or sets the value indicating whether <see cref="DataFormat.Binary"/> should be disabled when sending data to the backend.
/// </summary>
/// <value>The value indicating whether <see cref="DataFormat.Binary"/> should be disabled when sending data to the backend.</value>
public Boolean DisableBinaryProtocolSend { get; set; }
/// <summary>
/// Gets or sets the value indicating whether <see cref="DataFormat.Binary"/> should be disabled when receiving data from the backend.
/// </summary>
/// <value>The value indicating whether <see cref="DataFormat.Binary"/> should be disabled when receiving data from the backend.</value>
public Boolean DisableBinaryProtocolReceive { get; set; }
}
/// <summary>
/// This class binds together the passive configuration data of the <see cref="PgSQLConnectionCreationInfoData"/> and behaviour (callbacks) needed when creating and initializing <see cref="PgSQLConnection"/>s.
/// </summary>
public sealed class PgSQLConnectionCreationInfo : NetworkConnectionCreationInfo<PgSQLConnectionCreationInfoData, PgSQLConnectionConfiguration, PgSQLInitializationConfiguration, PgSQLProtocolConfiguration, PgSQLPoolingConfiguration, PgSQLAuthenticationConfiguration>
{
private const String SCRAM_SHA_256 = "SCRAM-SHA-256";
private const String SCRAM_SHA_512 = "SCRAM-SHA-512";
/// <summary>
/// Creates a new instance of <see cref="PgSQLConnectionCreationInfo"/> with given <see cref="PgSQLConnectionCreationInfoData"/>.
/// </summary>
/// <param name="data">The <see cref="PgSQLConnectionCreationInfoData"/> to use.</param>
/// <exception cref="ArgumentNullException">If <paramref name="data"/> is <c>null</c>.</exception>
/// <remarks>
/// In .NET Core App 1.1+ and .NET Desktop 4.0+ environments this will also set up default values for <see cref="P:CBAM.SQL.PostgreSQL.PgSQLConnectionCreationInfo.ProvideSSLStream"/>.
/// </remarks>
public PgSQLConnectionCreationInfo(
PgSQLConnectionCreationInfoData data
) : base( data )
{
this.CreateSASLMechanism = ( names ) =>
{
FluentCryptography.Digest.BlockDigestAlgorithm algorithm;
String mechanismName;
if ( String.IsNullOrEmpty( names ) )
{
algorithm = null;
mechanismName = null;
}
else
{
if ( names.IndexOf( SCRAM_SHA_512 ) >= 0 )
{
algorithm = new FluentCryptography.Digest.SHA512();
mechanismName = SCRAM_SHA_512;
}
else if ( names.IndexOf( SCRAM_SHA_256 ) >= 0 )
{
algorithm = new FluentCryptography.Digest.SHA256();
mechanismName = SCRAM_SHA_256;
}
else
{
algorithm = null;
mechanismName = null;
}
}
return (algorithm?.CreateSASLClientSCRAM(), mechanismName);
};
}
/// <summary>
/// This callback will be used during SCRAM authentication to select the <see cref="SASLMechanism"/> based on the advertised mechanisms sent by server as a string.
/// The constructor will set this to default value which supports SCRAM-SHA-256 and SCRAM-SHA-512, but this may be overridden for custom <see cref="SASLMechanism"/>s.
/// </summary>
/// <value>The callback to select <see cref="SASLMechanism"/> from a list of SASL mechanisms advertised by backend.</value>
/// <remarks>
/// This callback should return a tuple of <see cref="SASLMechanism"/> and the name of it.
/// </remarks>
public Func<String, (SASLMechanism, String)> CreateSASLMechanism { get; set; }
/// <summary>
/// This callback will be used during SCRAM authentication, when the user is successfully authenticated.
/// It will receive a digest of the cleartext password, so that it can be e.g. saved for later purpose.
/// </summary>
/// <value>The callback to call on successful SASL SCRAM authentication, receiving password digest as argument.</value>
public Action<Byte[]> OnSASLSCRAMSuccess { get; set; }
}
}
/// <summary>
/// This class contains extension methods for types defined in this assembly.
/// </summary>
public static partial class E_CBAM
{
/// <summary>
/// This is helper method to create a new deep copy of this <see cref="PgSQLConnectionCreationInfoData"/> instance.
/// </summary>
/// <param name="data">This <see cref="PgSQLConnectionCreationInfoData"/>.</param>
/// <returns>A new instance of <see cref="PgSQLConnectionCreationInfoData"/>, with all values deeply copied from this <see cref="PgSQLConnectionCreationInfoData"/>.</returns>
/// <exception cref="NullReferenceException">If this <see cref="PgSQLConnectionCreationInfoData"/> is <c>null</c>.</exception>
public static PgSQLConnectionCreationInfoData CreateCopy( this PgSQLConnectionCreationInfoData data )
{
#if !NETSTANDARD1_0
var conn = data.Connection;
#endif
var init = data.Initialization;
var db = init?.Database;
var protocol = init?.Protocol;
var pool = init?.ConnectionPool;
var auth = init?.Authentication;
return new PgSQLConnectionCreationInfoData()
{
#if !NETSTANDARD1_0
Connection = new PgSQLConnectionConfiguration()
{
Host = conn?.Host,
Port = conn?.Port ?? 0,
ConnectionSSLMode = conn?.ConnectionSSLMode ?? ConnectionSSLMode.NotRequired,
SSLProtocols = conn?.SSLProtocols ?? PgSQLConnectionConfiguration.DEFAULT_SSL_PROTOCOL
},
#endif
Initialization = new PgSQLInitializationConfiguration()
{
Database = new PgSQLDatabaseConfiguration()
{
Name = db?.Name,
SearchPath = db?.SearchPath
},
Authentication = new PgSQLAuthenticationConfiguration()
{
Username = auth?.Username,
Password = auth?.Password,
PasswordDigest = auth?.PasswordDigest
},
Protocol = new PgSQLProtocolConfiguration()
{
ForceTypeIDLoad = protocol?.ForceTypeIDLoad ?? false,
DisableBinaryProtocolSend = protocol?.DisableBinaryProtocolSend ?? false,
DisableBinaryProtocolReceive = protocol?.DisableBinaryProtocolReceive ?? false
},
ConnectionPool = new PgSQLPoolingConfiguration()
{
ConnectionsOwnStringPool = pool?.ConnectionsOwnStringPool ?? false
}
}
};
}
}