Guest User

Untitled

a guest
Jan 2nd, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. // Require the module.
  2. var AWS_SM = require("awssecretmanager");
  3.  
  4. // Request object. * Based on the requirement we can change it. *
  5. let feed = {
  6. region : "ap-south-1",
  7. secretName : "stage/db/mongoDB",
  8. env : "staging",
  9. appName : "auth"
  10. };
  11.  
  12. // Util function to fetch the secret data based on the request object.
  13. AWS_SM.getSecretdata(feed, (err, secret)=>{
  14. if(err)
  15. throw err;
  16. else{
  17. // DB connection logic here.
  18. }
  19. });
  20.  
  21. // Here I stored the KEY: VALUE pattern in secret manager is :
  22.  
  23. "appName-env-keyName" : "value" Eg : "auth-staging-dbName" : "stagingDBName"
  24.  
  25. Utils response for the above key value pair is
  26.  
  27. "keyName" : "StagingDBName"
  28.  
  29. If we want to store any secret data based on env & app, we have to follow the same pattern ("appName-env-keyName" : "value") in secret manager.
  30. Note : In case if we are following the other pattern, based on that we can optimise the Utility.
  31. Here to connect to the secretmanager I used my system aws configure data.
  32.  
  33.  
  34. Example Secret Data :
  35. //secretName : "stage/db/mongoDB"
  36. //region : "ap-south-1"
  37.  
  38. "auth-staging-dbName" : "stagingDB",
  39. "auth-staging-userName" : "stagingDbAdmin",
  40. "auth-staging-password" : "admin@123",
  41. "auth-live-dbName" : "liveDB",
  42. "auth-live-userName" : "liveDbAdmin",
  43. "auth-live-password" : "admin@123"
  44.  
  45. example request object for Util function :
  46.  
  47. {
  48. region : "ap-south-1",
  49. secretName : "stage/db/mongoDB",
  50. env : "staging",
  51. appName : "auth"
  52. }
  53.  
  54. Response from the util function is :
  55.  
  56. {
  57. dbName : "stagingDB",
  58. userName : "stagingDbAdmin",
  59. password : "admin@123"
  60. }
  61.  
  62. After receiving this data from the sesret manager, we can establish a DB connection (that is in application logic).
Add Comment
Please, Sign In to add comment