infrahub-sdk 0.0.7 → 0.0.9
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/README.md +31 -2
- package/dist/index.d.ts +13 -1
- package/dist/index.js +83 -10
- package/dist/index.test.js +60 -0
- package/dist/integration.test.d.ts +9 -0
- package/dist/integration.test.js +52 -0
- package/dist/rest.test.d.ts +1 -0
- package/dist/rest.test.js +930 -0
- package/examples/expired-certificate-example.ts +36 -0
- package/package.json +1 -1
- package/src/index.test.ts +73 -0
- package/src/index.ts +109 -14
- package/src/integration.test.ts +63 -0
- package/src/rest.test.ts +1107 -0
- package/test-schema.ts +76 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Example: Connecting to Infrahub with an expired or self-signed certificate
|
|
2
|
+
import { InfrahubClient, InfrahubClientOptions } from 'infrahub-sdk';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
|
|
5
|
+
// Option 1: Disable certificate verification (for development/testing only)
|
|
6
|
+
// This will bypass ALL certificate validation including expired certificates
|
|
7
|
+
const clientWithoutVerification = new InfrahubClient({
|
|
8
|
+
address: 'https://your-infrahub-server.com',
|
|
9
|
+
token: 'your-api-token',
|
|
10
|
+
tls: {
|
|
11
|
+
rejectUnauthorized: false
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// Test the connection
|
|
16
|
+
async function testConnection() {
|
|
17
|
+
try {
|
|
18
|
+
const version = await clientWithoutVerification.getVersion();
|
|
19
|
+
console.log('Successfully connected! Infrahub version:', version);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error('Connection failed:', error);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
testConnection();
|
|
26
|
+
|
|
27
|
+
// Option 2: Use a custom CA certificate (production-friendly approach)
|
|
28
|
+
// Uncomment the following to use a custom CA certificate instead:
|
|
29
|
+
//
|
|
30
|
+
// const clientWithCustomCA = new InfrahubClient({
|
|
31
|
+
// address: 'https://your-infrahub-server.com',
|
|
32
|
+
// token: 'your-api-token',
|
|
33
|
+
// tls: {
|
|
34
|
+
// ca: fs.readFileSync('/path/to/your/ca-certificate.pem')
|
|
35
|
+
// }
|
|
36
|
+
// });
|
package/package.json
CHANGED
package/src/index.test.ts
CHANGED
|
@@ -45,6 +45,58 @@ describe('InfrahubClient', () => {
|
|
|
45
45
|
consoleErrorSpy.mockRestore();
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
+
it('should provide helpful error message for certificate errors', async () => {
|
|
49
|
+
const mockRequest = jest
|
|
50
|
+
.fn()
|
|
51
|
+
.mockImplementation(() =>
|
|
52
|
+
Promise.reject(new Error('request failed, reason: certificate has expired'))
|
|
53
|
+
);
|
|
54
|
+
(client as any).graphqlClient.request = mockRequest;
|
|
55
|
+
const consoleErrorSpy = jest
|
|
56
|
+
.spyOn(console, 'error')
|
|
57
|
+
.mockImplementation(() => {});
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
await client.executeGraphQL('{ test }');
|
|
61
|
+
expect(true).toBe(false); // Should not reach here
|
|
62
|
+
} catch (error) {
|
|
63
|
+
expect(error).toBeInstanceOf(Error);
|
|
64
|
+
if (error instanceof Error) {
|
|
65
|
+
expect(error.message).toContain('certificate has expired');
|
|
66
|
+
expect(error.message).toContain('Certificate validation failed');
|
|
67
|
+
expect(error.message).toContain('rejectUnauthorized: false');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
consoleErrorSpy.mockRestore();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should provide helpful error message for self-signed certificate errors', async () => {
|
|
75
|
+
const mockRequest = jest
|
|
76
|
+
.fn()
|
|
77
|
+
.mockImplementation(() =>
|
|
78
|
+
Promise.reject(new Error('request failed, reason: self-signed certificate'))
|
|
79
|
+
);
|
|
80
|
+
(client as any).graphqlClient.request = mockRequest;
|
|
81
|
+
const consoleErrorSpy = jest
|
|
82
|
+
.spyOn(console, 'error')
|
|
83
|
+
.mockImplementation(() => {});
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
await client.executeGraphQL('{ test }');
|
|
87
|
+
expect(true).toBe(false); // Should not reach here
|
|
88
|
+
} catch (error) {
|
|
89
|
+
expect(error).toBeInstanceOf(Error);
|
|
90
|
+
if (error instanceof Error) {
|
|
91
|
+
expect(error.message).toContain('self-signed certificate');
|
|
92
|
+
expect(error.message).toContain('Certificate validation failed');
|
|
93
|
+
expect(error.message).toContain('tls: { rejectUnauthorized: false }');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
consoleErrorSpy.mockRestore();
|
|
98
|
+
});
|
|
99
|
+
|
|
48
100
|
it('should initialize rest client with baseUrl', () => {
|
|
49
101
|
expect((client as any).rest).toBeDefined();
|
|
50
102
|
});
|
|
@@ -86,6 +138,27 @@ describe('InfrahubClient', () => {
|
|
|
86
138
|
expect((tlsClient as any).graphqlClient).toBeInstanceOf(GraphQLClient);
|
|
87
139
|
});
|
|
88
140
|
|
|
141
|
+
it('should create agent with rejectUnauthorized: false when explicitly set', () => {
|
|
142
|
+
const https = require('https');
|
|
143
|
+
const createAgentSpy = jest.spyOn(https, 'Agent');
|
|
144
|
+
|
|
145
|
+
const options: InfrahubClientOptions = {
|
|
146
|
+
address: baseURL,
|
|
147
|
+
token: token,
|
|
148
|
+
tls: {
|
|
149
|
+
rejectUnauthorized: false
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const tlsClient = new InfrahubClient(options);
|
|
154
|
+
|
|
155
|
+
expect(createAgentSpy).toHaveBeenCalled();
|
|
156
|
+
const agentOptions = createAgentSpy.mock.calls[createAgentSpy.mock.calls.length - 1][0] as any;
|
|
157
|
+
expect(agentOptions.rejectUnauthorized).toBe(false);
|
|
158
|
+
|
|
159
|
+
createAgentSpy.mockRestore();
|
|
160
|
+
});
|
|
161
|
+
|
|
89
162
|
it('should accept TLS configuration with custom CA certificate', () => {
|
|
90
163
|
const options: InfrahubClientOptions = {
|
|
91
164
|
address: baseURL,
|
package/src/index.ts
CHANGED
|
@@ -27,10 +27,14 @@ export interface TLSConfig {
|
|
|
27
27
|
/**
|
|
28
28
|
* If true, the server certificate is verified against the list of supplied CAs.
|
|
29
29
|
* Set to false to disable certificate verification (not recommended for production).
|
|
30
|
+
*
|
|
31
|
+
* Note: Setting this to false will bypass all certificate validation, including
|
|
32
|
+
* expired certificates, self-signed certificates, and hostname mismatches.
|
|
30
33
|
*/
|
|
31
34
|
rejectUnauthorized?: boolean;
|
|
32
35
|
/**
|
|
33
36
|
* Optional CA certificates to trust. Can be a string or Buffer containing the certificate(s).
|
|
37
|
+
* Use this when connecting to servers with custom or self-signed certificates.
|
|
34
38
|
*/
|
|
35
39
|
ca?: string | Buffer | Array<string | Buffer>;
|
|
36
40
|
/**
|
|
@@ -49,7 +53,15 @@ export interface InfrahubClientOptions {
|
|
|
49
53
|
branch?: string;
|
|
50
54
|
/**
|
|
51
55
|
* TLS/SSL configuration options for HTTPS connections.
|
|
52
|
-
* Use this to handle custom certificates or disable certificate verification.
|
|
56
|
+
* Use this to handle custom certificates, expired certificates, or disable certificate verification.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // Disable certificate verification (development only)
|
|
60
|
+
* { tls: { rejectUnauthorized: false } }
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* // Use custom CA certificate
|
|
64
|
+
* { tls: { ca: fs.readFileSync('/path/to/ca.pem') } }
|
|
53
65
|
*/
|
|
54
66
|
tls?: TLSConfig;
|
|
55
67
|
}
|
|
@@ -71,22 +83,61 @@ export class InfrahubClient {
|
|
|
71
83
|
this.branch = new InfrahubBranchManager(this);
|
|
72
84
|
|
|
73
85
|
// Create custom fetch with TLS options if provided
|
|
74
|
-
|
|
86
|
+
// Also handle Request objects properly for node-fetch v2 compatibility
|
|
87
|
+
let httpsAgent: https.Agent | undefined;
|
|
75
88
|
if (options.tls) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
}
|
|
89
|
+
// Build agent options, ensuring rejectUnauthorized is explicitly set if provided
|
|
90
|
+
const agentOptions: https.AgentOptions = {};
|
|
91
|
+
|
|
92
|
+
if (options.tls.rejectUnauthorized !== undefined) {
|
|
93
|
+
agentOptions.rejectUnauthorized = options.tls.rejectUnauthorized;
|
|
94
|
+
}
|
|
95
|
+
if (options.tls.ca !== undefined) {
|
|
96
|
+
agentOptions.ca = options.tls.ca;
|
|
97
|
+
}
|
|
98
|
+
if (options.tls.cert !== undefined) {
|
|
99
|
+
agentOptions.cert = options.tls.cert;
|
|
100
|
+
}
|
|
101
|
+
if (options.tls.key !== undefined) {
|
|
102
|
+
agentOptions.key = options.tls.key;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
httpsAgent = new https.Agent(agentOptions);
|
|
88
106
|
}
|
|
89
107
|
|
|
108
|
+
// Wrap fetch to handle Request objects from openapi-fetch
|
|
109
|
+
// node-fetch v2 doesn't properly extract URL from Request objects
|
|
110
|
+
const customFetch = ((
|
|
111
|
+
input: Parameters<typeof fetch>[0],
|
|
112
|
+
init?: Parameters<typeof fetch>[1]
|
|
113
|
+
) => {
|
|
114
|
+
let url: string;
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
116
|
+
let options: any = init || {};
|
|
117
|
+
|
|
118
|
+
// Handle Request objects by extracting URL and merging options
|
|
119
|
+
if (input && typeof input === 'object' && 'url' in input) {
|
|
120
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
121
|
+
const req = input as any;
|
|
122
|
+
url = req.url;
|
|
123
|
+
options = {
|
|
124
|
+
method: req.method,
|
|
125
|
+
headers: req.headers,
|
|
126
|
+
body: req.body,
|
|
127
|
+
...init
|
|
128
|
+
};
|
|
129
|
+
} else {
|
|
130
|
+
url = input as string;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Add HTTPS agent if configured
|
|
134
|
+
if (httpsAgent) {
|
|
135
|
+
options = { ...options, agent: httpsAgent };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return fetch(url, options);
|
|
139
|
+
}) as typeof fetch;
|
|
140
|
+
|
|
90
141
|
// Initialize the openapi-fetch client with TLS configuration
|
|
91
142
|
this.rest = createClient<paths>({
|
|
92
143
|
baseUrl: this.baseUrl,
|
|
@@ -194,6 +245,50 @@ export class InfrahubClient {
|
|
|
194
245
|
});
|
|
195
246
|
return await (this.graphqlClient.request as any)(query, variables);
|
|
196
247
|
} catch (error) {
|
|
248
|
+
// Check if this is a certificate error and provide helpful guidance
|
|
249
|
+
if (error instanceof Error) {
|
|
250
|
+
const errorMessage = error.message.toLowerCase();
|
|
251
|
+
const isCertError =
|
|
252
|
+
errorMessage.includes('certificate') ||
|
|
253
|
+
errorMessage.includes('cert') ||
|
|
254
|
+
errorMessage.includes('ssl') ||
|
|
255
|
+
errorMessage.includes('tls');
|
|
256
|
+
const isExpired = errorMessage.includes('expired');
|
|
257
|
+
const isSelfSigned = errorMessage.includes('self-signed') || errorMessage.includes('self signed');
|
|
258
|
+
const isUnauthorized = errorMessage.includes('unauthorized');
|
|
259
|
+
|
|
260
|
+
if (isCertError && (isExpired || isSelfSigned || isUnauthorized)) {
|
|
261
|
+
const helpMessage = [
|
|
262
|
+
'',
|
|
263
|
+
'Certificate validation failed. To resolve this issue, you can configure TLS options:',
|
|
264
|
+
'',
|
|
265
|
+
'1. Disable certificate verification (for development/testing only):',
|
|
266
|
+
' const client = new InfrahubClient({',
|
|
267
|
+
' address: "your-address",',
|
|
268
|
+
' token: "your-token",',
|
|
269
|
+
' tls: { rejectUnauthorized: false }',
|
|
270
|
+
' });',
|
|
271
|
+
'',
|
|
272
|
+
'2. Or provide a custom CA certificate (requires: import fs from "fs"):',
|
|
273
|
+
' const client = new InfrahubClient({',
|
|
274
|
+
' address: "your-address",',
|
|
275
|
+
' token: "your-token",',
|
|
276
|
+
' tls: { ca: fs.readFileSync("/path/to/ca-cert.pem") }',
|
|
277
|
+
' });',
|
|
278
|
+
'',
|
|
279
|
+
'See the README.md for more TLS configuration options.',
|
|
280
|
+
''
|
|
281
|
+
].join('\n');
|
|
282
|
+
|
|
283
|
+
console.error(helpMessage);
|
|
284
|
+
|
|
285
|
+
// Create a new error with helpful message appended
|
|
286
|
+
const enhancedError = new Error(`${error.message}\n${helpMessage}`);
|
|
287
|
+
enhancedError.stack = error.stack;
|
|
288
|
+
throw enhancedError;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
197
292
|
console.error('Error executing GraphQL query:', error);
|
|
198
293
|
throw error;
|
|
199
294
|
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration tests for the Infrahub SDK
|
|
3
|
+
*
|
|
4
|
+
* These tests require a running Infrahub server at http://localhost:8000
|
|
5
|
+
*
|
|
6
|
+
* Run with: npx jest --testPathPattern=integration --resetModules
|
|
7
|
+
* Or run directly: npx ts-node src/integration.test.ts
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { InfrahubClient, InfrahubClientOptions } from './index';
|
|
11
|
+
import { describe, expect, it, beforeAll } from '@jest/globals';
|
|
12
|
+
|
|
13
|
+
const INFRAHUB_URL = process.env.INFRAHUB_URL || 'http://localhost:8000';
|
|
14
|
+
const INFRAHUB_TOKEN = process.env.INFRAHUB_TOKEN || '';
|
|
15
|
+
|
|
16
|
+
describe('Integration Tests - Schema Loading', () => {
|
|
17
|
+
let client: InfrahubClient;
|
|
18
|
+
|
|
19
|
+
beforeAll(() => {
|
|
20
|
+
const options: InfrahubClientOptions = {
|
|
21
|
+
address: INFRAHUB_URL,
|
|
22
|
+
token: INFRAHUB_TOKEN
|
|
23
|
+
};
|
|
24
|
+
client = new InfrahubClient(options);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should load schema from the server', async () => {
|
|
28
|
+
const result = await client.rest.GET('/api/schema');
|
|
29
|
+
|
|
30
|
+
expect(result.error).toBeUndefined();
|
|
31
|
+
expect(result.response?.status).toBe(200);
|
|
32
|
+
expect(result.data).toBeDefined();
|
|
33
|
+
|
|
34
|
+
// Schema should have nodes and/or generics arrays
|
|
35
|
+
if (result.data) {
|
|
36
|
+
expect(typeof result.data).toBe('object');
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should load schema summary from the server', async () => {
|
|
41
|
+
const result = await client.rest.GET('/api/schema/summary');
|
|
42
|
+
|
|
43
|
+
expect(result.error).toBeUndefined();
|
|
44
|
+
expect(result.response?.status).toBe(200);
|
|
45
|
+
expect(result.data).toBeDefined();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should get server info', async () => {
|
|
49
|
+
const result = await client.rest.GET('/api/info');
|
|
50
|
+
|
|
51
|
+
expect(result.error).toBeUndefined();
|
|
52
|
+
expect(result.response?.status).toBe(200);
|
|
53
|
+
expect(result.data).toBeDefined();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should get server config', async () => {
|
|
57
|
+
const result = await client.rest.GET('/api/config');
|
|
58
|
+
|
|
59
|
+
expect(result.error).toBeUndefined();
|
|
60
|
+
expect(result.response?.status).toBe(200);
|
|
61
|
+
expect(result.data).toBeDefined();
|
|
62
|
+
});
|
|
63
|
+
});
|