Skip to content

Commit

Permalink
HBASE-23330: Fix delegation token fetch with MasterRegistry (apache#1084
Browse files Browse the repository at this point in the history
) (apache#4598)

Signed-off-by: Andrew Purtell <[email protected]>
(cherry picked from commit d8b3f55)

Co-authored-by: Bharath Vissapragada <[email protected]>
(cherry picked from commit 2cd30e5)
Change-Id: I7cf85b4b611c71b0e87d0b23a1f85542e912e197
  • Loading branch information
2 people authored and Jenkins committed Aug 18, 2022
1 parent cd2de1f commit 906c9e6
Show file tree
Hide file tree
Showing 70 changed files with 1,762 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -298,6 +299,15 @@ CompletableFuture<MasterService.Interface> getMasterStub() {
}, stub -> true, "master stub");
}

String getClusterId() {
try {
return registry.getClusterId().get();
} catch (InterruptedException | ExecutionException e) {
LOG.error("Error fetching cluster ID: ", e);
}
return null;
}

void clearMasterStubCache(MasterService.Interface stub) {
masterStub.compareAndSet(stub, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ default Table getTable(TableName tableName, ExecutorService pool) throws IOExcep
*/
TableBuilder getTableBuilder(TableName tableName, ExecutorService pool);

/**
* @return the cluster ID unique to this HBase cluster.
*/
String getClusterId();

/**
* Retrieve an Hbck implementation to fix an HBase cluster. The returned Hbck is not guaranteed to
* be thread-safe. A new instance should be created by each thread. This is a lightweight
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2175,4 +2175,14 @@ private static <T> T get(CompletableFuture<T> future) throws IOException {
throw new IOException(cause);
}
}

@Override
public String getClusterId() {
try {
return registry.getClusterId().get();
} catch (InterruptedException | ExecutionException e) {
LOG.error("Error fetching cluster ID: ", e);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,10 @@ public void abort(String why, Throwable e) {
public boolean isAborted() {
return delegate.isAborted();
}
}

@Override
public String getClusterId() {
return delegate.getClusterId();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,10 @@ public TableBuilder getTableBuilder(TableName tableName, ExecutorService pool) {
@Override
public void clearRegionLocationCache() {
}

@Override
public String getClusterId() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,5 +314,10 @@ public TableBuilder getTableBuilder(TableName tableName, ExecutorService pool) {
@Override
public void clearRegionLocationCache() {
}

@Override
public String getClusterId() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@ public Hbck getHbck() throws IOException {
public Hbck getHbck(ServerName masterServer) throws IOException {
return conn.getHbck(masterServer);
}

@Override
public String getClusterId() {
return conn.getClusterId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos;
import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.hbase.zookeeper.ZKClusterId;
import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.security.token.Token;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.zookeeper.KeeperException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -197,7 +194,7 @@ public static void obtainTokenForJob(final Connection conn, final JobConf job, U
public static void addTokenForJob(final Connection conn, final JobConf job, User user)
throws IOException, InterruptedException {

Token<AuthenticationTokenIdentifier> token = getAuthToken(conn.getConfiguration(), user);
Token<AuthenticationTokenIdentifier> token = getAuthToken(conn, user);
if (token == null) {
token = ClientTokenUtil.obtainToken(conn, user);
}
Expand All @@ -215,7 +212,7 @@ public static void addTokenForJob(final Connection conn, final JobConf job, User
*/
public static void addTokenForJob(final Connection conn, User user, Job job)
throws IOException, InterruptedException {
Token<AuthenticationTokenIdentifier> token = getAuthToken(conn.getConfiguration(), user);
Token<AuthenticationTokenIdentifier> token = getAuthToken(conn, user);
if (token == null) {
token = ClientTokenUtil.obtainToken(conn, user);
}
Expand All @@ -233,7 +230,7 @@ public static void addTokenForJob(final Connection conn, User user, Job job)
*/
public static boolean addTokenIfMissing(Connection conn, User user)
throws IOException, InterruptedException {
Token<AuthenticationTokenIdentifier> token = getAuthToken(conn.getConfiguration(), user);
Token<AuthenticationTokenIdentifier> token = getAuthToken(conn, user);
if (token == null) {
token = ClientTokenUtil.obtainToken(conn, user);
user.getUGI().addToken(token.getService(), token);
Expand All @@ -246,19 +243,12 @@ public static boolean addTokenIfMissing(Connection conn, User user)
* Get the authentication token of the user for the cluster specified in the configuration
* @return null if the user does not have the token, otherwise the auth token for the cluster.
*/
private static Token<AuthenticationTokenIdentifier> getAuthToken(Configuration conf, User user)
throws IOException, InterruptedException {
ZKWatcher zkw = new ZKWatcher(conf, "TokenUtil-getAuthToken", null);
try {
String clusterId = ZKClusterId.readClusterIdZNode(zkw);
if (clusterId == null) {
throw new IOException("Failed to get cluster ID");
}
return new AuthenticationTokenSelector().selectToken(new Text(clusterId), user.getTokens());
} catch (KeeperException e) {
throw new IOException(e);
} finally {
zkw.close();
private static Token<AuthenticationTokenIdentifier> getAuthToken(Connection conn, User user)
throws IOException {
final String clusterId = conn.getClusterId();
if (clusterId == null) {
throw new IOException("Failed to get cluster ID");
}
return new AuthenticationTokenSelector().selectToken(new Text(clusterId), user.getTokens());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,19 @@ public boolean updateConnectionAccessTime() {
return false;
}

/**
* @return Cluster ID for the HBase cluster or null if there is an err making the connection.
*/
public String getClusterId() {
try {
ConnectionInfo connInfo = getCurrentConnection();
return connInfo.connection.getClusterId();
} catch (IOException e) {
LOG.error("Error getting connection: ", e);
}
return null;
}

class ConnectionInfo {
final Connection connection;
final String userName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,5 +570,10 @@ public RegionLocator getRegionLocator() throws IOException {
@Override
public void clearRegionLocationCache() {
}

@Override
public String getClusterId() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,11 @@ public TThriftServerType getThriftServerType() {
return TThriftServerType.ONE;
}

@Override
public String getClusterId() throws TException {
return connectionCache.getClusterId();
}

private static IOError getIOError(Throwable throwable) {
IOError error = new IOErrorWithCause(throwable);
error.setCanRetry(!(throwable instanceof DoNotRetryIOException));
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 906c9e6

Please sign in to comment.