4545import java .util .concurrent .atomic .AtomicBoolean ;
4646import java .util .concurrent .atomic .AtomicInteger ;
4747
48+ import org .apache .commons .lang .StringUtils ;
4849import org .apache .commons .logging .Log ;
4950import org .apache .commons .logging .LogFactory ;
5051import org .apache .hadoop .conf .Configuration ;
52+ import org .apache .hadoop .hbase .AuthFailedException ;
5153import org .apache .hadoop .hbase .DoNotRetryIOException ;
5254import org .apache .hadoop .hbase .HBaseConfiguration ;
5355import org .apache .hadoop .hbase .HConstants ;
183185import org .apache .hadoop .hbase .protobuf .generated .MasterProtos .UnassignRegionResponse ;
184186import org .apache .hadoop .hbase .regionserver .RegionServerStoppedException ;
185187import org .apache .hadoop .hbase .security .User ;
188+ import org .apache .hadoop .hbase .security .User .SecureHadoopUser ;
186189import org .apache .hadoop .hbase .security .UserProvider ;
187190import org .apache .hadoop .hbase .util .Bytes ;
188191import org .apache .hadoop .hbase .util .EnvironmentEdgeManager ;
192195import org .apache .hadoop .hbase .zookeeper .ZKUtil ;
193196import org .apache .hadoop .hbase .zookeeper .ZooKeeperWatcher ;
194197import org .apache .hadoop .ipc .RemoteException ;
198+ import org .apache .hadoop .security .UserGroupInformation ;
195199import org .apache .zookeeper .KeeperException ;
196200
197201import com .google .common .annotations .VisibleForTesting ;
@@ -336,8 +340,32 @@ public static HConnection createConnection(Configuration conf) throws IOExceptio
336340 }
337341
338342 static ClusterConnection createConnectionInternal (Configuration conf ) throws IOException {
343+ // UserProvider provider = UserProvider.instantiate(conf);
344+ // return createConnection(conf, false, null, provider.getCurrent());
339345 UserProvider provider = UserProvider .instantiate (conf );
340- return createConnection (conf , false , null , provider .getCurrent ());
346+ User currentUser = null ;
347+ try {
348+ currentUser = provider .getCurrent ();
349+ } catch (Exception e ) {
350+ LOG .warn ("Failed to get current user from configuration." , e );
351+ currentUser = null ;
352+ }
353+
354+ if (currentUser == null ) {
355+ currentUser = getCurrentUser ();
356+ }
357+
358+ return createConnection (conf , false , null , currentUser );
359+ }
360+
361+ private static User getCurrentUser (){
362+ String currentUserName = System .getProperty ("user.name" );
363+ if (StringUtils .isBlank (currentUserName )){
364+ LOG .warn ("The current user name is blank, and set it to hbase user." );
365+ currentUserName = "hbase" ;
366+ }
367+
368+ return new User .SecureHadoopUser (UserGroupInformation .createRemoteUser (currentUserName ));
341369 }
342370
343371 /**
@@ -362,8 +390,21 @@ static ClusterConnection createConnectionInternal(Configuration conf) throws IOE
362390 */
363391 public static HConnection createConnection (Configuration conf , ExecutorService pool )
364392 throws IOException {
393+ // UserProvider provider = UserProvider.instantiate(conf);
365394 UserProvider provider = UserProvider .instantiate (conf );
366- return createConnection (conf , false , pool , provider .getCurrent ());
395+ User currentUser = null ;
396+ try {
397+ currentUser = provider .getCurrent ();
398+ } catch (Exception e ) {
399+ LOG .warn ("Failed to get current user from configuration." , e );
400+ currentUser = null ;
401+ }
402+
403+ if (currentUser == null ) {
404+ currentUser = getCurrentUser ();
405+ }
406+
407+ return createConnection (conf , false , pool , currentUser );
367408 }
368409
369410 /**
@@ -420,8 +461,21 @@ public static HConnection createConnection(Configuration conf, ExecutorService p
420461 @ Deprecated
421462 static HConnection createConnection (final Configuration conf , final boolean managed )
422463 throws IOException {
464+ //UserProvider provider = UserProvider.instantiate(conf);
423465 UserProvider provider = UserProvider .instantiate (conf );
424- return createConnection (conf , managed , null , provider .getCurrent ());
466+ User currentUser = null ;
467+ try {
468+ currentUser = provider .getCurrent ();
469+ } catch (Exception e ) {
470+ LOG .warn ("Failed to get current user from configuration." , e );
471+ currentUser = null ;
472+ }
473+
474+ if (currentUser == null ) {
475+ currentUser = getCurrentUser ();
476+ }
477+
478+ return createConnection (conf , managed , null , currentUser );
425479 }
426480
427481 @ Deprecated
@@ -1555,6 +1609,11 @@ Object makeStub() throws IOException {
15551609 exceptionCaught = e ;
15561610 } catch (ServiceException e ) {
15571611 exceptionCaught = e ;
1612+ String msg = e .getMessage ();
1613+ if (msg .indexOf ("AuthFailedException" ) != -1 )
1614+ {
1615+ throw new AuthFailedException (msg );
1616+ }
15581617 }
15591618
15601619 throw new MasterNotRunningException (exceptionCaught );
@@ -1735,9 +1794,15 @@ public MasterKeepAliveConnection getKeepAliveMasterService()
17351794 MasterServiceStubMaker stubMaker = new MasterServiceStubMaker ();
17361795 try {
17371796 this .masterServiceState .stub = stubMaker .makeStub ();
1738- } catch (MasterNotRunningException ex ) {
1797+ }
1798+ catch (AuthFailedException ex1 )
1799+ {
1800+ throw ex1 ;
1801+ }
1802+ catch (MasterNotRunningException ex ) {
17391803 throw ex ;
1740- } catch (IOException e ) {
1804+ }
1805+ catch (IOException e ) {
17411806 // rethrow as MasterNotRunningException so that we can keep the method sig
17421807 throw new MasterNotRunningException (e );
17431808 }
0 commit comments