@alacard-project/config-sdk 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/clients/config.client.d.ts +0 -3
- package/dist/clients/config.client.js +11 -70
- package/dist/{modules/config.module.d.ts → config.module.d.ts} +1 -1
- package/dist/{modules/config.module.js → config.module.js} +12 -14
- package/dist/index.d.ts +3 -2
- package/dist/index.js +5 -2
- package/package.json +24 -20
- package/eslint.config.mjs +0 -29
- package/proto/config.proto +0 -39
- package/src/clients/config.client.ts +0 -252
- package/src/clients/vault.client.ts +0 -82
- package/src/constants/index.ts +0 -1
- package/src/enums/env.enum.ts +0 -7
- package/src/generated/config.ts +0 -834
- package/src/index.ts +0 -8
- package/src/modules/config.module.ts +0 -28
- package/src/types/config.types.ts +0 -38
- package/src/types/grpc.types.ts +0 -15
- package/src/types/types.ts +0 -12
- package/src/utils/nest-helpers.ts +0 -69
- package/test/config.client.spec.ts +0 -108
- package/test/vault.client.spec.ts +0 -62
- package/tsconfig.json +0 -21
- /package/dist/utils/{nest-helpers.d.ts → config.helpers.d.ts} +0 -0
- /package/dist/utils/{nest-helpers.js → config.helpers.js} +0 -0
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
|
2
|
-
import { Logger } from '@nestjs/common';
|
|
3
|
-
import { VaultOptions, VaultCerts } from '../types/config.types';
|
|
4
|
-
|
|
5
|
-
export class VaultClient {
|
|
6
|
-
private readonly logger: Logger = new Logger('VaultSDK');
|
|
7
|
-
private http: AxiosInstance;
|
|
8
|
-
private options: VaultOptions;
|
|
9
|
-
private token: string | null = null;
|
|
10
|
-
private tokenExpiry: number = 0;
|
|
11
|
-
|
|
12
|
-
constructor(options: VaultOptions) {
|
|
13
|
-
this.options = options;
|
|
14
|
-
this.http = axios.create({
|
|
15
|
-
baseURL: `${options.address}/v1`,
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
private async login(): Promise<void> {
|
|
20
|
-
if (this.token && Date.now() < this.tokenExpiry) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
const response: AxiosResponse = await this.http.post('/auth/approle/login', {
|
|
26
|
-
role_id: this.options.roleId,
|
|
27
|
-
secret_id: this.options.secretId,
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
const { client_token, lease_duration } = response.data.auth;
|
|
31
|
-
this.token = client_token;
|
|
32
|
-
this.tokenExpiry = Date.now() + (lease_duration - 60) * 1000;
|
|
33
|
-
|
|
34
|
-
this.http.defaults.headers.common['X-Vault-Token'] = this.token;
|
|
35
|
-
this.logger.log('Successfully authenticated with Vault AppRole');
|
|
36
|
-
} catch (error: unknown) {
|
|
37
|
-
const errorMsg = error instanceof Error ? (error as any).response?.data?.errors?.[0] || error.message : String(error);
|
|
38
|
-
this.logger.error(`Vault login failed: ${errorMsg}`);
|
|
39
|
-
throw new Error(`Vault login failed: ${errorMsg}`);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public async getKVSecrets(path: string): Promise<Record<string, string>> {
|
|
44
|
-
await this.login();
|
|
45
|
-
try {
|
|
46
|
-
const response: AxiosResponse = await this.http.get(`/secret/data/${path}`);
|
|
47
|
-
return response.data.data.data;
|
|
48
|
-
} catch (error: unknown) {
|
|
49
|
-
if (error && typeof error === 'object' && 'response' in error) {
|
|
50
|
-
const axiosError = error as { response?: { status?: number } };
|
|
51
|
-
if (axiosError.response?.status === 404) {
|
|
52
|
-
this.logger.warn(`Secret not found at path: ${path}`);
|
|
53
|
-
return {};
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
57
|
-
this.logger.error(`Failed to fetch secrets from path ${path}: ${errorMsg}`);
|
|
58
|
-
throw new Error(`Failed to fetch secrets: ${errorMsg}`);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public async issueCertificate(commonName: string): Promise<VaultCerts> {
|
|
63
|
-
await this.login();
|
|
64
|
-
const pkiPath = this.options.pkiPath || 'pki/issue/config-service';
|
|
65
|
-
try {
|
|
66
|
-
const response: AxiosResponse = await this.http.post(pkiPath, {
|
|
67
|
-
common_name: commonName,
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
const { ca_chain, certificate, private_key } = response.data.data;
|
|
71
|
-
return {
|
|
72
|
-
ca: (ca_chain as string[])[0] || '',
|
|
73
|
-
certificate: certificate as string,
|
|
74
|
-
privateKey: private_key as string,
|
|
75
|
-
};
|
|
76
|
-
} catch (error: unknown) {
|
|
77
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
78
|
-
this.logger.error(`Failed to issue certificate for ${commonName}: ${errorMsg}`);
|
|
79
|
-
throw new Error(`Failed to issue certificate: ${errorMsg}`);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
package/src/constants/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const CONFIG_OPTIONS = 'CONFIG_OPTIONS';
|