UNPKG

@1mill/sops

Version:

```bash npm install @1mill/sops ```

3 lines (2 loc) 3.48 kB
var e="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function r(e,n,i){if(!e.s){if(i instanceof t){if(!i.s)return void(i.o=r.bind(null,e,n));1&n&&(n=i.s),i=i.v}if(i&&i.then)return void i.then(r.bind(null,e,n),r.bind(null,e,2));e.s=n,e.v=i;var s=e.o;s&&s(e)}}var t=function(){function e(){}return e.prototype.then=function(t,n){var i=new e,s=this.s;if(s){var o=1&s?t:n;if(o){try{r(i,1,o(this.v))}catch(e){r(i,2,e)}return i}return this}return this.o=function(e){try{var s=e.v;1&e.s?r(i,1,t?t(s):s):n?r(i,1,n(s)):r(i,2,s)}catch(e){r(i,2,e)}},i},e}();function n(e){return e instanceof t&&1&e.s}var i=require("fs"),s=require("@aws-sdk/client-kms"),o=s.DecryptCommand,c=s.KMSClient,u=require("./fetchNodeEnv").fetchNodeEnv,f=require("./walkAndDecrypt").walkAndDecrypt;module.exports={Sops:function(){function s(e){var r=e.accessKeyId,t=void 0===r?u("MILL_SOPS_AWS_ACCESS_KEY_ID")||u("AWS_ACCESS_KEY_ID")||u("AWS_ACCESS_KEY"):r,n=e.endpoint,i=void 0===n?u("MILL_SOPS_AWS_ENDPOINT")||u("AWS_ENDPOINT"):n,s=e.file,o=void 0===s?u("MILL_SOPS_FILE")||"production"===u("NODE_ENV")?"prod.secrets.sops.json":"dev.secrets.sops.json":s,c=e.region,f=void 0===c?u("MILL_SOPS_AWS_REGION")||u("AWS_REGION"):c,h=e.secretAccessKey,S=void 0===h?u("MILL_SOPS_AWS_SECRET_ACCESS_KEY")||u("AWS_SECRET_ACCESS_KEY"):h,d=e.sessionToken,a=void 0===d?u("AWS_SESSION_TOKEN"):d;if(this.accessKeyId=t,!this.accessKeyId)throw new Error('AWS "accessKeyId" is required');if(this.region=f,!this.region)throw new Error('AWS "region" is required');if(this.secretAccessKey=S,!this.secretAccessKey)throw new Error('AWS "secretAccessKey" is required');if(this.sessionToken=a,this.endpoint=i,this.client=void 0,this.file=o,!this.file)throw new Error('SOPS "file" is required');this.secrets={}}var h=s.prototype;return h._buildSecrets=function(){try{var s,u=this,h=function(){u.secrets=f({tree:S,key:d})};if(0!==Object.keys(u.secrets).length)return Promise.resolve();void 0===u.client&&(u.client=new c({credentials:{accessKeyId:u.accessKeyId,secretAccessKey:u.secretAccessKey,sessionToken:u.sessionToken},endpoint:u.endpoint,region:u.region}));var S=JSON.parse(i.readFileSync(u.file,"utf-8").toString()),d=null,a=function(i,s,o){if("function"==typeof i[e]){var c,u,f,h=i[e]();if(function e(i){try{for(;!((c=h.next()).done||o&&o());)if((i=s(c.value))&&i.then){if(!n(i))return void i.then(e,f||(f=r.bind(null,u=new t,2)));i=i.v}u?r(u,1,i):u=i}catch(e){r(u||(u=new t),2,e)}}(),h.return){var S=function(e){try{c.done||h.return()}catch(e){}return e};if(u&&u.then)return u.then(S,function(e){throw S(e)});S()}return u}if(!("length"in i))throw new TypeError("Object is not iterable");for(var d=[],a=0;a<i.length;a++)d.push(i[a]);return function(e,i,s){var o,c,u=-1;return function f(h){try{for(;++u<e.length&&(!s||!s());)if((h=i(u))&&h.then){if(!n(h))return void h.then(f,c||(c=r.bind(null,o=new t,2)));h=h.v}o?r(o,1,h):o=h}catch(e){r(o||(o=new t),2,e)}}(),o}(d,function(e){return s(d[e])},o)}(S.sops.kms,function(e){if(e.enc){var r=new o({CiphertextBlob:Buffer.from(e.enc,"base64")});return Promise.resolve(u.client.send(r)).then(function(e){(d=e.Plaintext)&&(s=1)})}},function(){return s});return Promise.resolve(a&&a.then?a.then(h):h())}catch(e){return Promise.reject(e)}},h.decrypt=function(e){try{var r=this;return Promise.resolve(r._buildSecrets()).then(function(){return e?r.secrets[e]:r.secrets})}catch(e){return Promise.reject(e)}},s}()}; //# sourceMappingURL=index.js.map