1
1
using System . Security . Cryptography ;
2
2
using System . Security . Cryptography . X509Certificates ;
3
+ using Microsoft . Extensions . Configuration ;
3
4
using MongoDB . Bson ;
4
5
using MongoDB . Driver ;
5
6
using MongoDB . Driver . Encryption ;
6
7
7
8
namespace QueryableEncryption ;
8
9
9
- public static class QueryableEncryptionHelpers
10
+ public class QueryableEncryptionHelpers
10
11
{
11
- public static Dictionary < string , IReadOnlyDictionary < string , object > > GetKmsProviderCredentials ( string kmsProvider ,
12
+ private readonly IConfigurationRoot _appSettings ;
13
+ public QueryableEncryptionHelpers ( IConfigurationRoot appSettings )
14
+ {
15
+ _appSettings = appSettings ;
16
+ }
17
+
18
+ public Dictionary < string , IReadOnlyDictionary < string , object > > GetKmsProviderCredentials ( string kmsProvider ,
12
19
bool generateNewLocalKey )
13
20
{
14
21
if ( kmsProvider == "aws" )
@@ -17,8 +24,8 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
17
24
var kmsProviderCredentials = new Dictionary < string , IReadOnlyDictionary < string , object > > ( ) ;
18
25
var awsKmsOptions = new Dictionary < string , object >
19
26
{
20
- { "accessKeyId" , Environment . GetEnvironmentVariable ( "AWS_ACCESS_KEY_ID" ) ! } , // Your AWS access key ID
21
- { "secretAccessKey" , Environment . GetEnvironmentVariable ( "AWS_SECRET_ACCESS_KEY" ) ! } // Your AWS secret access key
27
+ { "accessKeyId" , _appSettings [ "Aws:AccessKeyId" ] } , // Your AWS access key ID
28
+ { "secretAccessKey" , _appSettings [ "Aws:SecretAccessKey" ] } // Your AWS secret access key
22
29
} ;
23
30
kmsProviderCredentials . Add ( kmsProvider , awsKmsOptions ) ;
24
31
// end-aws-kms-credentials
@@ -30,9 +37,9 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
30
37
var kmsProviderCredentials = new Dictionary < string , IReadOnlyDictionary < string , object > > ( ) ;
31
38
var azureKmsOptions = new Dictionary < string , object >
32
39
{
33
- { "tenantId" , Environment . GetEnvironmentVariable ( "AZURE_TENANT_ID" ) ! } , // Your Azure tenant ID
34
- { "clientId" , Environment . GetEnvironmentVariable ( "AZURE_CLIENT_ID" ) ! } , // Your Azure client ID
35
- { "clientSecret" , Environment . GetEnvironmentVariable ( "AZURE_CLIENT_SECRET" ) ! } // Your Azure client secret
40
+ { "tenantId" , _appSettings [ "Azure:TenantId" ] } , // Your Azure tenant ID
41
+ { "clientId" , _appSettings [ "Azure:ClientId" ] } , // Your Azure client ID
42
+ { "clientSecret" , _appSettings [ "Azure:ClientSecret" ] } // Your Azure client secret
36
43
} ;
37
44
kmsProviderCredentials . Add ( kmsProvider , azureKmsOptions ) ;
38
45
// end-azure-kms-credentials
@@ -44,8 +51,8 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
44
51
var kmsProviderCredentials = new Dictionary < string , IReadOnlyDictionary < string , object > > ( ) ;
45
52
var gcpKmsOptions = new Dictionary < string , object >
46
53
{
47
- { "email" , Environment . GetEnvironmentVariable ( "GCP_EMAIL" ) ! } , // Your GCP email
48
- { "privateKey" , Environment . GetEnvironmentVariable ( "GCP_PRIVATE_KEY" ) ! } // Your GCP private key
54
+ { "email" , _appSettings [ "Gcp:Email" ] } , // Your GCP email
55
+ { "privateKey" , _appSettings [ "Gcp:PrivateKey" ] } // Your GCP private key
49
56
} ;
50
57
kmsProviderCredentials . Add ( kmsProvider , gcpKmsOptions ) ;
51
58
// end-gcp-kms-credentials
@@ -57,7 +64,7 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
57
64
var kmsProviderCredentials = new Dictionary < string , IReadOnlyDictionary < string , object > > ( ) ;
58
65
var kmipKmsOptions = new Dictionary < string , object >
59
66
{
60
- { "endpoint" , Environment . GetEnvironmentVariable ( "KMIP_KMS_ENDPOINT" ) ! } // Your KMIP KMS endpoint
67
+ { "endpoint" , _appSettings [ "Kmip:KmsEndpoint" ] } // Your KMIP KMS endpoint
61
68
} ;
62
69
kmsProviderCredentials . Add ( kmsProvider , kmipKmsOptions ) ;
63
70
// end-kmip-kms-credentials
@@ -67,7 +74,7 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
67
74
{
68
75
if ( generateNewLocalKey )
69
76
{
70
- File . Delete ( "master-key.txt" ) ;
77
+ File . Delete ( "customer- master-key.txt" ) ;
71
78
72
79
// start-generate-local-key
73
80
using var randomNumberGenerator = RandomNumberGenerator . Create ( ) ;
@@ -76,7 +83,7 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
76
83
var bytes = new byte [ 96 ] ;
77
84
randomNumberGenerator . GetBytes ( bytes ) ;
78
85
var localCustomerMasterKeyBase64Write = Convert . ToBase64String ( bytes ) ;
79
- File . WriteAllText ( "master-key.txt" , localCustomerMasterKeyBase64Write ) ;
86
+ File . WriteAllText ( "customer- master-key.txt" , localCustomerMasterKeyBase64Write ) ;
80
87
}
81
88
catch ( Exception e )
82
89
{
@@ -90,7 +97,7 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
90
97
var kmsProviderCredentials = new Dictionary < string , IReadOnlyDictionary < string , object > > ( ) ;
91
98
try
92
99
{
93
- var localCustomerMasterKeyBase64Read = File . ReadAllText ( "master-key.txt" ) ;
100
+ var localCustomerMasterKeyBase64Read = File . ReadAllText ( "customer- master-key.txt" ) ;
94
101
var localCustomerMasterKeyBytes = Convert . FromBase64String ( localCustomerMasterKeyBase64Read ) ;
95
102
96
103
var localOptions = new Dictionary < string , object >
@@ -100,27 +107,27 @@ public static Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProv
100
107
101
108
kmsProviderCredentials . Add ( kmsProvider , localOptions ) ;
102
109
}
110
+ // end-get-local-key
103
111
catch ( Exception e )
104
112
{
105
113
Console . WriteLine ( e . Message ) ;
106
114
}
107
- // end-get-local-key
108
115
return kmsProviderCredentials ;
109
116
110
117
}
111
118
112
119
throw new Exception ( "Invalid KMS provider string" ) ;
113
120
}
114
121
115
- public static BsonDocument GetCustomerMasterKeyCredentials ( string kmsProvider )
122
+ public BsonDocument GetCustomerMasterKeyCredentials ( string kmsProvider )
116
123
{
117
124
if ( kmsProvider == "aws" )
118
125
{
119
126
// start-aws-cmk-credentials
120
127
var customerMasterKeyCredentials = new BsonDocument
121
128
{
122
- { "key" , Environment . GetEnvironmentVariable ( "AWS_KEY_ARN" ) } , // Your AWS Key ARN
123
- { "region" , Environment . GetEnvironmentVariable ( "AWS_KEY_REGION" ) } // Your AWS Key Region
129
+ { "key" , _appSettings [ "Aws:KeyArn" ] } , // Your AWS Key ARN
130
+ { "region" , _appSettings [ "Aws:KeyRegion" ] } // Your AWS Key Region
124
131
} ;
125
132
// end-aws-cmk-credentials
126
133
return customerMasterKeyCredentials ;
@@ -130,8 +137,8 @@ public static BsonDocument GetCustomerMasterKeyCredentials(string kmsProvider)
130
137
// start-azure-cmk-credentials
131
138
var customerMasterKeyCredentials = new BsonDocument
132
139
{
133
- { "keyVaultEndpoint" , Environment . GetEnvironmentVariable ( "AZURE_KEY_VAULT_ENDPOINT" ) } , // Your Azure Key Vault Endpoint
134
- { "keyName" , Environment . GetEnvironmentVariable ( "AZURE_KEY_NAME" ) } // Your Azure Key Name
140
+ { "keyVaultEndpoint" , _appSettings [ "Azure:KeyVaultEndpoint" ] } , // Your Azure Key Vault Endpoint
141
+ { "keyName" , _appSettings [ "Azure:KeyName" ] } // Your Azure Key Name
135
142
} ;
136
143
// end-azure-cmk-credentials
137
144
return customerMasterKeyCredentials ;
@@ -141,43 +148,44 @@ public static BsonDocument GetCustomerMasterKeyCredentials(string kmsProvider)
141
148
// start-gcp-cmk-credentials
142
149
var customerMasterKeyCredentials = new BsonDocument
143
150
{
144
- { "projectId" , Environment . GetEnvironmentVariable ( "GCP_PROJECT_ID" ) } , // Your GCP Project ID
145
- { "location" , Environment . GetEnvironmentVariable ( "GCP_LOCATION" ) } , // Your GCP Key Location
146
- { "keyRing" , Environment . GetEnvironmentVariable ( "GCP_KEY_RING" ) } , // Your GCP Key Ring
147
- { "keyName" , Environment . GetEnvironmentVariable ( "GCP_KEY_NAME" ) } // Your GCP Key Name
151
+ { "projectId" , _appSettings [ "Gcp:ProjectId" ] } , // Your GCP Project ID
152
+ { "location" , _appSettings [ "Gcp:Location" ] } , // Your GCP Key Location
153
+ { "keyRing" , _appSettings [ "Gcp:KeyRing" ] } , // Your GCP Key Ring
154
+ { "keyName" , _appSettings [ "Gcp:KeyName" ] } // Your GCP Key Name
148
155
} ;
149
156
// end-gcp-cmk-credentials
150
- return customerMasterKeyCredentials ;
157
+ return customerMasterKeyCredentials ;
151
158
}
152
- else if ( kmsProvider == "kmip" )
159
+ else if ( kmsProvider == "kmip" || kmsProvider == "local" )
153
160
{
154
161
// start-kmip-local-cmk-credentials
155
162
var customerMasterKeyCredentials = new BsonDocument ( ) ;
156
163
// end-kmip-local-cmk-credentials
157
164
return customerMasterKeyCredentials ;
158
- } else
159
- {
165
+ }
166
+ else
167
+ {
160
168
throw new Exception ( "Invalid KMS provider string" ) ;
161
169
}
162
170
}
163
171
164
- public static AutoEncryptionOptions GetAutoEncryptionOptions ( CollectionNamespace keyVaultNamespace ,
172
+ public AutoEncryptionOptions GetAutoEncryptionOptions ( CollectionNamespace keyVaultNamespace ,
165
173
IReadOnlyDictionary < string , IReadOnlyDictionary < string , object > > kmsProviderCredentials )
166
174
{
167
175
var kmsProvider = kmsProviderCredentials . Keys . First ( ) ;
168
176
169
177
if ( kmsProvider == "kmip" )
170
178
{
179
+ var tlsOptions = GetKmipTlsOptions ( ) ;
180
+
171
181
// start-kmip-encryption-options
172
182
var extraOptions = new Dictionary < string , object >
173
183
{
174
- { "cryptSharedLibPath" , Environment . GetEnvironmentVariable (
175
- "CRYPT_SHARED_LIB_PATH" ) } // Path to your Automatic Encryption Shared Library
184
+ { "cryptSharedLibPath" , _appSettings [ "CryptSharedLibPath" ] } // Path to your Automatic Encryption Shared Library
176
185
} ;
177
186
178
- var tlsOptions = GetKmipTlsOptions ( ) ;
179
-
180
- var autoEncryptionOptions = new AutoEncryptionOptions ( keyVaultNamespace ,
187
+ var autoEncryptionOptions = new AutoEncryptionOptions (
188
+ keyVaultNamespace ,
181
189
kmsProviderCredentials ,
182
190
extraOptions : extraOptions ,
183
191
tlsOptions : tlsOptions ) ;
@@ -189,11 +197,11 @@ public static AutoEncryptionOptions GetAutoEncryptionOptions(CollectionNamespace
189
197
// start-auto-encryption-options
190
198
var extraOptions = new Dictionary < string , object >
191
199
{
192
- { "cryptSharedLibPath" , Environment . GetEnvironmentVariable (
193
- "CRYPT_SHARED_LIB_PATH" ) } // Path to your Automatic Encryption Shared Library
200
+ { "cryptSharedLibPath" , _appSettings [ "CryptSharedLibPath" ] } // Path to your Automatic Encryption Shared Library
194
201
} ;
195
202
196
- var autoEncryptionOptions = new AutoEncryptionOptions ( keyVaultNamespace ,
203
+ var autoEncryptionOptions = new AutoEncryptionOptions (
204
+ keyVaultNamespace ,
197
205
kmsProviderCredentials ,
198
206
extraOptions : extraOptions ) ;
199
207
// end-auto-encryption-options
@@ -202,7 +210,7 @@ public static AutoEncryptionOptions GetAutoEncryptionOptions(CollectionNamespace
202
210
}
203
211
}
204
212
205
- public static ClientEncryption GetClientEncryption ( IMongoClient keyVaultClient ,
213
+ public ClientEncryption GetClientEncryption ( IMongoClient keyVaultClient ,
206
214
CollectionNamespace keyVaultNamespace , Dictionary < string , IReadOnlyDictionary < string , object > > kmsProviderCredentials )
207
215
{
208
216
var kmsProvider = kmsProviderCredentials . Keys . First ( ) ;
@@ -236,12 +244,12 @@ public static ClientEncryption GetClientEncryption(IMongoClient keyVaultClient,
236
244
}
237
245
}
238
246
239
- private static Dictionary < string , SslSettings > GetKmipTlsOptions ( )
247
+ private Dictionary < string , SslSettings > GetKmipTlsOptions ( )
240
248
{
241
249
// start-tls-options
242
250
var tlsOptions = new Dictionary < string , SslSettings > ( ) ;
243
251
var sslSettings = new SslSettings ( ) ;
244
- var clientCertificate = new X509Certificate2 ( Environment . GetEnvironmentVariable ( "KMIP_TLS_CERT_P12" ) ! ) ; // Full path to your client certificate p12 file
252
+ var clientCertificate = new X509Certificate2 ( _appSettings [ "Kmip:TlsCertP12" ] ! ) ; // Full path to your client certificate p12 file
245
253
sslSettings . ClientCertificates = new [ ] { clientCertificate } ;
246
254
tlsOptions . Add ( "kmip" , sslSettings ) ;
247
255
// end-tls-options
0 commit comments