@alacard-project/config-sdk 1.0.0

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.
@@ -0,0 +1,12 @@
1
+ export interface ConfigMap {
2
+ [key: string]: string;
3
+ }
4
+
5
+ export interface ConfigOptions {
6
+ serviceName: string;
7
+ environment: string;
8
+ grpcUrl: string;
9
+ version?: string;
10
+ kafkaBrokers?: string[];
11
+ useDotenvFallback?: boolean;
12
+ }
@@ -0,0 +1,84 @@
1
+ import axios, { AxiosInstance } from 'axios';
2
+
3
+ export interface VaultOptions {
4
+ address: string;
5
+ roleId: string;
6
+ secretId: string;
7
+ pkiPath?: string;
8
+ }
9
+
10
+ export interface VaultCerts {
11
+ ca: string;
12
+ certificate: string;
13
+ privateKey: string;
14
+ }
15
+
16
+ export class VaultClient {
17
+ private http: AxiosInstance;
18
+ private options: VaultOptions;
19
+ private token: string | null = null;
20
+ private tokenExpiry: number = 0;
21
+
22
+ constructor(options: VaultOptions) {
23
+ this.options = options;
24
+ this.http = axios.create({
25
+ baseURL: `${options.address}/v1`,
26
+ });
27
+ }
28
+
29
+ private async login(): Promise<void> {
30
+ if (this.token && Date.now() < this.tokenExpiry) {
31
+ return;
32
+ }
33
+
34
+ try {
35
+ const response = await this.http.post('/auth/approle/login', {
36
+ role_id: this.options.roleId,
37
+ secret_id: this.options.secretId,
38
+ });
39
+
40
+ const { client_token, lease_duration } = response.data.auth;
41
+ this.token = client_token;
42
+ // Buffer of 60 seconds for token renewal
43
+ this.tokenExpiry = Date.now() + (lease_duration - 60) * 1000;
44
+
45
+ this.http.defaults.headers.common['X-Vault-Token'] = this.token;
46
+ console.log('[VaultSDK] Successfully authenticated with AppRole');
47
+ } catch (error: any) {
48
+ throw new Error(`[VaultSDK] Login failed: ${error.response?.data?.errors?.[0] || error.message}`);
49
+ }
50
+ }
51
+
52
+ async getKVSecrets(path: string): Promise<Record<string, string>> {
53
+ await this.login();
54
+ try {
55
+ const response = await this.http.get(`/secret/data/${path}`);
56
+ return response.data.data.data;
57
+ } catch (error: any) {
58
+ if (error.response?.status === 404) {
59
+ console.warn(`[VaultSDK] Secret not found at path: ${path}`);
60
+ return {};
61
+ }
62
+ throw new Error(`[VaultSDK] Failed to fetch secrets: ${error.message}`);
63
+ }
64
+ }
65
+
66
+ async issueCertificate(commonName: string): Promise<VaultCerts> {
67
+ await this.login();
68
+ const pkiPath = this.options.pkiPath || 'pki/issue/config-service';
69
+ try {
70
+ const response = await this.http.post(pkiPath, {
71
+ common_name: commonName,
72
+ });
73
+
74
+ const { ca_chain, certificate, private_key } = response.data.data;
75
+ return {
76
+ ca: ca_chain[0] || '', // Use the first CA in the chain
77
+ certificate,
78
+ privateKey: private_key,
79
+ };
80
+ } catch (error: any) {
81
+ throw new Error(`[VaultSDK] Failed to issue certificate: ${error.message}`);
82
+ }
83
+ }
84
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "CommonJS",
4
+ "target": "ES2022",
5
+ "declaration": true,
6
+ "outDir": "./dist",
7
+ "rootDir": "./src",
8
+ "strict": true,
9
+ "esModuleInterop": true,
10
+ "skipLibCheck": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "moduleResolution": "node"
13
+ },
14
+ "include": [
15
+ "src/**/*"
16
+ ],
17
+ "exclude": [
18
+ "node_modules",
19
+ "dist"
20
+ ]
21
+ }