-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HDFS-15765. WebHdfs: add support for basic auth and custom API path #5447
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6e04605
WebHdfs: add support for basic auth and custom API path
trakos 3febb1c
WebHdfs: use UTF-8 for basic auth
trakos ee84301
WebHdfs: avoid hiding uri field in test
trakos 5babfbc
WebHdfs: reformat BasicAuthConfigurator
trakos 87e3901
WebHdfs: tweak tests and BasicAuthConfigurator instantiation
trakos 9852e9a
Move BasicAuthConfigurator.getConfigurator to URLConnectionFactory.cr…
trakos 57e1e56
Make sure that uri isn't null
trakos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...ct/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/BasicAuthConfigurator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.hadoop.hdfs.web; | ||
|
|
||
| import org.apache.hadoop.security.authentication.client.ConnectionConfigurator; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.HttpURLConnection; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Base64; | ||
|
|
||
| /** | ||
| * This class adds basic authentication to the connection, | ||
| * allowing users to access webhdfs over HTTP with basic authentication, | ||
| * for example when using Apache Knox. | ||
| */ | ||
| public class BasicAuthConfigurator implements ConnectionConfigurator { | ||
| private final ConnectionConfigurator parent; | ||
| private final String credentials; | ||
|
|
||
| static public ConnectionConfigurator getConfigurator( | ||
| ConnectionConfigurator configurator, String basicAuthCred | ||
| ) { | ||
| if (basicAuthCred != null && !basicAuthCred.isEmpty()) { | ||
| return new BasicAuthConfigurator(configurator, basicAuthCred); | ||
| } else { | ||
| return configurator; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param credentials a string of the form "username:password" | ||
| */ | ||
| public BasicAuthConfigurator( | ||
| ConnectionConfigurator parent, | ||
| String credentials | ||
| ) { | ||
| this.parent = parent; | ||
| this.credentials = credentials; | ||
| } | ||
|
|
||
| @Override | ||
| public HttpURLConnection configure(HttpURLConnection conn) throws IOException { | ||
| if (parent != null) { | ||
| parent.configure(conn); | ||
| } | ||
|
|
||
| if (credentials != null && !credentials.equals("")) { | ||
| conn.setRequestProperty( | ||
| "AUTHORIZATION", | ||
| "Basic " + Base64.getEncoder().encodeToString( | ||
| credentials.getBytes(StandardCharsets.UTF_8) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return conn; | ||
| } | ||
|
|
||
| public void destroy() { | ||
| if (parent != null && parent instanceof SSLConnectionConfigurator) { | ||
| ((SSLConnectionConfigurator)parent).destroy(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,6 +184,7 @@ public class WebHdfsFileSystem extends FileSystem | |
| private boolean isTLSKrb; | ||
|
|
||
| private boolean isServerHCFSCompatible = true; | ||
| private String pathPrefix = ""; | ||
|
|
||
| /** | ||
| * Return the protocol scheme for the FileSystem. | ||
|
|
@@ -238,17 +239,19 @@ public synchronized void initialize(URI uri, Configuration conf | |
| if(isOAuth) { | ||
| LOG.debug("Enabling OAuth2 in WebHDFS"); | ||
| connectionFactory = URLConnectionFactory | ||
| .newOAuth2URLConnectionFactory(connectTimeout, readTimeout, conf); | ||
| .newOAuth2URLConnectionFactory(connectTimeout, readTimeout, conf, uri.getUserInfo()); | ||
| } else { | ||
| LOG.debug("Not enabling OAuth2 in WebHDFS"); | ||
| connectionFactory = URLConnectionFactory | ||
| .newDefaultURLConnectionFactory(connectTimeout, readTimeout, conf); | ||
| .newDefaultURLConnectionFactory(connectTimeout, readTimeout, conf, uri.getUserInfo()); | ||
| } | ||
|
|
||
| this.isTLSKrb = "HTTPS_ONLY".equals(conf.get(DFS_HTTP_POLICY_KEY)); | ||
|
|
||
| ugi = UserGroupInformation.getCurrentUser(); | ||
| this.uri = URI.create(uri.getScheme() + "://" + uri.getAuthority()); | ||
| // Drop path and user:password from URI. | ||
| this.uri = URI.create(uri.getScheme() + "://" + uri.getHost() | ||
| + (uri.getPort() == -1 ? "" : ":" + uri.getPort())); | ||
| this.nnAddrs = resolveNNAddr(); | ||
|
|
||
| boolean isHA = HAUtilClient.isClientFailoverConfigured(conf, this.uri); | ||
|
|
@@ -307,6 +310,20 @@ public StorageStatistics provide() { | |
| return new DFSOpsCountStatistics(); | ||
| } | ||
| }); | ||
| pathPrefix = PATH_PREFIX; | ||
| boolean useBasePath = conf.getBoolean( | ||
| HdfsClientConfigKeys.DFS_CLIENT_WEBHDFS_USE_BASE_PATH_KEY, | ||
| HdfsClientConfigKeys.DFS_CLIENT_WEBHDFS_USE_BASE_PATH_DEFAULT | ||
| ); | ||
| if (uri.getPath() != null && !uri.getPath().equals("") && useBasePath) { | ||
|
||
| pathPrefix = uri.getPath(); | ||
| if (pathPrefix.endsWith("/")) { | ||
| pathPrefix = pathPrefix.substring(0, pathPrefix.length() - 1); | ||
| } | ||
| if (!pathPrefix.endsWith(PATH_PREFIX)) { | ||
| pathPrefix += PATH_PREFIX; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -634,7 +651,7 @@ URL toUrl(final HttpOpParam.Op op, final Path fspath, | |
| final Param<?,?>... parameters) throws IOException { | ||
| //initialize URI path and query | ||
|
|
||
| final String path = PATH_PREFIX | ||
| final String path = pathPrefix | ||
| + (fspath == null? "/": makeQualified(fspath).toUri().getRawPath()); | ||
| final String query = op.toQueryString() | ||
| + Param.toSortedString("&", getAuthParameters(op)) | ||
|
|
||
83 changes: 83 additions & 0 deletions
83
...adoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/web/TestBasicAuthConfigurator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.hadoop.hdfs.web; | ||
|
|
||
| import org.apache.hadoop.security.authentication.client.ConnectionConfigurator; | ||
| import org.junit.Test; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.HttpURLConnection; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Base64; | ||
|
|
||
| public class TestBasicAuthConfigurator { | ||
| @Test | ||
| public void testNullCredentials() throws IOException { | ||
| ConnectionConfigurator conf = new BasicAuthConfigurator(null, null); | ||
| HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); | ||
| conf.configure(conn); | ||
| Mockito.verify(conn, Mockito.never()).setRequestProperty(Mockito.any(), Mockito.any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyCredentials() throws IOException { | ||
| ConnectionConfigurator conf = new BasicAuthConfigurator(null, ""); | ||
| HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); | ||
| conf.configure(conn); | ||
| Mockito.verify(conn, Mockito.never()).setRequestProperty(Mockito.any(), Mockito.any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCredentialsSet() throws IOException { | ||
| String credentials = "user:pass"; | ||
| ConnectionConfigurator conf = new BasicAuthConfigurator(null, credentials); | ||
| HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); | ||
| conf.configure(conn); | ||
| Mockito.verify(conn, Mockito.times(1)).setRequestProperty( | ||
| "AUTHORIZATION", | ||
| "Basic " + Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.UTF_8)) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParentConfigurator() throws IOException { | ||
| ConnectionConfigurator parent = Mockito.mock(ConnectionConfigurator.class); | ||
| String credentials = "user:pass"; | ||
| ConnectionConfigurator conf = new BasicAuthConfigurator(parent, credentials); | ||
| HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); | ||
| conf.configure(conn); | ||
| Mockito.verify(conn, Mockito.times(1)).setRequestProperty( | ||
| "AUTHORIZATION", | ||
| "Basic " + Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.UTF_8)) | ||
| ); | ||
| Mockito.verify(parent, Mockito.times(1)).configure(conn); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCredentialsSetWithUtfAndSpecialCharacters() throws IOException { | ||
| String credentials = "\uD80C\uDD04@ą:pass"; | ||
| ConnectionConfigurator conf = new BasicAuthConfigurator(null, credentials); | ||
| HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); | ||
| conf.configure(conn); | ||
| Mockito.verify(conn, Mockito.times(1)).setRequestProperty( | ||
| "AUTHORIZATION", | ||
| "Basic " + Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.UTF_8)) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since this method has to be called by URLConnectionFactory which is in same package. Lets have it package-protected and remove
publicThere 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.
Should this method be moved to
URLConnectionFactoryclass only?Reason being, when external class calls
BasicAuthConfigurator.getConfigurator, to developer it can look that we want something out ofBasicAuthConfigurator, but instead it either gives BasicAuthConfigurator object or parent-configurator object. What you feel?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.
Good point, we might as well move it to
URLConnectionFactoryand make it private there. I've made the change.