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

Add identity/authorization headers into Rest Client calls #613

Merged
merged 11 commits into from
Apr 13, 2023

Conversation

kokibas
Copy link
Contributor

@kokibas kokibas commented Mar 27, 2023

Description

Here I create a new Request Options object based on Request Options.DEFAULT using the to Builder() method, add the "Authorization" header with the token, and build a new RequestOptions object using the build() method.

Issues Resolved

#430

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@kokibas kokibas changed the title Add headers [FEATURE] Add identity/authorization headers into Rest Client calls Mar 27, 2023
@codecov-commenter
Copy link

codecov-commenter commented Mar 27, 2023

Codecov Report

Merging #613 (281e595) into main (97d51f4) will decrease coverage by 0.08%.
The diff coverage is 80.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@             Coverage Diff              @@
##               main     #613      +/-   ##
============================================
- Coverage     63.70%   63.62%   -0.08%     
  Complexity      263      263              
============================================
  Files            52       52              
  Lines          1138     1141       +3     
  Branches         36       36              
============================================
+ Hits            725      726       +1     
- Misses          402      404       +2     
  Partials         11       11              
Impacted Files Coverage Δ
src/main/java/org/opensearch/sdk/SDKClient.java 90.56% <80.00%> (-1.67%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

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

Thanks for this initial PR. It's very helpful that you have shown how to add the headers.

Can we make a setter for the client that updates the options? Something like setOptions(options) which you create this way?

src/main/java/org/opensearch/sdk/SDKClient.java Outdated Show resolved Hide resolved
@dbwiddis dbwiddis added the CCI Part of the College Contributor Initiative label Mar 27, 2023
Signed-off-by: Nurym <[email protected]>
Copy link
Member

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

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

A few general comments here:

  1. We need to continue to provide the default option if nothing is passed. Don't hard-code strings.
  2. We should just have a single string for a header, and not require the user to pass two strings that we append to each other.
  3. We should allow adding the header after instantiation. A setHeader() method would work for this but I think a much better idea is just having a setRequestOptions() method.

Copy link
Member

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

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

Let's simplify this to just having a RequestOptions field.

src/main/java/org/opensearch/sdk/SDKClient.java Outdated Show resolved Hide resolved
src/main/java/org/opensearch/sdk/SDKClient.java Outdated Show resolved Hide resolved
src/main/java/org/opensearch/sdk/SDKClient.java Outdated Show resolved Hide resolved
dbwiddis
dbwiddis previously approved these changes Mar 29, 2023
Copy link
Member

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

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

LGTM, not sure this is the "final solution" but should work for now until we figure out something better!

@owaiskazi19
Copy link
Member

owaiskazi19 commented Apr 5, 2023

@dbwiddis I'm not clear with what does this change actually does? Talks about adding identity/authorization headers but all I see is a setter and the default value replaced?

@dbwiddis
Copy link
Member

dbwiddis commented Apr 5, 2023

@dbwiddis I'm not clear with what does this change actually does? Talks about adding identity/authorization headers but all I see is a setter and the default value replaced?

Per the issue being addressed, #430:

SDKClient REST calls need to edit the RequestOptions to add headers.

There may be a need to close/re-initialize the clients or just change the headers, that will need to be determined later.

Such editing was explored earlier in this PR in commits now superceded, such as this one:

public void search(SearchRequest request, ActionListener<SearchResponse> listener) {
    RequestOptions options = RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "Bearer TOKEN_HERE").build();
    restHighLevelClient.searchAsync(request, options, listener);
}

Of course, that solution hard codes a string as the token, which obviously doesn't work. But it points out the fact that if we want to use client.search(request, listener) we need the ability to pass other request options than the default.

Given the singleton nature of our RestClient, we can accept an incoming RestRequest from OpenSearch, which will (in the future) contain a token that we can use to send back. We can set this token into the SDKRestClient using the setter in this PR.

Is this the final solution? I doubt it. It's not thread safe. There are other client changes needed per #612. This is also just a hack to our (deprecated) client used to aid migration, see also #625.

Per my approval comment:

not sure this is the "final solution" but should work for now until we figure out something better!

If you have a better idea, this would be a good time to post it!

Copy link
Member

@owaiskazi19 owaiskazi19 left a comment

Choose a reason for hiding this comment

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

@dbwiddis this PR handles the Request Options and provides a way to add a new header to the async requets and not use only the DEFAULT one. It doesn't actually adds identity/authorization headers. A little confusing PR title got me thinking in the first place.

src/main/java/org/opensearch/sdk/SDKClient.java Outdated Show resolved Hide resolved
src/main/java/org/opensearch/sdk/SDKClient.java Outdated Show resolved Hide resolved
src/main/java/org/opensearch/sdk/SDKClient.java Outdated Show resolved Hide resolved
src/main/java/org/opensearch/sdk/SDKClient.java Outdated Show resolved Hide resolved
@kokibas kokibas changed the title [FEATURE] Add identity/authorization headers into Rest Client calls Add identity/authorization headers into Rest Client calls Apr 7, 2023
Copy link
Member

@owaiskazi19 owaiskazi19 left a comment

Choose a reason for hiding this comment

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

Still few places left to replace RequestOptions.DEFAULT; value with options like this. We should add options for all the clients which have DEFAULT currently.

@kokibas kokibas requested a review from owaiskazi19 April 7, 2023 19:27
@dbwiddis
Copy link
Member

dbwiddis commented Apr 7, 2023

We should add options for all the clients which have DEFAULT currently.

I'm not so sure we should be adding more stuff to the Java Clients, rather than just documenting how to use them. Do we actually have anything wrapped in the other clients yet?

@owaiskazi19
Copy link
Member

I'm not so sure we should be adding more stuff to the Java Clients, rather than just documenting how to use them. Do we actually have anything wrapped in the other clients yet?

That was regards to SDKIndicesClient which @kokibas has already taken care of. Are you saying we should not add options for SDKIndicesClient?

@dbwiddis
Copy link
Member

dbwiddis commented Apr 8, 2023

That was regards to SDKIndicesClient which @kokibas has already taken care of. Are you saying we should not add options for SDKIndicesClient?

Ah, sorry, I missed that context. Yes the options should be at the highest level of the Rest Client and copied to the other clients. If we removed the static from these classes, they would have access to the outer class object.

@dbwiddis dbwiddis merged commit 0506955 into opensearch-project:main Apr 13, 2023
opensearch-trigger-bot bot pushed a commit that referenced this pull request Apr 13, 2023
* added method headersToRequest

Signed-off-by: Nurym <[email protected]>

* creating a new equest ptions object

Signed-off-by: Nurym <[email protected]>

* set on the client

Signed-off-by: Nurym <[email protected]>

* added setter for requestoptions

Signed-off-by: Nurym <[email protected]>

* Merge branch 'main' into RepositoryExtension

Signed-off-by: Nurym <[email protected]>

* added options for all request

Signed-off-by: Nurym <[email protected]>

* added options for all request

Signed-off-by: Nurym <[email protected]>

---------

Signed-off-by: Nurym <[email protected]>
Co-authored-by: Owais Kazi <[email protected]>
(cherry picked from commit 0506955)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
dbwiddis pushed a commit that referenced this pull request Apr 13, 2023
* added method headersToRequest



* creating a new equest ptions object



* set on the client



* added setter for requestoptions



* Merge branch 'main' into RepositoryExtension



* added options for all request



* added options for all request



---------



(cherry picked from commit 0506955)

Signed-off-by: Nurym <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Owais Kazi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 1.x CCI Part of the College Contributor Initiative first-time-contributor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants