-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HADOOP-18890: remove use of okhttp in runtime code #6057
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
Changes from all commits
e13c51b
4d8e8c8
de11b54
2504e29
0bb3e35
eb3e143
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,21 +19,28 @@ | |
| package org.apache.hadoop.hdfs.web.oauth2; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Request; | ||
| import okhttp3.RequestBody; | ||
| import okhttp3.Response; | ||
|
|
||
| import org.apache.hadoop.classification.InterfaceAudience; | ||
| import org.apache.hadoop.classification.InterfaceStability; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.hdfs.web.URLConnectionFactory; | ||
| import org.apache.hadoop.util.JsonSerialization; | ||
| import org.apache.hadoop.util.Timer; | ||
| import org.apache.http.HttpHeaders; | ||
| import org.apache.http.HttpStatus; | ||
| import org.apache.http.NameValuePair; | ||
| import org.apache.http.client.config.RequestConfig; | ||
| import org.apache.http.client.entity.UrlEncodedFormEntity; | ||
| import org.apache.http.client.methods.CloseableHttpResponse; | ||
| import org.apache.http.client.methods.HttpPost; | ||
| import org.apache.http.impl.client.CloseableHttpClient; | ||
| import org.apache.http.impl.client.HttpClientBuilder; | ||
| import org.apache.http.message.BasicNameValuePair; | ||
| import org.apache.http.util.EntityUtils; | ||
|
|
||
| import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.OAUTH_CLIENT_ID_KEY; | ||
| import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.OAUTH_REFRESH_URL_KEY; | ||
|
|
@@ -97,38 +104,37 @@ public synchronized String getAccessToken() throws IOException { | |
| } | ||
|
|
||
| void refresh() throws IOException { | ||
| OkHttpClient client = new OkHttpClient.Builder() | ||
| .connectTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS) | ||
| .readTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS) | ||
| .build(); | ||
|
|
||
| String bodyString = Utils.postBody(CLIENT_SECRET, getCredential(), | ||
| GRANT_TYPE, CLIENT_CREDENTIALS, | ||
| CLIENT_ID, clientId); | ||
|
|
||
| RequestBody body = RequestBody.create(bodyString, URLENCODED); | ||
|
|
||
| Request request = new Request.Builder() | ||
| .url(refreshURL) | ||
| .post(body) | ||
| final List<NameValuePair> pairs = new ArrayList<>(); | ||
| pairs.add(new BasicNameValuePair(CLIENT_SECRET, getCredential())); | ||
| pairs.add(new BasicNameValuePair(GRANT_TYPE, CLIENT_CREDENTIALS)); | ||
| pairs.add(new BasicNameValuePair(CLIENT_ID, clientId)); | ||
| final RequestConfig config = RequestConfig.custom() | ||
| .setConnectTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT) | ||
| .setConnectionRequestTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT) | ||
| .setSocketTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT) | ||
| .build(); | ||
| try (Response response = client.newCall(request).execute()) { | ||
| if (!response.isSuccessful()) { | ||
| throw new IOException("Unexpected code " + response); | ||
| } | ||
|
|
||
| if (response.code() != HttpStatus.SC_OK) { | ||
| throw new IllegalArgumentException("Received invalid http response: " | ||
| + response.code() + ", text = " + response.toString()); | ||
| try (CloseableHttpClient client = | ||
| HttpClientBuilder.create().setDefaultRequestConfig(config).build()) { | ||
| final HttpPost httpPost = new HttpPost(refreshURL); | ||
| httpPost.setEntity(new UrlEncodedFormEntity(pairs, StandardCharsets.UTF_8)); | ||
| httpPost.setHeader(HttpHeaders.CONTENT_TYPE, URLENCODED); | ||
| try (CloseableHttpResponse response = client.execute(httpPost)) { | ||
| final int statusCode = response.getStatusLine().getStatusCode(); | ||
| if (statusCode != HttpStatus.SC_OK) { | ||
| throw new IllegalArgumentException( | ||
| "Received invalid http response: " + statusCode + ", text = " + | ||
| EntityUtils.toString(response.getEntity())); | ||
| } | ||
| Map<?, ?> responseBody = JsonSerialization.mapReader().readValue( | ||
| EntityUtils.toString(response.getEntity())); | ||
|
|
||
| String newExpiresIn = responseBody.get(EXPIRES_IN).toString(); | ||
| timer.setExpiresIn(newExpiresIn); | ||
|
|
||
| accessToken = responseBody.get(ACCESS_TOKEN).toString(); | ||
| } | ||
|
|
||
| Map<?, ?> responseBody = JsonSerialization.mapReader().readValue( | ||
| response.body().string()); | ||
|
|
||
| String newExpiresIn = responseBody.get(EXPIRES_IN).toString(); | ||
| timer.setExpiresIn(newExpiresIn); | ||
|
|
||
| accessToken = responseBody.get(ACCESS_TOKEN).toString(); | ||
| } catch (RuntimeException e) { | ||
| throw new IOException("Unable to obtain access token from credential", e); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add e.toString to the text
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm duplicating this because Spotbugs has its weird rules. The pre-existing catch already does this - do I change all the catches? |
||
| } catch (Exception e) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is now a duplicate of the handler above, isn't it? so they can be collapsed
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spotbugs is what is making me duplicate the catch. It doesn't like catches of Exception and this is one workaround. |
||
| throw new IOException("Unable to obtain access token from credential", e); | ||
| } | ||
|
|
||
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.
let's check the return content type too; been burned by both proxies and how the abfs oauth failure is 200 + text/html for users.
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.
The pre-existing code did not check the content-type of the return. It is not uncommon for an API to return JSON but not to have
application/jsonContent-Type. I can make the change but I'm worried that the API we call may not set the expected content-type on the response.