55using System . Data . OracleClient ;
66using System . Linq ;
77using System . Text ;
8+ using System . Threading ;
89using System . Threading . Tasks ;
910using System . Transactions ;
1011using Microsoft . AspNetCore . Builder ;
1819using Microsoft . OpenApi . Models ;
1920using Newtonsoft . Json ;
2021using 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+
2132
2233namespace inventory_dotnet
2334{
@@ -79,6 +90,7 @@ public String ListenForMessages()
7990 //Other options include...
8091 // using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
8192 // DbProviderFactory factory = DbProviderFactories.GetFactory("Oracle.ManagedDataAccess.Client"); DbCommand oracleCommand = factory.CreateCommand();
93+ getSecretFromVault ( ) ;
8294 String tnsAdmin = Environment . GetEnvironmentVariable ( "TNS_ADMIN" ) ;
8395 OracleConfiguration . WalletLocation = tnsAdmin ;
8496 String pw = Environment . GetEnvironmentVariable ( "DB_PASSWORD" ) ;
@@ -157,7 +169,7 @@ public String ListenForMessages()
157169 checkInventoryReturnLocationCommand . Parameters . Add ( p_inventorylocationParam ) ;
158170 checkInventoryReturnLocationCommand . ExecuteNonQuery ( ) ;
159171
160- // direct query version (ie not using sproc )...
172+ // direct query version (ie not using stored procedure )...
161173 // checkInventoryCommand.CommandText =
162174 // @"update inventory set inventorycount = inventorycount - 1 where inventoryid = " +
163175 // order.itemid +
@@ -183,10 +195,7 @@ public String ListenForMessages()
183195 inventory . suggestiveSale = inventoryLocation . Equals ( "null" ) ? "" : "beer" ;
184196 string inventoryJSON =
185197 JsonConvert . SerializeObject ( inventory ) ;
186- System
187- . Console
188- . WriteLine ( "order.itemid inventoryJSON {0}" ,
189- inventoryJSON ) ;
198+ System . Console . WriteLine ( "order.itemid inventoryJSON {0}" , inventoryJSON ) ;
190199 //enqueue to inventory queue (in param)
191200 OracleCommand inventorySendMessageCommand =
192201 new OracleCommand ( ) ;
@@ -214,5 +223,52 @@ public String ListenForMessages()
214223 }
215224 }
216225 }
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+
217273 }
218274}
0 commit comments