@rytass/secret-adapter-vault-nestjs
Version:
Rytass Sceret Vault nestjs adapter
116 lines (110 loc) • 4.04 kB
JavaScript
;
var common = require('@nestjs/common');
var config = require('@nestjs/config');
var secretAdapterVault = require('@rytass/secret-adapter-vault');
const VAULT_PATH_TOKEN = Symbol('VAULT_PATH_TOKEN');
function _ts_decorate$1(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
}
function _ts_param(paramIndex, decorator) {
return function(target, key) {
decorator(target, key, paramIndex);
};
}
class VaultService {
config;
manager;
onReadyCallbacks;
fallbackToEnvFile;
constructor(config, path){
this.config = config;
this.onReadyCallbacks = [];
this.fallbackToEnvFile = false;
const host = config.get('VAULT_HOST');
const user = config.get('VAULT_ACCOUNT');
const pass = config.get('VAULT_PASSWORD');
if (!host) {
this.fallbackToEnvFile = true;
return;
}
this.manager = new secretAdapterVault.VaultSecret(path, {
host,
auth: {
account: user,
password: pass
},
onError: (err)=>{
this.fallbackToEnvFile = true;
this.onReadyCallbacks.forEach((done)=>done(config));
},
onReady: ()=>{
this.onReadyCallbacks.forEach((done)=>done());
}
});
}
async get(key) {
if (this.fallbackToEnvFile) {
return Promise.resolve(this.config.get(key) || '');
}
if (this.manager.state === secretAdapterVault.VaultSecretState.READY) {
return this.manager.get(key);
}
return new Promise((resolve)=>{
this.onReadyCallbacks.push((dataSource = this.manager)=>{
resolve(dataSource.get(key));
});
});
}
}
VaultService = _ts_decorate$1([
common.Injectable(),
_ts_param(1, common.Inject(VAULT_PATH_TOKEN)),
_ts_metadata("design:type", Function),
_ts_metadata("design:paramtypes", [
typeof config.ConfigService === "undefined" ? Object : config.ConfigService,
String
])
], VaultService);
function _ts_decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
class VaultModule {
static forRoot(options = {
path: '/'
}) {
return {
imports: [
config.ConfigModule.forRoot({
envFilePath: options.fallbackFile
})
],
module: VaultModule,
providers: [
config.ConfigService,
{
provide: VAULT_PATH_TOKEN,
useValue: options.path
},
VaultService
],
exports: [
VaultService
]
};
}
}
VaultModule = _ts_decorate([
common.Global(),
common.Module({})
], VaultModule);
exports.VaultModule = VaultModule;
exports.VaultService = VaultService;