Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
fix Wrong expiry timer in OAuthProvider.ts #336
Browse files Browse the repository at this point in the history
  • Loading branch information
jwulf committed Oct 24, 2023
1 parent 64d0e9f commit 38abf47
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ _Changes in APIs or behaviour that may affect existing applications that use zee

_New shiny stuff._

- Camunda Platform 8.3.0 introduces multi-tenancy. To support this, the Node.js client adds an optional `tenantId` parameter to `DeployResource`, `DeployProcess`, `CreateProcessInstance`, `CreateProcessInstanceWithResult`, and `PublishMessage`. You can also specify a `tenantId` in the ZBClient constructor or via the environment variable `ZEEBE_TENANT_ID`. This will be transparently added to all method invocations. See [#330](https://github.com/camunda-community-hub/zeebe-client-node-js/issues/330) for more details.
- Camunda Platform 8.3.0 introduces multi-tenancy. To support this, the Node.js client adds an optional `tenantId` parameter to `DeployResource`, `DeployProcess`, `CreateProcessInstance`, `CreateProcessInstanceWithResult`, and `PublishMessage`. You can also specify a `tenantId` in the ZBClient constructor or via the environment variable `ZEEBE_TENANT_ID`. In the case that you specify it via the environment or constructor, it will be transparently added to all method invocations. See [#330](https://github.com/camunda-community-hub/zeebe-client-node-js/issues/330) for more details.

## Fixes

_Things that were broken and are now fixed._

- An error message "Grpc Stream Error: 16 UNAUTHENTICATED: Failed to parse bearer token, see cause for details" would be logged intermittently. This was because under particular conditions an expired token cached on disk could be used for API calls. To prevent this, the disk-cached token is evicted at the same time as the in-memory token. See [#336](https://github.com/camunda-community-hub/zeebe-client-node-js/issues/336) for more details.

# Version 8.2.5

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zeebe-node",
"version": "8.3.0-alpha7",
"version": "8.3.0-alpha9",
"description": "The Node.js client library for the Zeebe Workflow Automation Engine.",
"keywords": [
"zeebe",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/OAuthProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ test('In-memory cache is populated and evicted after timeout', done => {

req.on('end', () => {
res.writeHead(200, { 'Content-Type': 'application/json' })
let expires_in = 2
let expires_in = 2 // seconds
res.end(
'{"access_token": "something", "expires_in": ' +
expires_in +
Expand Down
15 changes: 13 additions & 2 deletions src/lib/OAuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,19 @@ export class OAuthProvider {
delete this.tokenCache[this.clientId]
return
}
this.expiryTimer = setTimeout(() => delete this.tokenCache[this.clientId], validityPeriod)
trace(`${this.uuid} start`)
// renew token 1s before it expires to avoid race conditions on the wire
// evict disk cache at same time as in-memory cache
// See: https://github.com/camunda-community-hub/zeebe-client-node-js/issues/336
const minimumCacheLifetime = 0; // Minimum cache lifetime in milliseconds
const renewTokenAfterMs = Math.max(validityPeriod - 1000, minimumCacheLifetime)
this.expiryTimer = setTimeout(() => {
trace(`${this.uuid} token expired`)
delete this.tokenCache[this.clientId]
if (this.useFileCache && fs.existsSync(this.cachedTokenFile(this.clientId))) {
fs.unlinkSync(this.cachedTokenFile(this.clientId))
}
}, renewTokenAfterMs)
trace(`${this.uuid} token expiry timer start: ${renewTokenAfterMs}ms`)
}

private cachedTokenFile = (clientId: string) =>
Expand Down

0 comments on commit 38abf47

Please sign in to comment.