Skip to content

Commit 68e7793

Browse files
committed
feat: add support for self-signed SSL certificates
1 parent 4ca8603 commit 68e7793

File tree

4 files changed

+74
-15
lines changed

4 files changed

+74
-15
lines changed

README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -746,17 +746,18 @@ Holds the information related to an event parsing error. This class attempts to
746746
747747
Check out the [authentication](#authentication) section for more information on how to provide the right values.
748748
749-
| Name | Type | Description |
750-
| ---------------- | ------ | ------------------------------------------------------------------------------------------------------------------------- |
751-
| `authType` | string | Authentication type. One of `user-supplied`, `username-password`, `oauth-client-credentials` or `oauth-jwt-bearer`. |
752-
| `pubSubEndpoint` | string | A custom Pub/Sub API endpoint. The default endpoint `api.pubsub.salesforce.com:7443` is used if none is supplied. |
753-
| `accessToken` | string | Salesforce access token. |
754-
| `instanceUrl` | string | Salesforce instance URL. |
755-
| `organizationId` | string | Optional organization ID. If you don't provide one, we'll attempt to parse it from the accessToken. |
756-
| `loginUrl` | string | Salesforce login host. One of `https://login.salesforce.com`, `https://test.salesforce.com` or your domain specific host. |
757-
| `clientId` | string | Connected app client ID. |
758-
| `clientSecret` | string | Connected app client secret. |
759-
| `privateKey` | string | Private key content. |
760-
| `username` | string | Salesforce username. |
761-
| `password` | string | Salesforce user password. |
762-
| `userToken` | string | Salesforce user security token. |
749+
| Name | Type | Description |
750+
| ----------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
751+
| `authType` | string | Authentication type. One of `user-supplied`, `username-password`, `oauth-client-credentials` or `oauth-jwt-bearer`. |
752+
| `pubSubEndpoint` | string | A custom Pub/Sub API endpoint. The default endpoint `api.pubsub.salesforce.com:7443` is used if none is supplied. |
753+
| `accessToken` | string | Salesforce access token. |
754+
| `instanceUrl` | string | Salesforce instance URL. |
755+
| `organizationId` | string | Optional organization ID. If you don't provide one, we'll attempt to parse it from the accessToken. |
756+
| `loginUrl` | string | Salesforce login host. One of `https://login.salesforce.com`, `https://test.salesforce.com` or your domain specific host. |
757+
| `clientId` | string | Connected app client ID. |
758+
| `clientSecret` | string | Connected app client secret. |
759+
| `privateKey` | string | Private key content. |
760+
| `username` | string | Salesforce username. |
761+
| `password` | string | Salesforce user password. |
762+
| `userToken` | string | Salesforce user security token. |
763+
| `rejectUnauthorizedSsl` | boolean | Optional flag used to accept self-signed SSL certificates for testing purposes when set to `false`. Default is `true` (client rejects self-signed SSL certificates). |

src/client.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ export default class PubSubApiClient {
167167
const callCreds =
168168
grpc.credentials.createFromMetadataGenerator(metaCallback);
169169
const combCreds = grpc.credentials.combineChannelCredentials(
170-
grpc.credentials.createSsl(rootCert),
170+
grpc.credentials.createSsl(rootCert, null, null, {
171+
rejectUnauthorized: this.#config.rejectUnauthorizedSsl
172+
}),
171173
callCreds
172174
);
173175

src/utils/configurationLoader.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AuthType } from './types.js';
22

33
const DEFAULT_PUB_SUB_ENDPOINT = 'api.pubsub.salesforce.com:7443';
4+
const DEFAULT_REJECT_UNAUTHORIZED_SSL = true;
45

56
export default class ConfigurationLoader {
67
/**
@@ -45,9 +46,63 @@ export default class ConfigurationLoader {
4546
`Unsupported authType value: ${config.authType}`
4647
);
4748
}
49+
// Sanitize rejectUnauthorizedSsl property
50+
ConfigurationLoader.#loadBooleanValue(
51+
config,
52+
'rejectUnauthorizedSsl',
53+
DEFAULT_REJECT_UNAUTHORIZED_SSL
54+
);
4855
return config;
4956
}
5057

58+
/**
59+
* Loads a boolean value from a config key.
60+
* Falls back to the provided default value if no value is specified.
61+
* Errors out if the config value can't be converted to a boolean value.
62+
* @param {Configuration} config
63+
* @param {string} key
64+
* @param {boolean} defaultValue
65+
*/
66+
static #loadBooleanValue(config, key, defaultValue) {
67+
// Load the default value if no value is specified
68+
if (
69+
!Object.hasOwn(config, key) ||
70+
config[key] === undefined ||
71+
config[key] === null
72+
) {
73+
config[key] = defaultValue;
74+
return;
75+
}
76+
77+
const value = config[key];
78+
const type = typeof value;
79+
switch (type) {
80+
case 'boolean':
81+
// Do nothing, value is valid
82+
break;
83+
case 'string':
84+
{
85+
switch (value.toUppercase()) {
86+
case 'TRUE':
87+
config[key] = true;
88+
break;
89+
case 'FALSE':
90+
config[key] = false;
91+
break;
92+
default:
93+
throw new Error(
94+
`Expected boolean value for ${key}, found ${type} with value ${value}`
95+
);
96+
}
97+
}
98+
break;
99+
default:
100+
throw new Error(
101+
`Expected boolean value for ${key}, found ${type} with value ${value}`
102+
);
103+
}
104+
}
105+
51106
/**
52107
* @param {Configuration} config the client configuration
53108
* @returns {Configuration} sanitized configuration

src/utils/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export const EventSubscriptionAdminState = {
122122
* @property {string} [accessToken] Salesforce access token.
123123
* @property {string} [instanceUrl] Salesforce instance URL.
124124
* @property {string} [organizationId] Optional organization ID. If you don't provide one, we'll attempt to parse it from the accessToken.
125+
* @property {boolean} [rejectUnauthorizedSsl] Optional flag used to accept self-signed SSL certificates for testing purposes when set to `false`. Default is `true` (client rejects self-signed SSL certificates).
125126
*/
126127

127128
/**

0 commit comments

Comments
 (0)