@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.
- package/.idea/workspace.xml +45 -0
- package/README.md +107 -0
- package/dist/config-client.d.ts +33 -0
- package/dist/config-client.js +172 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/proto/config.d.ts +116 -0
- package/dist/proto/config.js +624 -0
- package/dist/types/types.d.ts +11 -0
- package/dist/types/types.js +2 -0
- package/dist/vault-client.d.ts +21 -0
- package/dist/vault-client.js +72 -0
- package/package.json +25 -0
- package/proto/config.proto +39 -0
- package/src/config-client.ts +180 -0
- package/src/index.ts +1 -0
- package/src/proto/config.ts +835 -0
- package/src/types/types.ts +12 -0
- package/src/vault-client.ts +84 -0
- package/tsconfig.json +21 -0
|
@@ -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
|
+
}
|