5
5
using System . Data . OracleClient ;
6
6
using System . Linq ;
7
7
using System . Text ;
8
+ using System . Threading ;
8
9
using System . Threading . Tasks ;
9
10
using System . Transactions ;
10
11
using Microsoft . AspNetCore . Builder ;
18
19
using Microsoft . OpenApi . Models ;
19
20
using Newtonsoft . Json ;
20
21
using Oracle . ManagedDataAccess . Client ;
22
+ // dotnet add package OCI.DotNetSDK.Common --version 29.0.0
23
+ // dotnet add package OCI.DotNetSDK.Secrets --version 29.0.0
24
+ using System . IO ;
25
+ using Oci . SecretsService . Responses ;
26
+ using Oci . SecretsService ;
27
+ using Oci . Common ;
28
+ using Oci . Common . Auth ;
29
+ using Oci . SecretsService . Models ;
30
+
31
+
21
32
22
33
namespace inventory_dotnet
23
34
{
@@ -79,6 +90,7 @@ public String ListenForMessages()
79
90
//Other options include...
80
91
// using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
81
92
// DbProviderFactory factory = DbProviderFactories.GetFactory("Oracle.ManagedDataAccess.Client"); DbCommand oracleCommand = factory.CreateCommand();
93
+ getSecretFromVault ( ) ;
82
94
String tnsAdmin = Environment . GetEnvironmentVariable ( "TNS_ADMIN" ) ;
83
95
OracleConfiguration . WalletLocation = tnsAdmin ;
84
96
String pw = Environment . GetEnvironmentVariable ( "DB_PASSWORD" ) ;
@@ -157,7 +169,7 @@ public String ListenForMessages()
157
169
checkInventoryReturnLocationCommand . Parameters . Add ( p_inventorylocationParam ) ;
158
170
checkInventoryReturnLocationCommand . ExecuteNonQuery ( ) ;
159
171
160
- // direct query version (ie not using sproc )...
172
+ // direct query version (ie not using stored procedure )...
161
173
// checkInventoryCommand.CommandText =
162
174
// @"update inventory set inventorycount = inventorycount - 1 where inventoryid = " +
163
175
// order.itemid +
@@ -183,10 +195,7 @@ public String ListenForMessages()
183
195
inventory . suggestiveSale = inventoryLocation . Equals ( "null" ) ? "" : "beer" ;
184
196
string inventoryJSON =
185
197
JsonConvert . SerializeObject ( inventory ) ;
186
- System
187
- . Console
188
- . WriteLine ( "order.itemid inventoryJSON {0}" ,
189
- inventoryJSON ) ;
198
+ System . Console . WriteLine ( "order.itemid inventoryJSON {0}" , inventoryJSON ) ;
190
199
//enqueue to inventory queue (in param)
191
200
OracleCommand inventorySendMessageCommand =
192
201
new OracleCommand ( ) ;
@@ -214,5 +223,52 @@ public String ListenForMessages()
214
223
}
215
224
}
216
225
}
226
+
227
+ public String getSecretFromVault ( ) {
228
+ System . Console . WriteLine ( "getSecretFromVault " ) ;
229
+ String vaultSecretOCID = Environment . GetEnvironmentVariable ( "VAULT_SECRET_OCID" ) ;
230
+ System . Console . WriteLine ( "vaultSecretOCID {0}" , vaultSecretOCID ) ;
231
+ if ( vaultSecretOCID == "" ) {
232
+ return "" ;
233
+ }
234
+ String ociRegion = Environment . GetEnvironmentVariable ( "OCI_REGION" ) ;
235
+ System . Console . WriteLine ( "ociRegion {0}" , ociRegion ) ;
236
+ if ( ociRegion == "" ) {
237
+ return "" ;
238
+ }
239
+ var response = getSecretResponse ( vaultSecretOCID , ociRegion ) . GetAwaiter ( ) . GetResult ( ) ;
240
+ System . Console . WriteLine ( "getSecretFromVault response {0}" , response ) ;
241
+ System . Console . WriteLine ( "getSecretFromVault response.SecretBundle.SecretId; {0}" , response . SecretBundle . SecretId ) ;
242
+ System . Console . WriteLine ( "getSecretFromVault secretBundle {0}" , response . SecretBundle . SecretBundleContent ) ;
243
+ byte [ ] data = System . Convert . FromBase64String ( ( ( Base64SecretBundleContentDetails ) response . SecretBundle . SecretBundleContent ) . Content ) ;
244
+ System . Console . WriteLine ( "getSecretFromVault System.Text.ASCIIEncoding.ASCII.GetString(data) {0}" , System . Text . ASCIIEncoding . ASCII . GetString ( data ) ) ;
245
+ return System . Text . ASCIIEncoding . ASCII . GetString ( data ) ;
246
+ }
247
+
248
+ public static async Task < GetSecretBundleResponse > getSecretResponse ( string vaultSecretOCID , string ociRegion )
249
+ {
250
+ var getSecretBundleRequest = new Oci . SecretsService . Requests . GetSecretBundleRequest
251
+ {
252
+ // SecretId = "ocid1.vaultsecret.oc1.iad.amaaaaaaq33dybya5qo2jtafngz7krbqdt64fygvm4v5ml7dnamg6ct7vaza"
253
+ SecretId = vaultSecretOCID
254
+ } ;
255
+ var provider = new InstancePrincipalsAuthenticationDetailsProvider ( ) ;
256
+ try
257
+ {
258
+ using ( var client = new SecretsClient ( provider , new ClientConfiguration ( ) ) )
259
+ {
260
+ // client.SetRegion("us-ashburn-1");
261
+ client . SetRegion ( ociRegion ) ;
262
+ return await client . GetSecretBundle ( getSecretBundleRequest ) ;
263
+ }
264
+ }
265
+ catch ( Exception e )
266
+ {
267
+ Console . WriteLine ( $ "GetSecretBundle Failed with { e . Message } ") ;
268
+ throw e ;
269
+ }
270
+ }
271
+
272
+
217
273
}
218
274
}
0 commit comments