-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Stack Connectors][Microsoft Defender] Add caching of OAuth access token to connector #206975
Merged
paul-tavares
merged 8 commits into
elastic:main
from
paul-tavares:task/olm-ms-connector-cache-access-token
Jan 21, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5624358
Initial set of changes in support of access token caching
paul-tavares 8c44123
caches the token from MS and reuses it
paul-tavares ed659f3
Change expired token threshold to 2 seconds
paul-tavares e213b4e
Updated mocks and delete tests for token from connector test file
paul-tavares 9718855
Tests for oAuth token manager
paul-tavares 5fdbb2b
Merge branch 'main' into task/olm-ms-connector-cache-access-token
paul-tavares ab30e89
Merge branch 'main' into task/olm-ms-connector-cache-access-token
paul-tavares 40f5f27
Merge branch 'main' into task/olm-ms-connector-cache-access-token
paul-tavares File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...onnectors/server/connector_types/microsoft_defender_endpoint/o_auth_token_manager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { | ||
CreateMicrosoftDefenderConnectorMockResponse, | ||
microsoftDefenderEndpointConnectorMocks, | ||
} from './mocks'; | ||
import { OAuthTokenManager } from './o_auth_token_manager'; | ||
import { ConnectorTokenClient } from '@kbn/actions-plugin/server/lib/connector_token_client'; | ||
|
||
describe('Microsoft Defender for Endpoint oAuth token manager', () => { | ||
let testMock: CreateMicrosoftDefenderConnectorMockResponse; | ||
let msOAuthManagerMock: OAuthTokenManager; | ||
let connectorTokenManagerClientMock: jest.Mocked<ConnectorTokenClient>; | ||
|
||
beforeEach(() => { | ||
testMock = microsoftDefenderEndpointConnectorMocks.create(); | ||
connectorTokenManagerClientMock = testMock.options.services | ||
.connectorTokenClient as jest.Mocked<ConnectorTokenClient>; | ||
msOAuthManagerMock = new OAuthTokenManager({ | ||
...testMock.options, | ||
apiRequest: async (...args) => testMock.instanceMock.request(...args), | ||
}); | ||
}); | ||
|
||
it('should call MS api to generate new token', async () => { | ||
await msOAuthManagerMock.get(testMock.usageCollector); | ||
|
||
expect(testMock.instanceMock.request).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
url: `${testMock.options.config.oAuthServerUrl}/${testMock.options.config.tenantId}/oauth2/v2.0/token`, | ||
method: 'POST', | ||
data: { | ||
grant_type: 'client_credentials', | ||
client_id: testMock.options.config.clientId, | ||
scope: testMock.options.config.oAuthScope, | ||
client_secret: testMock.options.secrets.clientSecret, | ||
}, | ||
}), | ||
testMock.usageCollector | ||
); | ||
}); | ||
|
||
it('should use cached token when one exists', async () => { | ||
const { | ||
connectorId, | ||
token, | ||
expiresAt: expiresAtMillis, | ||
tokenType, | ||
} = microsoftDefenderEndpointConnectorMocks.createConnectorToken(); | ||
await connectorTokenManagerClientMock.create({ | ||
connectorId, | ||
token, | ||
expiresAtMillis, | ||
tokenType, | ||
}); | ||
await msOAuthManagerMock.get(testMock.usageCollector); | ||
|
||
expect(testMock.instanceMock.request).not.toHaveBeenCalled(); | ||
expect(connectorTokenManagerClientMock.get).toHaveBeenCalledWith({ | ||
connectorId: '1', | ||
tokenType: 'access_token', | ||
}); | ||
}); | ||
|
||
it('should call MS API to generate new token when the cached token is expired', async () => { | ||
const { connectorId, token, tokenType } = | ||
microsoftDefenderEndpointConnectorMocks.createConnectorToken(); | ||
await connectorTokenManagerClientMock.create({ | ||
connectorId, | ||
token, | ||
expiresAtMillis: '2024-01-16T13:02:43.494Z', | ||
tokenType, | ||
}); | ||
await msOAuthManagerMock.get(testMock.usageCollector); | ||
|
||
expect(connectorTokenManagerClientMock.get).toHaveBeenCalledWith({ | ||
connectorId: '1', | ||
tokenType: 'access_token', | ||
}); | ||
expect(testMock.instanceMock.request).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
url: `${testMock.options.config.oAuthServerUrl}/${testMock.options.config.tenantId}/oauth2/v2.0/token`, | ||
}), | ||
testMock.usageCollector | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this
services.connectorTokenClient
a functionality exposed by response ops team, or you created that in a separate PR before?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is exposed by the ResponseOps framework. Its purpose is to only store tokens so they can be reused across instances of kibana.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I'll try to rebuild the token cache in CS to use this one 👍