- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConnectionPoolConfiguration.cs
More file actions
Latest commit
308 lines (277 loc) · 13.5 KB
/
Copy pathConnectionPoolConfiguration.cs
File metadata and controls
308 lines (277 loc) · 13.5 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
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.
*/
usingCBAM.SQL.Implementation;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.IO;
usingSystem.Net;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingCBAM.SQL.PostgreSQL.Implementation;
usingUtilPack;
usingCBAM.Abstractions.Implementation;
usingCBAM.SQL.PostgreSQL;
usingIOUtils.Network.Configuration;
usingFluentCryptography.SASL;
usingFluentCryptography.SASL.SCRAM;
#if !NETSTANDARD1_0
usingIOUtils.Network.ResourcePooling;
#endif
namespaceCBAM.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"/>
publicclassPgSQLConnectionCreationInfoData: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>
publicclassPgSQLConnectionConfiguration: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>
publicclassPgSQLInitializationConfiguration: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"/>
publicPgSQLDatabaseConfigurationDatabase{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>
publicclassPgSQLPoolingConfiguration:NetworkPoolingConfiguration
{
}
/// <summary>
/// This class contains all passive configuration data related to authentication of new <see cref="PgSQLConnection"/>.
/// </summary>
publicclassPgSQLAuthenticationConfiguration
{
internalstaticreadonlyEncodingPasswordByteEncoding=newUTF8Encoding(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>
publicStringUsername{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"/>
publicByte[]PasswordBytes{get;privateset;}
/// <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>
publicByte[]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>
publicStringPassword
{
get
{
vararr=this.PasswordBytes;
returnarr==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>
publicclassPgSQLDatabaseConfiguration
{
/// <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>
publicStringName{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>
publicStringSearchPath{get;set;}
}
/// <summary>
/// This class contains all passive data configuration related to protocol used to communicate with backend process.
/// </summary>
publicclassPgSQLProtocolConfiguration
{
/// <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>
publicBooleanForceTypeIDLoad{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>
publicBooleanDisableBinaryProtocolSend{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>
publicBooleanDisableBinaryProtocolReceive{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>
publicsealedclassPgSQLConnectionCreationInfo:NetworkConnectionCreationInfo<PgSQLConnectionCreationInfoData,PgSQLConnectionConfiguration,PgSQLInitializationConfiguration,PgSQLProtocolConfiguration,PgSQLPoolingConfiguration,PgSQLAuthenticationConfiguration>
{
privateconstStringSCRAM_SHA_256="SCRAM-SHA-256";
privateconstStringSCRAM_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>
publicPgSQLConnectionCreationInfo(
PgSQLConnectionCreationInfoDatadata
):base(data)
{
this.CreateSASLMechanism=(names)=>
{
FluentCryptography.Digest.BlockDigestAlgorithmalgorithm;
StringmechanismName;
if(String.IsNullOrEmpty(names))
{
algorithm=null;
mechanismName=null;
}
else
{
if(names.IndexOf(SCRAM_SHA_512)>=0)
{
algorithm=newFluentCryptography.Digest.SHA512();
mechanismName=SCRAM_SHA_512;
}
elseif(names.IndexOf(SCRAM_SHA_256)>=0)
{
algorithm=newFluentCryptography.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>
publicFunc<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>
publicAction<Byte[]>OnSASLSCRAMSuccess{get;set;}
}
}
/// <summary>
/// This class contains extension methods for types defined in this assembly.
/// </summary>
publicstaticpartialclassE_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>
publicstaticPgSQLConnectionCreationInfoDataCreateCopy(thisPgSQLConnectionCreationInfoDatadata)
{
#if !NETSTANDARD1_0
varconn=data.Connection;
#endif
varinit=data.Initialization;
vardb=init?.Database;
varprotocol=init?.Protocol;
varpool=init?.ConnectionPool;
varauth=init?.Authentication;
returnnewPgSQLConnectionCreationInfoData()
{
#if !NETSTANDARD1_0
Connection=newPgSQLConnectionConfiguration()
{
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=newPgSQLProtocolConfiguration()
{
ForceTypeIDLoad=protocol?.ForceTypeIDLoad??false,
DisableBinaryProtocolSend=protocol?.DisableBinaryProtocolSend??false,
DisableBinaryProtocolReceive=protocol?.DisableBinaryProtocolReceive??false
},
ConnectionPool =new PgSQLPoolingConfiguration()
{
ConnectionsOwnStringPool = pool?.ConnectionsOwnStringPool??false
}
}
};
}
}