You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2023/01/16 21:00:19 UTC

[GitHub] [beam] EgbertW opened a new pull request, #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

EgbertW opened a new pull request, #25024:
URL: https://github.com/apache/beam/pull/25024

   Addresses #25018 
   
   This PR adds a `withDefaultHeaders` setting to the ElasticsearchIO ConnectionConfiguration. This allows you to provide any type of `org.apache.http.Header` instead of sticking to the built-in `ApiKey` and `Bearer` headers supported already. In a way, their functionality could be implemented using this option instead if desirable.
   
   This addresses #25018 - it allows to create a custom class implementing `org.apache.http.Header` that manages the bearer token lifecycle and use this with ElasticsearchIO - this all without imposing any specifics in the ElasticsearchIO library.
   
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [X] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/get-started-contributing/#make-the-reviewers-job-easier).
   
   To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/workflows/Build%20python%20source%20distribution%20and%20wheels/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/workflows/Python%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/workflows/Java%20Tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go tests](https://github.com/apache/beam/workflows/Go%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] EgbertW commented on a diff in pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
EgbertW commented on code in PR #25024:
URL: https://github.com/apache/beam/pull/25024#discussion_r1073151424


##########
sdks/java/io/elasticsearch/src/main/java/org/apache/beam/sdk/io/elasticsearch/ElasticsearchIO.java:
##########
@@ -501,29 +506,55 @@ public ConnectionConfiguration withPassword(String password) {
     }
 
     /**
-     * If Elasticsearch authentication is enabled, provide an API key.
+     * If Elasticsearch authentication is enabled, provide an API key. Be aware that you can only
+     * use one of {@Code withApiToken()}, {@code withBearerToken()} and {@code withDefaultHeaders}
+     * at the same time, as they (potentially) use the same header.
      *
      * @param apiKey the API key used to authenticate to Elasticsearch
      * @return a {@link ConnectionConfiguration} describes a connection configuration to
      *     Elasticsearch.
      */
     public ConnectionConfiguration withApiKey(String apiKey) {
       checkArgument(!Strings.isNullOrEmpty(apiKey), "apiKey can not be null or empty");
+      checkArgument(getBearerToken() == null, "apiKey can not be combined with bearerToken");
+      checkArgument(getDefaultHeaders() == null, "apiKey can not be combined with defaultHeaders");
       return builder().setApiKey(apiKey).build();
     }
 
     /**
-     * If Elasticsearch authentication is enabled, provide a bearer token.
+     * If Elasticsearch authentication is enabled, provide a bearer token. Be aware that you can
+     * only use one of {@Code withApiToken()}, {@code withBearerToken()} and {@code
+     * withDefaultHeaders} at the same time, as they (potentially) use the same header.
      *
      * @param bearerToken the bearer token used to authenticate to Elasticsearch
      * @return a {@link ConnectionConfiguration} describes a connection configuration to
      *     Elasticsearch.
      */
     public ConnectionConfiguration withBearerToken(String bearerToken) {
       checkArgument(!Strings.isNullOrEmpty(bearerToken), "bearerToken can not be null or empty");
+      checkArgument(getApiKey() == null, "bearerToken can not be combined with apiKey");
+      checkArgument(getDefaultHeaders() == null, "apiKey can not be combined with defaultHeaders");

Review Comment:
   Oops. Fixed!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] egalpin commented on a diff in pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
egalpin commented on code in PR #25024:
URL: https://github.com/apache/beam/pull/25024#discussion_r1071624010


##########
sdks/java/io/elasticsearch/src/main/java/org/apache/beam/sdk/io/elasticsearch/ElasticsearchIO.java:
##########
@@ -514,6 +521,8 @@ public ConnectionConfiguration withApiKey(String apiKey) {
 
     /**
      * If Elasticsearch authentication is enabled, provide a bearer token.
+     * Be aware that you can only use one of {@Code withApiToken()}, {@code withBearerToken()} and
+     * {@code withDefaultHeaders} at the same time, as they will override eachother.

Review Comment:
   Is there a reasonable way to add Precondition checks to try to alert the user when these mutually exclusive fields have been used in conjunction?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] EgbertW commented on pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
EgbertW commented on PR #25024:
URL: https://github.com/apache/beam/pull/25024#issuecomment-1396636460

   > Thanks @EgbertW ! I’ll merge after all the workflows pass, which I expect them to 😊 Hopefully this can make the 2.45.0 cut scheduled for today!
   
   Thanks for merging! Looking forward to seeing it in the next release :)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] EgbertW commented on pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
EgbertW commented on PR #25024:
URL: https://github.com/apache/beam/pull/25024#issuecomment-1386572615

   > > > I'm not denying the utility here, but I'm not 100% clear on how this addresses #25018. Could you elaborate on how this technique can be used to work with short-lived credentials?
   > > 
   > > 
   > > Of course. What I did to work with short-lived tokens is create a new header class, something resembling:
   > > ```
   > > class OauthTokenHeader extends BasicHeader {
   > >     OIDCIDToken accessToken;
   > > 
   > >     ...
   > > 
   > >     @Override
   > >     public String getValue() {
   > >         if (accessToken.isExpired()) {
   > >             accessToken.renew();
   > >         }
   > >         return String.format("Bearer %s", accessToken.getToken());
   > >     }
   > > }
   > > ```
   > > 
   > > 
   > >     
   > >       
   > >     
   > > 
   > >       
   > >     
   > > 
   > >     
   > >   
   > > Since the Elasticsearch RestClient will add the `defaultHeaders` on each outgoing request, it will invoke `getValue` on every request, giving the opportunity to check if the token has expired and if so, renew it.
   > 
   > Aha! Very nice 😊 I think this kind of detail might be non-obvious to many users who might want to accomplish the same. Could you please add this example to the docstring for `withDefaultHeader` as an example for how someone might use it?
   
   I just updated the PR with this example added to the docstring.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] egalpin merged pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
egalpin merged PR #25024:
URL: https://github.com/apache/beam/pull/25024


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] EgbertW commented on pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
EgbertW commented on PR #25024:
URL: https://github.com/apache/beam/pull/25024#issuecomment-1384988804

   > I'm not denying the utility here, but I'm not 100% clear on how this addresses #25018. Could you elaborate on how this technique can be used to work with short-lived credentials?
   
   Of course. What I did to work with short-lived tokens is create a new header class, something resembling:
   
   ```
   class OauthTokenHeader extends BasicHeader {
       OIDCIDToken accessToken;
   
       ...
   
       @Override
       public String getValue() {
           if (accessToken.isExpired()) {
               accessToken.renew();
           }
           return String.format("Bearer %s", accessToken.getToken());
       }
   }
   ```
   
   Since the Elasticsearch RestClient will add the `defaultHeaders` on each outgoing request, it will invoke `getValue` on every request, giving the opportunity to check if the token has expired and if so, renew it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] egalpin commented on a diff in pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
egalpin commented on code in PR #25024:
URL: https://github.com/apache/beam/pull/25024#discussion_r1072864660


##########
sdks/java/io/elasticsearch/src/main/java/org/apache/beam/sdk/io/elasticsearch/ElasticsearchIO.java:
##########
@@ -501,29 +506,55 @@ public ConnectionConfiguration withPassword(String password) {
     }
 
     /**
-     * If Elasticsearch authentication is enabled, provide an API key.
+     * If Elasticsearch authentication is enabled, provide an API key. Be aware that you can only
+     * use one of {@Code withApiToken()}, {@code withBearerToken()} and {@code withDefaultHeaders}
+     * at the same time, as they (potentially) use the same header.
      *
      * @param apiKey the API key used to authenticate to Elasticsearch
      * @return a {@link ConnectionConfiguration} describes a connection configuration to
      *     Elasticsearch.
      */
     public ConnectionConfiguration withApiKey(String apiKey) {
       checkArgument(!Strings.isNullOrEmpty(apiKey), "apiKey can not be null or empty");
+      checkArgument(getBearerToken() == null, "apiKey can not be combined with bearerToken");
+      checkArgument(getDefaultHeaders() == null, "apiKey can not be combined with defaultHeaders");
       return builder().setApiKey(apiKey).build();
     }
 
     /**
-     * If Elasticsearch authentication is enabled, provide a bearer token.
+     * If Elasticsearch authentication is enabled, provide a bearer token. Be aware that you can
+     * only use one of {@Code withApiToken()}, {@code withBearerToken()} and {@code
+     * withDefaultHeaders} at the same time, as they (potentially) use the same header.
      *
      * @param bearerToken the bearer token used to authenticate to Elasticsearch
      * @return a {@link ConnectionConfiguration} describes a connection configuration to
      *     Elasticsearch.
      */
     public ConnectionConfiguration withBearerToken(String bearerToken) {
       checkArgument(!Strings.isNullOrEmpty(bearerToken), "bearerToken can not be null or empty");
+      checkArgument(getApiKey() == null, "bearerToken can not be combined with apiKey");
+      checkArgument(getDefaultHeaders() == null, "apiKey can not be combined with defaultHeaders");
       return builder().setBearerToken(bearerToken).build();
     }
 
+    /**
+     * For authentication or custom requirements, provide a set if default headers for the client.
+     * Be aware that you can only use one of {@code withApiToken()}, {@code withBearerToken()} and
+     * {@code withDefaultHeaders} at the same time, as they (potentially) use the same header.
+     *
+     * @param defaultHeaders the headers to add to outgoing requests
+     * @return a {@link ConnectionConfiguration} describes a connection configuration to
+     *     Elasticsearch.
+     */
+    public ConnectionConfiguration withDefaultHeaders(Header[] defaultHeaders) {

Review Comment:
   Any reason to prefer an array as input despite the need to convert to list for AutoValue? To better emulate the input to `restClientBuilder.setDefaultHeaders` ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] egalpin commented on pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
egalpin commented on PR #25024:
URL: https://github.com/apache/beam/pull/25024#issuecomment-1384657436

   Thanks @EgbertW !  To fix up the `spotless` linter check, you can run `./gradlew spotlessApply` from the project root on your machine; that will auto-fix any style issues which can then be committed.
   
   I noticed also that there are unit test failures due to compilation failures.  You can run tests locally by running one of the ES test suites like `./gradlew --info :sdks:java:io:elasticsearch-tests:elasticsearch-tests-7:test` to confirm that there are no more failures 😊 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] github-actions[bot] commented on pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #25024:
URL: https://github.com/apache/beam/pull/25024#issuecomment-1384578067

   Assigning reviewers. If you would like to opt out of this review, comment `assign to next reviewer`:
   
   R: @apilloud for label java.
   R: @Abacn for label io.
   
   Available commands:
   - `stop reviewer notifications` - opt out of the automated review tooling
   - `remind me after tests pass` - tag the comment author after tests pass
   - `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
   
   The PR bot will only process comments in the main thread (not review comments).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] egalpin commented on a diff in pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
egalpin commented on code in PR #25024:
URL: https://github.com/apache/beam/pull/25024#discussion_r1072860908


##########
sdks/java/io/elasticsearch/src/main/java/org/apache/beam/sdk/io/elasticsearch/ElasticsearchIO.java:
##########
@@ -501,29 +506,55 @@ public ConnectionConfiguration withPassword(String password) {
     }
 
     /**
-     * If Elasticsearch authentication is enabled, provide an API key.
+     * If Elasticsearch authentication is enabled, provide an API key. Be aware that you can only
+     * use one of {@Code withApiToken()}, {@code withBearerToken()} and {@code withDefaultHeaders}
+     * at the same time, as they (potentially) use the same header.
      *
      * @param apiKey the API key used to authenticate to Elasticsearch
      * @return a {@link ConnectionConfiguration} describes a connection configuration to
      *     Elasticsearch.
      */
     public ConnectionConfiguration withApiKey(String apiKey) {
       checkArgument(!Strings.isNullOrEmpty(apiKey), "apiKey can not be null or empty");
+      checkArgument(getBearerToken() == null, "apiKey can not be combined with bearerToken");
+      checkArgument(getDefaultHeaders() == null, "apiKey can not be combined with defaultHeaders");
       return builder().setApiKey(apiKey).build();
     }
 
     /**
-     * If Elasticsearch authentication is enabled, provide a bearer token.
+     * If Elasticsearch authentication is enabled, provide a bearer token. Be aware that you can
+     * only use one of {@Code withApiToken()}, {@code withBearerToken()} and {@code
+     * withDefaultHeaders} at the same time, as they (potentially) use the same header.
      *
      * @param bearerToken the bearer token used to authenticate to Elasticsearch
      * @return a {@link ConnectionConfiguration} describes a connection configuration to
      *     Elasticsearch.
      */
     public ConnectionConfiguration withBearerToken(String bearerToken) {
       checkArgument(!Strings.isNullOrEmpty(bearerToken), "bearerToken can not be null or empty");
+      checkArgument(getApiKey() == null, "bearerToken can not be combined with apiKey");
+      checkArgument(getDefaultHeaders() == null, "apiKey can not be combined with defaultHeaders");

Review Comment:
   nit: `apiKey` was copied over, should be `bearerToken`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] egalpin commented on a diff in pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
egalpin commented on code in PR #25024:
URL: https://github.com/apache/beam/pull/25024#discussion_r1073682783


##########
sdks/java/io/elasticsearch/src/main/java/org/apache/beam/sdk/io/elasticsearch/ElasticsearchIO.java:
##########
@@ -501,29 +506,55 @@ public ConnectionConfiguration withPassword(String password) {
     }
 
     /**
-     * If Elasticsearch authentication is enabled, provide an API key.
+     * If Elasticsearch authentication is enabled, provide an API key. Be aware that you can only
+     * use one of {@Code withApiToken()}, {@code withBearerToken()} and {@code withDefaultHeaders}
+     * at the same time, as they (potentially) use the same header.
      *
      * @param apiKey the API key used to authenticate to Elasticsearch
      * @return a {@link ConnectionConfiguration} describes a connection configuration to
      *     Elasticsearch.
      */
     public ConnectionConfiguration withApiKey(String apiKey) {
       checkArgument(!Strings.isNullOrEmpty(apiKey), "apiKey can not be null or empty");
+      checkArgument(getBearerToken() == null, "apiKey can not be combined with bearerToken");
+      checkArgument(getDefaultHeaders() == null, "apiKey can not be combined with defaultHeaders");
       return builder().setApiKey(apiKey).build();
     }
 
     /**
-     * If Elasticsearch authentication is enabled, provide a bearer token.
+     * If Elasticsearch authentication is enabled, provide a bearer token. Be aware that you can
+     * only use one of {@Code withApiToken()}, {@code withBearerToken()} and {@code
+     * withDefaultHeaders} at the same time, as they (potentially) use the same header.
      *
      * @param bearerToken the bearer token used to authenticate to Elasticsearch
      * @return a {@link ConnectionConfiguration} describes a connection configuration to
      *     Elasticsearch.
      */
     public ConnectionConfiguration withBearerToken(String bearerToken) {
       checkArgument(!Strings.isNullOrEmpty(bearerToken), "bearerToken can not be null or empty");
+      checkArgument(getApiKey() == null, "bearerToken can not be combined with apiKey");
+      checkArgument(getDefaultHeaders() == null, "apiKey can not be combined with defaultHeaders");
       return builder().setBearerToken(bearerToken).build();
     }
 
+    /**
+     * For authentication or custom requirements, provide a set if default headers for the client.
+     * Be aware that you can only use one of {@code withApiToken()}, {@code withBearerToken()} and
+     * {@code withDefaultHeaders} at the same time, as they (potentially) use the same header.
+     *
+     * @param defaultHeaders the headers to add to outgoing requests
+     * @return a {@link ConnectionConfiguration} describes a connection configuration to
+     *     Elasticsearch.
+     */
+    public ConnectionConfiguration withDefaultHeaders(Header[] defaultHeaders) {

Review Comment:
   👍



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] egalpin commented on pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
egalpin commented on PR #25024:
URL: https://github.com/apache/beam/pull/25024#issuecomment-1386096233

   > > I'm not denying the utility here, but I'm not 100% clear on how this addresses #25018. Could you elaborate on how this technique can be used to work with short-lived credentials?
   > 
   > Of course. What I did to work with short-lived tokens is create a new header class, something resembling:
   > 
   > ```
   > class OauthTokenHeader extends BasicHeader {
   >     OIDCIDToken accessToken;
   > 
   >     ...
   > 
   >     @Override
   >     public String getValue() {
   >         if (accessToken.isExpired()) {
   >             accessToken.renew();
   >         }
   >         return String.format("Bearer %s", accessToken.getToken());
   >     }
   > }
   > ```
   > 
   > Since the Elasticsearch RestClient will add the `defaultHeaders` on each outgoing request, it will invoke `getValue` on every request, giving the opportunity to check if the token has expired and if so, renew it.
   
   
   Aha! Very nice 😊  I think this kind of detail might be non-obvious to many users who might want to accomplish the same.  Could you please add this example to the docstring for `withDefaultHeader` as an example for how someone might use it?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] EgbertW commented on a diff in pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
EgbertW commented on code in PR #25024:
URL: https://github.com/apache/beam/pull/25024#discussion_r1073131415


##########
sdks/java/io/elasticsearch/src/main/java/org/apache/beam/sdk/io/elasticsearch/ElasticsearchIO.java:
##########
@@ -501,29 +506,55 @@ public ConnectionConfiguration withPassword(String password) {
     }
 
     /**
-     * If Elasticsearch authentication is enabled, provide an API key.
+     * If Elasticsearch authentication is enabled, provide an API key. Be aware that you can only
+     * use one of {@Code withApiToken()}, {@code withBearerToken()} and {@code withDefaultHeaders}
+     * at the same time, as they (potentially) use the same header.
      *
      * @param apiKey the API key used to authenticate to Elasticsearch
      * @return a {@link ConnectionConfiguration} describes a connection configuration to
      *     Elasticsearch.
      */
     public ConnectionConfiguration withApiKey(String apiKey) {
       checkArgument(!Strings.isNullOrEmpty(apiKey), "apiKey can not be null or empty");
+      checkArgument(getBearerToken() == null, "apiKey can not be combined with bearerToken");
+      checkArgument(getDefaultHeaders() == null, "apiKey can not be combined with defaultHeaders");
       return builder().setApiKey(apiKey).build();
     }
 
     /**
-     * If Elasticsearch authentication is enabled, provide a bearer token.
+     * If Elasticsearch authentication is enabled, provide a bearer token. Be aware that you can
+     * only use one of {@Code withApiToken()}, {@code withBearerToken()} and {@code
+     * withDefaultHeaders} at the same time, as they (potentially) use the same header.
      *
      * @param bearerToken the bearer token used to authenticate to Elasticsearch
      * @return a {@link ConnectionConfiguration} describes a connection configuration to
      *     Elasticsearch.
      */
     public ConnectionConfiguration withBearerToken(String bearerToken) {
       checkArgument(!Strings.isNullOrEmpty(bearerToken), "bearerToken can not be null or empty");
+      checkArgument(getApiKey() == null, "bearerToken can not be combined with apiKey");
+      checkArgument(getDefaultHeaders() == null, "apiKey can not be combined with defaultHeaders");
       return builder().setBearerToken(bearerToken).build();
     }
 
+    /**
+     * For authentication or custom requirements, provide a set if default headers for the client.
+     * Be aware that you can only use one of {@code withApiToken()}, {@code withBearerToken()} and
+     * {@code withDefaultHeaders} at the same time, as they (potentially) use the same header.
+     *
+     * @param defaultHeaders the headers to add to outgoing requests
+     * @return a {@link ConnectionConfiguration} describes a connection configuration to
+     *     Elasticsearch.
+     */
+    public ConnectionConfiguration withDefaultHeaders(Header[] defaultHeaders) {

Review Comment:
   Main reason is to mimic what is happening in e.g. the `create()` method that accepts an array `String[] addresses` which is converted to a list, where this list is used again to instantiate an array.
   
   Secondary reason is the assumption that the class is trying to avoid the use of mutable Lists, so a copy would need to be made anyway.
   
   Third reason is that `AutoValue` doesn't understand non-primitive arrays. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] egalpin commented on pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
egalpin commented on PR #25024:
URL: https://github.com/apache/beam/pull/25024#issuecomment-1384663093

   I'm not denying the utility here, but I'm not 100% clear on how this addresses https://github.com/apache/beam/issues/25018.  Could you elaborate on how this technique can be used to work with short-lived credentials?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] EgbertW commented on a diff in pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
EgbertW commented on code in PR #25024:
URL: https://github.com/apache/beam/pull/25024#discussion_r1071892453


##########
sdks/java/io/elasticsearch/src/main/java/org/apache/beam/sdk/io/elasticsearch/ElasticsearchIO.java:
##########
@@ -514,6 +521,8 @@ public ConnectionConfiguration withApiKey(String apiKey) {
 
     /**
      * If Elasticsearch authentication is enabled, provide a bearer token.
+     * Be aware that you can only use one of {@Code withApiToken()}, {@code withBearerToken()} and
+     * {@code withDefaultHeaders} at the same time, as they will override eachother.

Review Comment:
   There is but I didn't add it because the same issue was already lurking with just `withApiKey` and `withBearerToken`. I added checks to all three methods now to check that only one is used at a time - see my last update.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] EgbertW commented on pull request #25024: Add withDefaultHeaders to connection configuration for ElasticsearchIO

Posted by GitBox <gi...@apache.org>.
EgbertW commented on PR #25024:
URL: https://github.com/apache/beam/pull/25024#issuecomment-1385013214

   > Thanks @EgbertW ! To fix up the `spotless` linter check, you can run `./gradlew spotlessApply` from the project root on your machine; that will auto-fix any style issues which can then be committed.
   > 
   > I noticed also that there are unit test failures due to compilation failures. You can run tests locally by running one of the ES test suites like `./gradlew --info :sdks:java:io:elasticsearch-tests:elasticsearch-tests-7:test` to confirm that there are no more failures 😊
   
   Ah, that is helpful. I just did and fixed the formatting and the compile error - I was using a newer feature that isn't available in Java 8.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org