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

es.createClient: improve config merging logic #129587

Merged

Conversation

pgayvallet
Copy link
Contributor

@pgayvallet pgayvallet commented Apr 6, 2022

Summary

Fix #128661

  • improve the config merging logic to handle the case of conflict between username/password and serviceAccountToken
  • use auth.bearer config instead of manual header for client configuration

Checklist

@pgayvallet pgayvallet added Feature:elasticsearch Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc release_note:skip Skip the PR/issue when compiling release notes v8.3.0 labels Apr 6, 2022
@@ -88,8 +88,9 @@ export function parseClientOptions(
password: config.password,
};
} else if (config.serviceAccountToken) {
// TODO: change once ES client has native support for service account tokens: https://github.com/elastic/elasticsearch-js/issues/1477
clientOptions.headers!.authorization = `Bearer ${config.serviceAccountToken}`;
clientOptions.auth = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not directly related to what this PR was supposed to address, but I saw this TODO and the client is now natively supporting bearer auth, so.

import { Writable } from '@kbn/utility-types';
import type { ElasticsearchClientConfig } from './client';

type WritableConfig = Writable<ElasticsearchClientConfig>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ElasticsearchClientConfig / ElasticsearchConfig defines their properties as readonly, so I had to use a writable version here.

@pgayvallet pgayvallet marked this pull request as ready for review April 6, 2022 18:15
@pgayvallet pgayvallet requested a review from a team as a code owner April 6, 2022 18:15
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-core (Team:Core)

delete writableBaseConfig.serviceAccountToken;
}

return merge({}, writableBaseConfig, configOverrides);
Copy link
Member

Choose a reason for hiding this comment

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

just a thought/NIT: since we're already shallow cloning the object on line 21 {} at the beginning is not needed. although having it might prevent future bugs if the code changes and then we're mutating objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, I agree that it's not strictly necessary, but it just felt safer to already add it. OTOH there is a unit test to check against mutating the source, so it may be fine

Comment on lines 158 to 160
clientConfig?: Partial<ElasticsearchClientConfig>
) {
const config = clientConfig ? merge({}, baseConfig, clientConfig) : baseConfig;
const config = clientConfig ? mergeConfig(baseConfig, clientConfig) : baseConfig;
Copy link
Member

Choose a reason for hiding this comment

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

isnt this easier to read?

clientConfig: Partial<ElasticsearchClientConfig> = {}
) {

const config = mergeConfig(baseConfig, clientConfig);

I am also concerned that not using mergeConfig in both cases might cause unexpected bugs since the function mergeConfig has special logic that will only apply to 1 branch of this statement

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fair enough, that seems reasonable

@pgayvallet pgayvallet requested a review from Bamieh April 8, 2022 06:26
Copy link
Member

@Bamieh Bamieh left a comment

Choose a reason for hiding this comment

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

LGTM

@pgayvallet
Copy link
Contributor Author

@elasticmachine merge upstream

@pgayvallet
Copy link
Contributor Author

@elasticmachine merge upstream

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

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

@pgayvallet pgayvallet added v7.17.3 auto-backport Deprecated - use backport:version if exact versions are needed labels Apr 11, 2022
@pgayvallet pgayvallet merged commit b822275 into elastic:main Apr 11, 2022
@kibanamachine
Copy link
Contributor

💔 All backports failed

Status Branch Result
7.17 Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 129587

Questions ?

Please refer to the Backport tool documentation

pgayvallet added a commit to pgayvallet/kibana that referenced this pull request Apr 11, 2022
* es.createClient: improve config merging logic

* add additional test

* fix test

* address review comments

Co-authored-by: Kibana Machine <[email protected]>
(cherry picked from commit b822275)

# Conflicts:
#	src/core/server/elasticsearch/elasticsearch_service.ts
pgayvallet added a commit that referenced this pull request Apr 11, 2022
* es.createClient: improve config merging logic (#129587)

* es.createClient: improve config merging logic

* add additional test

* fix test

* address review comments

Co-authored-by: Kibana Machine <[email protected]>
(cherry picked from commit b822275)

# Conflicts:
#	src/core/server/elasticsearch/elasticsearch_service.ts

* use 7.x config props
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Deprecated - use backport:version if exact versions are needed Feature:elasticsearch release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v7.17.3 v8.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ES service account token can be ignored for custom clients
5 participants