infrahub-sdk 0.0.7 → 0.0.8
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 +58 -7
- package/dist/index.test.js +60 -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 +79 -8
package/README.md
CHANGED
|
@@ -39,11 +39,25 @@ const usersData = await client.executeGraphQL(listDevices);
|
|
|
39
39
|
|
|
40
40
|
## TLS/SSL Configuration
|
|
41
41
|
|
|
42
|
-
When connecting to Infrahub instances with custom
|
|
42
|
+
When connecting to Infrahub instances with custom, self-signed, or expired certificates, you can configure TLS options:
|
|
43
|
+
|
|
44
|
+
### Handling Certificate Errors
|
|
45
|
+
|
|
46
|
+
If you encounter certificate errors such as:
|
|
47
|
+
- `certificate has expired`
|
|
48
|
+
- `self-signed certificate`
|
|
49
|
+
- `unable to verify the first certificate`
|
|
50
|
+
- `UNABLE_TO_VERIFY_LEAF_SIGNATURE`
|
|
51
|
+
|
|
52
|
+
You can resolve these by configuring the TLS options as shown below.
|
|
43
53
|
|
|
44
54
|
### Disable Certificate Verification (for development/testing)
|
|
45
55
|
|
|
56
|
+
This is the recommended approach for handling expired certificates in development/testing environments:
|
|
57
|
+
|
|
46
58
|
```typescript
|
|
59
|
+
import { InfrahubClient, InfrahubClientOptions } from 'infrahub-sdk';
|
|
60
|
+
|
|
47
61
|
const options: InfrahubClientOptions = {
|
|
48
62
|
address: "https://infrahub.example.com",
|
|
49
63
|
token: "your-api-token",
|
|
@@ -55,7 +69,22 @@ const options: InfrahubClientOptions = {
|
|
|
55
69
|
const client = new InfrahubClient(options);
|
|
56
70
|
```
|
|
57
71
|
|
|
58
|
-
**
|
|
72
|
+
**Important Notes:**
|
|
73
|
+
- Setting `rejectUnauthorized: false` disables ALL certificate validation
|
|
74
|
+
- This bypasses checks for expired, self-signed, and invalid certificates
|
|
75
|
+
- **Warning**: Never use this in production environments as it makes your connection vulnerable to man-in-the-middle attacks
|
|
76
|
+
- Only use this for development, testing, or when connecting to trusted internal servers
|
|
77
|
+
|
|
78
|
+
**Example:** See [examples/expired-certificate-example.ts](examples/expired-certificate-example.ts) for a complete working example.
|
|
79
|
+
|
|
80
|
+
### Troubleshooting
|
|
81
|
+
|
|
82
|
+
If you're still experiencing certificate errors after setting `rejectUnauthorized: false`:
|
|
83
|
+
|
|
84
|
+
1. **Verify the configuration is being applied**: Make sure you're creating the client with the TLS options
|
|
85
|
+
2. **Check for multiple client instances**: Ensure all instances use the same TLS configuration
|
|
86
|
+
3. **Network/Proxy Issues**: If you're behind a proxy, you may need additional configuration
|
|
87
|
+
4. **Node.js Version**: Ensure you're using Node.js 14 or later
|
|
59
88
|
|
|
60
89
|
### Use Custom CA Certificate
|
|
61
90
|
|
package/dist/index.d.ts
CHANGED
|
@@ -17,10 +17,14 @@ export interface TLSConfig {
|
|
|
17
17
|
/**
|
|
18
18
|
* If true, the server certificate is verified against the list of supplied CAs.
|
|
19
19
|
* Set to false to disable certificate verification (not recommended for production).
|
|
20
|
+
*
|
|
21
|
+
* Note: Setting this to false will bypass all certificate validation, including
|
|
22
|
+
* expired certificates, self-signed certificates, and hostname mismatches.
|
|
20
23
|
*/
|
|
21
24
|
rejectUnauthorized?: boolean;
|
|
22
25
|
/**
|
|
23
26
|
* Optional CA certificates to trust. Can be a string or Buffer containing the certificate(s).
|
|
27
|
+
* Use this when connecting to servers with custom or self-signed certificates.
|
|
24
28
|
*/
|
|
25
29
|
ca?: string | Buffer | Array<string | Buffer>;
|
|
26
30
|
/**
|
|
@@ -38,7 +42,15 @@ export interface InfrahubClientOptions {
|
|
|
38
42
|
branch?: string;
|
|
39
43
|
/**
|
|
40
44
|
* TLS/SSL configuration options for HTTPS connections.
|
|
41
|
-
* Use this to handle custom certificates or disable certificate verification.
|
|
45
|
+
* Use this to handle custom certificates, expired certificates, or disable certificate verification.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* // Disable certificate verification (development only)
|
|
49
|
+
* { tls: { rejectUnauthorized: false } }
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* // Use custom CA certificate
|
|
53
|
+
* { tls: { ca: fs.readFileSync('/path/to/ca.pem') } }
|
|
42
54
|
*/
|
|
43
55
|
tls?: TLSConfig;
|
|
44
56
|
}
|
package/dist/index.js
CHANGED
|
@@ -37,14 +37,26 @@ class InfrahubClient {
|
|
|
37
37
|
// Create custom fetch with TLS options if provided
|
|
38
38
|
let customFetch = node_fetch_1.default;
|
|
39
39
|
if (options.tls) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
40
|
+
// Build agent options, ensuring rejectUnauthorized is explicitly set if provided
|
|
41
|
+
const agentOptions = {};
|
|
42
|
+
if (options.tls.rejectUnauthorized !== undefined) {
|
|
43
|
+
agentOptions.rejectUnauthorized = options.tls.rejectUnauthorized;
|
|
44
|
+
}
|
|
45
|
+
if (options.tls.ca !== undefined) {
|
|
46
|
+
agentOptions.ca = options.tls.ca;
|
|
47
|
+
}
|
|
48
|
+
if (options.tls.cert !== undefined) {
|
|
49
|
+
agentOptions.cert = options.tls.cert;
|
|
50
|
+
}
|
|
51
|
+
if (options.tls.key !== undefined) {
|
|
52
|
+
agentOptions.key = options.tls.key;
|
|
53
|
+
}
|
|
54
|
+
const httpsAgent = new https_1.default.Agent(agentOptions);
|
|
46
55
|
customFetch = ((url, opts = {}) => {
|
|
47
|
-
|
|
56
|
+
// Ensure agent is passed for HTTPS URLs
|
|
57
|
+
// node-fetch v2 requires the agent to be explicitly set
|
|
58
|
+
const fetchOptions = { ...opts, agent: httpsAgent };
|
|
59
|
+
return (0, node_fetch_1.default)(url, fetchOptions);
|
|
48
60
|
});
|
|
49
61
|
}
|
|
50
62
|
// Initialize the openapi-fetch client with TLS configuration
|
|
@@ -128,6 +140,45 @@ class InfrahubClient {
|
|
|
128
140
|
return await this.graphqlClient.request(query, variables);
|
|
129
141
|
}
|
|
130
142
|
catch (error) {
|
|
143
|
+
// Check if this is a certificate error and provide helpful guidance
|
|
144
|
+
if (error instanceof Error) {
|
|
145
|
+
const errorMessage = error.message.toLowerCase();
|
|
146
|
+
const isCertError = errorMessage.includes('certificate') ||
|
|
147
|
+
errorMessage.includes('cert') ||
|
|
148
|
+
errorMessage.includes('ssl') ||
|
|
149
|
+
errorMessage.includes('tls');
|
|
150
|
+
const isExpired = errorMessage.includes('expired');
|
|
151
|
+
const isSelfSigned = errorMessage.includes('self-signed') || errorMessage.includes('self signed');
|
|
152
|
+
const isUnauthorized = errorMessage.includes('unauthorized');
|
|
153
|
+
if (isCertError && (isExpired || isSelfSigned || isUnauthorized)) {
|
|
154
|
+
const helpMessage = [
|
|
155
|
+
'',
|
|
156
|
+
'Certificate validation failed. To resolve this issue, you can configure TLS options:',
|
|
157
|
+
'',
|
|
158
|
+
'1. Disable certificate verification (for development/testing only):',
|
|
159
|
+
' const client = new InfrahubClient({',
|
|
160
|
+
' address: "your-address",',
|
|
161
|
+
' token: "your-token",',
|
|
162
|
+
' tls: { rejectUnauthorized: false }',
|
|
163
|
+
' });',
|
|
164
|
+
'',
|
|
165
|
+
'2. Or provide a custom CA certificate (requires: import fs from "fs"):',
|
|
166
|
+
' const client = new InfrahubClient({',
|
|
167
|
+
' address: "your-address",',
|
|
168
|
+
' token: "your-token",',
|
|
169
|
+
' tls: { ca: fs.readFileSync("/path/to/ca-cert.pem") }',
|
|
170
|
+
' });',
|
|
171
|
+
'',
|
|
172
|
+
'See the README.md for more TLS configuration options.',
|
|
173
|
+
''
|
|
174
|
+
].join('\n');
|
|
175
|
+
console.error(helpMessage);
|
|
176
|
+
// Create a new error with helpful message appended
|
|
177
|
+
const enhancedError = new Error(`${error.message}\n${helpMessage}`);
|
|
178
|
+
enhancedError.stack = error.stack;
|
|
179
|
+
throw enhancedError;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
131
182
|
console.error('Error executing GraphQL query:', error);
|
|
132
183
|
throw error;
|
|
133
184
|
}
|
package/dist/index.test.js
CHANGED
|
@@ -38,6 +38,50 @@ const globals_1 = require("@jest/globals");
|
|
|
38
38
|
await (0, globals_1.expect)(client.executeGraphQL('{ test }')).rejects.toThrow('GraphQL error');
|
|
39
39
|
consoleErrorSpy.mockRestore();
|
|
40
40
|
});
|
|
41
|
+
(0, globals_1.it)('should provide helpful error message for certificate errors', async () => {
|
|
42
|
+
const mockRequest = globals_1.jest
|
|
43
|
+
.fn()
|
|
44
|
+
.mockImplementation(() => Promise.reject(new Error('request failed, reason: certificate has expired')));
|
|
45
|
+
client.graphqlClient.request = mockRequest;
|
|
46
|
+
const consoleErrorSpy = globals_1.jest
|
|
47
|
+
.spyOn(console, 'error')
|
|
48
|
+
.mockImplementation(() => { });
|
|
49
|
+
try {
|
|
50
|
+
await client.executeGraphQL('{ test }');
|
|
51
|
+
(0, globals_1.expect)(true).toBe(false); // Should not reach here
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
(0, globals_1.expect)(error).toBeInstanceOf(Error);
|
|
55
|
+
if (error instanceof Error) {
|
|
56
|
+
(0, globals_1.expect)(error.message).toContain('certificate has expired');
|
|
57
|
+
(0, globals_1.expect)(error.message).toContain('Certificate validation failed');
|
|
58
|
+
(0, globals_1.expect)(error.message).toContain('rejectUnauthorized: false');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
consoleErrorSpy.mockRestore();
|
|
62
|
+
});
|
|
63
|
+
(0, globals_1.it)('should provide helpful error message for self-signed certificate errors', async () => {
|
|
64
|
+
const mockRequest = globals_1.jest
|
|
65
|
+
.fn()
|
|
66
|
+
.mockImplementation(() => Promise.reject(new Error('request failed, reason: self-signed certificate')));
|
|
67
|
+
client.graphqlClient.request = mockRequest;
|
|
68
|
+
const consoleErrorSpy = globals_1.jest
|
|
69
|
+
.spyOn(console, 'error')
|
|
70
|
+
.mockImplementation(() => { });
|
|
71
|
+
try {
|
|
72
|
+
await client.executeGraphQL('{ test }');
|
|
73
|
+
(0, globals_1.expect)(true).toBe(false); // Should not reach here
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
(0, globals_1.expect)(error).toBeInstanceOf(Error);
|
|
77
|
+
if (error instanceof Error) {
|
|
78
|
+
(0, globals_1.expect)(error.message).toContain('self-signed certificate');
|
|
79
|
+
(0, globals_1.expect)(error.message).toContain('Certificate validation failed');
|
|
80
|
+
(0, globals_1.expect)(error.message).toContain('tls: { rejectUnauthorized: false }');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
consoleErrorSpy.mockRestore();
|
|
84
|
+
});
|
|
41
85
|
(0, globals_1.it)('should initialize rest client with baseUrl', () => {
|
|
42
86
|
(0, globals_1.expect)(client.rest).toBeDefined();
|
|
43
87
|
});
|
|
@@ -72,6 +116,22 @@ const globals_1 = require("@jest/globals");
|
|
|
72
116
|
(0, globals_1.expect)(tlsClient).toBeDefined();
|
|
73
117
|
(0, globals_1.expect)(tlsClient.graphqlClient).toBeInstanceOf(graphql_request_1.GraphQLClient);
|
|
74
118
|
});
|
|
119
|
+
(0, globals_1.it)('should create agent with rejectUnauthorized: false when explicitly set', () => {
|
|
120
|
+
const https = require('https');
|
|
121
|
+
const createAgentSpy = globals_1.jest.spyOn(https, 'Agent');
|
|
122
|
+
const options = {
|
|
123
|
+
address: baseURL,
|
|
124
|
+
token: token,
|
|
125
|
+
tls: {
|
|
126
|
+
rejectUnauthorized: false
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const tlsClient = new index_1.InfrahubClient(options);
|
|
130
|
+
(0, globals_1.expect)(createAgentSpy).toHaveBeenCalled();
|
|
131
|
+
const agentOptions = createAgentSpy.mock.calls[createAgentSpy.mock.calls.length - 1][0];
|
|
132
|
+
(0, globals_1.expect)(agentOptions.rejectUnauthorized).toBe(false);
|
|
133
|
+
createAgentSpy.mockRestore();
|
|
134
|
+
});
|
|
75
135
|
(0, globals_1.it)('should accept TLS configuration with custom CA certificate', () => {
|
|
76
136
|
const options = {
|
|
77
137
|
address: baseURL,
|
|
@@ -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
|
}
|
|
@@ -73,17 +85,32 @@ export class InfrahubClient {
|
|
|
73
85
|
// Create custom fetch with TLS options if provided
|
|
74
86
|
let customFetch: typeof fetch = fetch;
|
|
75
87
|
if (options.tls) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
88
|
+
// Build agent options, ensuring rejectUnauthorized is explicitly set if provided
|
|
89
|
+
const agentOptions: https.AgentOptions = {};
|
|
90
|
+
|
|
91
|
+
if (options.tls.rejectUnauthorized !== undefined) {
|
|
92
|
+
agentOptions.rejectUnauthorized = options.tls.rejectUnauthorized;
|
|
93
|
+
}
|
|
94
|
+
if (options.tls.ca !== undefined) {
|
|
95
|
+
agentOptions.ca = options.tls.ca;
|
|
96
|
+
}
|
|
97
|
+
if (options.tls.cert !== undefined) {
|
|
98
|
+
agentOptions.cert = options.tls.cert;
|
|
99
|
+
}
|
|
100
|
+
if (options.tls.key !== undefined) {
|
|
101
|
+
agentOptions.key = options.tls.key;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const httpsAgent = new https.Agent(agentOptions);
|
|
105
|
+
|
|
82
106
|
customFetch = ((
|
|
83
107
|
url: Parameters<typeof fetch>[0],
|
|
84
108
|
opts: Parameters<typeof fetch>[1] = {}
|
|
85
109
|
) => {
|
|
86
|
-
|
|
110
|
+
// Ensure agent is passed for HTTPS URLs
|
|
111
|
+
// node-fetch v2 requires the agent to be explicitly set
|
|
112
|
+
const fetchOptions = { ...opts, agent: httpsAgent };
|
|
113
|
+
return fetch(url, fetchOptions);
|
|
87
114
|
}) as typeof fetch;
|
|
88
115
|
}
|
|
89
116
|
|
|
@@ -194,6 +221,50 @@ export class InfrahubClient {
|
|
|
194
221
|
});
|
|
195
222
|
return await (this.graphqlClient.request as any)(query, variables);
|
|
196
223
|
} catch (error) {
|
|
224
|
+
// Check if this is a certificate error and provide helpful guidance
|
|
225
|
+
if (error instanceof Error) {
|
|
226
|
+
const errorMessage = error.message.toLowerCase();
|
|
227
|
+
const isCertError =
|
|
228
|
+
errorMessage.includes('certificate') ||
|
|
229
|
+
errorMessage.includes('cert') ||
|
|
230
|
+
errorMessage.includes('ssl') ||
|
|
231
|
+
errorMessage.includes('tls');
|
|
232
|
+
const isExpired = errorMessage.includes('expired');
|
|
233
|
+
const isSelfSigned = errorMessage.includes('self-signed') || errorMessage.includes('self signed');
|
|
234
|
+
const isUnauthorized = errorMessage.includes('unauthorized');
|
|
235
|
+
|
|
236
|
+
if (isCertError && (isExpired || isSelfSigned || isUnauthorized)) {
|
|
237
|
+
const helpMessage = [
|
|
238
|
+
'',
|
|
239
|
+
'Certificate validation failed. To resolve this issue, you can configure TLS options:',
|
|
240
|
+
'',
|
|
241
|
+
'1. Disable certificate verification (for development/testing only):',
|
|
242
|
+
' const client = new InfrahubClient({',
|
|
243
|
+
' address: "your-address",',
|
|
244
|
+
' token: "your-token",',
|
|
245
|
+
' tls: { rejectUnauthorized: false }',
|
|
246
|
+
' });',
|
|
247
|
+
'',
|
|
248
|
+
'2. Or provide a custom CA certificate (requires: import fs from "fs"):',
|
|
249
|
+
' const client = new InfrahubClient({',
|
|
250
|
+
' address: "your-address",',
|
|
251
|
+
' token: "your-token",',
|
|
252
|
+
' tls: { ca: fs.readFileSync("/path/to/ca-cert.pem") }',
|
|
253
|
+
' });',
|
|
254
|
+
'',
|
|
255
|
+
'See the README.md for more TLS configuration options.',
|
|
256
|
+
''
|
|
257
|
+
].join('\n');
|
|
258
|
+
|
|
259
|
+
console.error(helpMessage);
|
|
260
|
+
|
|
261
|
+
// Create a new error with helpful message appended
|
|
262
|
+
const enhancedError = new Error(`${error.message}\n${helpMessage}`);
|
|
263
|
+
enhancedError.stack = error.stack;
|
|
264
|
+
throw enhancedError;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
197
268
|
console.error('Error executing GraphQL query:', error);
|
|
198
269
|
throw error;
|
|
199
270
|
}
|