11import { Inject , Injectable , Logger } from '@nestjs/common'
22import { ConfigurationService } from '@/cpin-module/infrastructure/configuration/configuration.service'
3- import { generateKVConfigUpdate } from './vault.utils'
43
54export interface VaultMetadata {
65 created_time : string
@@ -28,15 +27,15 @@ export class VaultClientService {
2827 ) {
2928 }
3029
31- private async request < T = any > ( method : string , path : string , options : { body ?: any } = { } ) : Promise < T | null > {
30+ private async fetch < T = any > ( path : string , options : { method ?: string , body ?: any } = { } ) : Promise < T | null > {
3231 const url = `${ this . config . vaultInternalUrl } ${ path } `
3332 const headers : Record < string , string > = {
3433 'Content-Type' : 'application/json' ,
3534 'X-Vault-Token' : this . config . vaultToken
3635 }
3736
3837 const response = await fetch ( url , {
39- method,
38+ method : options . method ,
4039 headers,
4140 body : options . body ? JSON . stringify ( options . body ) : undefined ,
4241 } )
@@ -53,7 +52,9 @@ export class VaultClientService {
5352 async read < T = any > ( path : string ) : Promise < VaultSecret < T > | null > {
5453 if ( path . startsWith ( '/' ) ) path = path . slice ( 1 )
5554 try {
56- const data = await this . request < VaultResponse < T > > ( 'GET' , `/v1/${ this . config . vaultKvName } /data/${ path } ` )
55+ const data = await this . fetch < VaultResponse < T > > ( `/v1/${ this . config . vaultKvName } /data/${ path } ` , {
56+ method : 'GET' ,
57+ } )
5758 if ( ! data ) return null
5859 return data . data
5960 } catch ( error ) {
@@ -65,7 +66,8 @@ export class VaultClientService {
6566 async write < T = any > ( data : T , path : string ) : Promise < void > {
6667 if ( path . startsWith ( '/' ) ) path = path . slice ( 1 )
6768 try {
68- await this . request ( 'POST' , `/v1/${ this . config . vaultKvName } /data/${ path } ` , {
69+ await this . fetch ( `/v1/${ this . config . vaultKvName } /data/${ path } ` , {
70+ method : 'POST' ,
6971 body : { data } ,
7072 } )
7173 } catch ( error ) {
@@ -77,10 +79,48 @@ export class VaultClientService {
7779 async destroy ( path : string ) : Promise < void > {
7880 if ( path . startsWith ( '/' ) ) path = path . slice ( 1 )
7981 try {
80- await this . request ( 'DELETE' , `/v1/${ this . config . vaultKvName } /metadata/${ path } ` )
82+ await this . fetch ( `/v1/${ this . config . vaultKvName } /metadata/${ path } ` , {
83+ method : 'DELETE' ,
84+ } )
8185 } catch ( error ) {
8286 this . logger . error ( `Failed to destroy vault path ${ path } : ${ error } ` )
8387 throw error
8488 }
8589 }
90+
91+ async upsertPolicyAcl ( policyName : string , data : any ) {
92+ await this . fetch ( `/v1/sys/policies/acl/${ policyName } ` , {
93+ method : 'POST' ,
94+ body : data ,
95+ } )
96+ }
97+
98+ async createMount ( name : string , data : any ) {
99+ this . fetch ( `/v1/sys/mounts/${ name } /tune` , {
100+ method : 'POST' ,
101+ body : data
102+ } )
103+ }
104+
105+ async updateMount ( name : string , data : any ) {
106+ this . fetch ( `/v1/sys/mounts/${ name } /tune` , {
107+ method : 'PUT' ,
108+ body : data
109+ } )
110+ }
111+
112+ async upsertRole ( roleName : string , policies : string [ ] ) {
113+ await this . fetch ( `/v1/auth/approle/role/${ roleName } ` , {
114+ method : 'POST' ,
115+ body : {
116+ secret_id_num_uses : '0' ,
117+ secret_id_ttl : '0' ,
118+ token_max_ttl : '0' ,
119+ token_num_uses : '0' ,
120+ token_ttl : '0' ,
121+ token_type : 'batch' ,
122+ token_policies : policies ,
123+ } ,
124+ } )
125+ }
86126}
0 commit comments