Skip to content
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

Invalidate alert API Key when generating a new one #53732

Merged
merged 10 commits into from
Jan 3, 2020

Conversation

mikecote
Copy link
Contributor

@mikecote mikecote commented Dec 20, 2019

Resolves #45144

In this PR, I'm invalidating API keys after generating a new one. This happens within the update, enable, updateApiKey calls as well as after deleting an alert.

I'm also renaming a variable within CreateAPIKeyResult interface to be more clear (created -> apiKeysEnabled). This is to be consistent with the new interface InvalidateAPIKeyResult.

There are some notes worth considering (created an issue to track them #53868):

  • If the underlying alert task is running while the API key is invalidated, the task may throw an error if it needed that key (authentication failure, invalid key, etc)
  • The same applies to actions if they are running while the API key is invalidated
  • If actions are scheduled but not executed before the API key is invalidated, the action will throw an error if it depended on that key
  • If an action is in a retry state and depended on that key, it will keep failing
  • The invalidated API keys still return from the Elasticsearch API Key API. They are returned with invalidated: true as an attribute

@mikecote mikecote added Feature:Alerting v8.0.0 release_note:skip Skip the PR/issue when compiling release notes v7.6.0 Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) labels Dec 20, 2019
@mikecote mikecote self-assigned this Dec 20, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-alerting-services (Team:Alerting Services)

@mikecote
Copy link
Contributor Author

@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

💔 Build Failed

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@elastic elastic deleted a comment from elasticmachine Dec 31, 2019
@kibanamachine
Copy link
Contributor

💔 Build Failed

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@mikecote
Copy link
Contributor Author

mikecote commented Jan 2, 2020

@elasticmachine merge upstream

@mikecote mikecote marked this pull request as ready for review January 2, 2020 13:34
@mikecote mikecote requested a review from a team as a code owner January 2, 2020 13:34
@mikecote mikecote requested a review from a team January 2, 2020 13:34
Copy link
Member

@pmuellr pmuellr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; added a comment/question about whether we need the new namespace param, whether we can just calculate as needed or at initialization based on spaceId

spaceId?: string;
namespace?: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between spaceId and namespace? Looks like spaceId is only ever used in scheduleAlert(). I see some a conversion function used elsewhere in files in the PR - spaceIdToNamespace(), wondering if we should just use spaceIdToNamespace(this.spaceId) instead of adding the namespace parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure myself of the difference but I have been told by the security team that there is a difference. Also that we should use spaceId as our source of truth to convert into namespace and basePath.

We can use the function spaceIdToNamespace to get the value but I would have to pass the spaceIdToNamespace function as an argument to alerts client instead. So I figured I might as well pass the result of the function. I'm indifferent on the approach, thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eh, I worry about two highly related values being stored like that, in case one gets "out of sync" with the other, but I don't think that's really an issue since you can't really change the space of a SO anyway. And having to send more functions in doesn't sound all that great either. Seems like leaving it the way it is, is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hoping eventually we can just pass around the setup and start objects instead of sub-functions / properties. Though, not sure if that's good or bad but would reduce the number of things being passed around drastically.

@gmmorris gmmorris self-requested a review January 3, 2020 14:08
}
return {
created: true,
apiKeysEnabled: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for the rename

const apiKeyId = Buffer.from(apiKey, 'base64')
.toString()
.split(':')[0];
const response = await this.invalidateAPIKey({ id: apiKeyId });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this throws (instead of returning an error_count > 0) then we wo't get the error message that says we failed to invalidate the key. Is it worth catching and logging accordingly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log message will still be there from the security plugin: https://github.com/elastic/kibana/blob/master/x-pack/plugins/security/server/authentication/api_keys.ts#L161.

Though I went ahead and also wrapped it ourselves to swallow the error in that case: 4a4aad9.

Copy link
Contributor

@gmmorris gmmorris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Left a couple of notes about testing for the unhappy path of invalidating keys, but other than that, looks good.

Comment on lines +38 to +41
alertsClientParams.invalidateAPIKey.mockResolvedValue({
apiKeysEnabled: true,
result: { error_count: 0 },
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like we're testing the happy path here, it might be worth adding a couple of tests that checks what happens when these api calls fail (throw an error, etc)

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@mikecote mikecote merged commit 8cc778a into elastic:master Jan 3, 2020
mikecote added a commit to mikecote/kibana that referenced this pull request Jan 3, 2020
* Initial work to auto cleanup old API keys

* Fix ESLint error

* Rename confusing variables

* Add test to ensure thrown errors are swallowed

* Add more tests

Co-authored-by: Elastic Machine <[email protected]>
mikecote added a commit that referenced this pull request Jan 3, 2020
* Initial work to auto cleanup old API keys

* Fix ESLint error

* Rename confusing variables

* Add test to ensure thrown errors are swallowed

* Add more tests

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
gmmorris added a commit to gmmorris/kibana that referenced this pull request Jan 6, 2020
* master:
  increase delay to make sure license refetched (elastic#53882)
  Allow custom NP plugin paths in production (elastic#53562)
  [Maps] show custom color ramps in legend (elastic#53780)
  [Lens] Expression type on document can be null (elastic#53883)
  [SIEM] [Detection engine] Add user permission to detection engine (elastic#53778)
  Update dependency @elastic/charts to v16.0.2 (elastic#52619)
  Set consistent EOL symbol in core API docs (elastic#53815)
  [Logs UI] Refactor query bar state to hooks (elastic#52656)
  [Maps] pass getFieldFormatter to DynamicTextProperty (elastic#53937)
  Invalidate alert API Key when generating a new one (elastic#53732)
  [Logs UI] HTTP API for log entries (elastic#53798)
gmmorris added a commit to gmmorris/kibana that referenced this pull request Jan 6, 2020
* master:
  increase delay to make sure license refetched (elastic#53882)
  Allow custom NP plugin paths in production (elastic#53562)
  [Maps] show custom color ramps in legend (elastic#53780)
  [Lens] Expression type on document can be null (elastic#53883)
  [SIEM] [Detection engine] Add user permission to detection engine (elastic#53778)
  Update dependency @elastic/charts to v16.0.2 (elastic#52619)
  Set consistent EOL symbol in core API docs (elastic#53815)
  [Logs UI] Refactor query bar state to hooks (elastic#52656)
  [Maps] pass getFieldFormatter to DynamicTextProperty (elastic#53937)
  Invalidate alert API Key when generating a new one (elastic#53732)
  [Logs UI] HTTP API for log entries (elastic#53798)
  [kbn/pm] add caching to bootstrap (elastic#53622)
  adds createdAt and updatedAt fields to alerting (elastic#53793)
gmmorris added a commit to gmmorris/kibana that referenced this pull request Jan 6, 2020
* master:
  increase delay to make sure license refetched (elastic#53882)
  Allow custom NP plugin paths in production (elastic#53562)
  [Maps] show custom color ramps in legend (elastic#53780)
  [Lens] Expression type on document can be null (elastic#53883)
  [SIEM] [Detection engine] Add user permission to detection engine (elastic#53778)
  Update dependency @elastic/charts to v16.0.2 (elastic#52619)
  Set consistent EOL symbol in core API docs (elastic#53815)
  [Logs UI] Refactor query bar state to hooks (elastic#52656)
  [Maps] pass getFieldFormatter to DynamicTextProperty (elastic#53937)
  Invalidate alert API Key when generating a new one (elastic#53732)
  [Logs UI] HTTP API for log entries (elastic#53798)
  [kbn/pm] add caching to bootstrap (elastic#53622)
  adds createdAt and updatedAt fields to alerting (elastic#53793)
  [SR] Enable component integration tests (elastic#53893)
jloleysens added a commit to jloleysens/kibana that referenced this pull request Jan 6, 2020
…nsole-dependencies

* 'master' of github.com:elastic/kibana: (33 commits)
  adds strict types to Alerting Client (elastic#53821)
  [Dashboard] Empty screen redesign (elastic#53681)
  Migrate config deprecations and `ShieldUser` functionality to the New Platform (elastic#53768)
  increase delay to make sure license refetched (elastic#53882)
  Allow custom NP plugin paths in production (elastic#53562)
  [Maps] show custom color ramps in legend (elastic#53780)
  [Lens] Expression type on document can be null (elastic#53883)
  [SIEM] [Detection engine] Add user permission to detection engine (elastic#53778)
  Update dependency @elastic/charts to v16.0.2 (elastic#52619)
  Set consistent EOL symbol in core API docs (elastic#53815)
  [Logs UI] Refactor query bar state to hooks (elastic#52656)
  [Maps] pass getFieldFormatter to DynamicTextProperty (elastic#53937)
  Invalidate alert API Key when generating a new one (elastic#53732)
  [Logs UI] HTTP API for log entries (elastic#53798)
  [kbn/pm] add caching to bootstrap (elastic#53622)
  adds createdAt and updatedAt fields to alerting (elastic#53793)
  [SR] Enable component integration tests (elastic#53893)
  Move index patterns: src/legacy/core_plugins/data 👉 src/plugins/data (elastic#53794)
  moved Task Manager server code under "server" directory (elastic#53777)
  Rename `/api/security/oidc` to `/api/security/oidc/callback`. (elastic#53886)
  ...

# Conflicts:
#	yarn.lock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Alerting release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) v7.6.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cleanup old API key when updating and deleting an alert (since it generates a new one on update).
6 participants