@@ -1585,22 +1585,34 @@ public Void run() throws Exception {
15851585 }
15861586
15871587 @ Test
1588- public void testProxyUser () throws Exception {
1588+ public void testProxyUserKerb () throws Exception {
1589+ doProxyUserTest (true );
1590+ }
1591+
1592+ @ Test
1593+ public void testProxyUserSimple () throws Exception {
1594+ doProxyUserTest (false );
1595+ }
1596+
1597+ public void doProxyUserTest (final boolean kerberos ) throws Exception {
15891598 Configuration conf = new Configuration ();
15901599 conf .set ("hadoop.security.authentication" , "kerberos" );
15911600 UserGroupInformation .setConfiguration (conf );
15921601 final File testDir = getTestDir ();
15931602 conf = createBaseKMSConf (testDir );
1594- conf .set ("hadoop.kms.authentication.type" , "kerberos" );
1603+ if (kerberos ) {
1604+ conf .set ("hadoop.kms.authentication.type" , "kerberos" );
1605+ }
15951606 conf .set ("hadoop.kms.authentication.kerberos.keytab" ,
15961607 keytab .getAbsolutePath ());
15971608 conf .set ("hadoop.kms.authentication.kerberos.principal" , "HTTP/localhost" );
15981609 conf .set ("hadoop.kms.authentication.kerberos.name.rules" , "DEFAULT" );
1599- conf .set ("hadoop.kms.proxyuser.client.users" , "foo" );
1610+ conf .set ("hadoop.kms.proxyuser.client.users" , "foo,bar " );
16001611 conf .set ("hadoop.kms.proxyuser.client.hosts" , "*" );
1601- conf .set (KeyAuthorizationKeyProvider .KEY_ACL + "kAA.ALL" , "*" );
1602- conf .set (KeyAuthorizationKeyProvider .KEY_ACL + "kBB.ALL" , "*" );
1603- conf .set (KeyAuthorizationKeyProvider .KEY_ACL + "kCC.ALL" , "*" );
1612+ conf .set (KeyAuthorizationKeyProvider .KEY_ACL + "kAA.ALL" , "client" );
1613+ conf .set (KeyAuthorizationKeyProvider .KEY_ACL + "kBB.ALL" , "foo" );
1614+ conf .set (KeyAuthorizationKeyProvider .KEY_ACL + "kCC.ALL" , "foo1" );
1615+ conf .set (KeyAuthorizationKeyProvider .KEY_ACL + "kDD.ALL" , "bar" );
16041616
16051617 writeConf (testDir , conf );
16061618
@@ -1611,9 +1623,16 @@ public Void call() throws Exception {
16111623 conf .setInt (KeyProvider .DEFAULT_BITLENGTH_NAME , 64 );
16121624 final URI uri = createKMSUri (getKMSUrl ());
16131625
1614- // proxyuser client using kerberos credentials
1615- final UserGroupInformation clientUgi = UserGroupInformation .
1616- loginUserFromKeytabAndReturnUGI ("client" , keytab .getAbsolutePath ());
1626+ UserGroupInformation proxyUgi = null ;
1627+ if (kerberos ) {
1628+ // proxyuser client using kerberos credentials
1629+ proxyUgi = UserGroupInformation .
1630+ loginUserFromKeytabAndReturnUGI ("client" , keytab .getAbsolutePath ());
1631+ } else {
1632+ proxyUgi = UserGroupInformation .createRemoteUser ("client" );
1633+ }
1634+
1635+ final UserGroupInformation clientUgi = proxyUgi ;
16171636 clientUgi .doAs (new PrivilegedExceptionAction <Void >() {
16181637 @ Override
16191638 public Void run () throws Exception {
@@ -1649,6 +1668,123 @@ public Void run() throws Exception {
16491668 return null ;
16501669 }
16511670 });
1671+
1672+ // authorized proxyuser
1673+ UserGroupInformation barUgi =
1674+ UserGroupInformation .createProxyUser ("bar" , clientUgi );
1675+ barUgi .doAs (new PrivilegedExceptionAction <Void >() {
1676+ @ Override
1677+ public Void run () throws Exception {
1678+ Assert .assertNotNull (kp .createKey ("kDD" ,
1679+ new KeyProvider .Options (conf )));
1680+ return null ;
1681+ }
1682+ });
1683+ return null ;
1684+ }
1685+ });
1686+
1687+ return null ;
1688+ }
1689+ });
1690+ }
1691+
1692+ @ Test
1693+ public void testWebHDFSProxyUserKerb () throws Exception {
1694+ doWebHDFSProxyUserTest (true );
1695+ }
1696+
1697+ @ Test
1698+ public void testWebHDFSProxyUserSimple () throws Exception {
1699+ doWebHDFSProxyUserTest (false );
1700+ }
1701+
1702+ public void doWebHDFSProxyUserTest (final boolean kerberos ) throws Exception {
1703+ Configuration conf = new Configuration ();
1704+ conf .set ("hadoop.security.authentication" , "kerberos" );
1705+ UserGroupInformation .setConfiguration (conf );
1706+ final File testDir = getTestDir ();
1707+ conf = createBaseKMSConf (testDir );
1708+ if (kerberos ) {
1709+ conf .set ("hadoop.kms.authentication.type" , "kerberos" );
1710+ }
1711+ conf .set ("hadoop.kms.authentication.kerberos.keytab" ,
1712+ keytab .getAbsolutePath ());
1713+ conf .set ("hadoop.kms.authentication.kerberos.principal" , "HTTP/localhost" );
1714+ conf .set ("hadoop.kms.authentication.kerberos.name.rules" , "DEFAULT" );
1715+ conf .set ("hadoop.security.kms.client.timeout" , "300" );
1716+ conf .set ("hadoop.kms.proxyuser.client.users" , "foo,bar" );
1717+ conf .set ("hadoop.kms.proxyuser.client.hosts" , "*" );
1718+ conf .set (KeyAuthorizationKeyProvider .KEY_ACL + "kAA.ALL" , "foo" );
1719+ conf .set (KeyAuthorizationKeyProvider .KEY_ACL + "kBB.ALL" , "foo1" );
1720+ conf .set (KeyAuthorizationKeyProvider .KEY_ACL + "kCC.ALL" , "bar" );
1721+
1722+ writeConf (testDir , conf );
1723+
1724+ runServer (null , null , testDir , new KMSCallable <Void >() {
1725+ @ Override
1726+ public Void call () throws Exception {
1727+ final Configuration conf = new Configuration ();
1728+ conf .setInt (KeyProvider .DEFAULT_BITLENGTH_NAME , 64 );
1729+ final URI uri = createKMSUri (getKMSUrl ());
1730+
1731+ UserGroupInformation proxyUgi = null ;
1732+ if (kerberos ) {
1733+ // proxyuser client using kerberos credentials
1734+ proxyUgi = UserGroupInformation .
1735+ loginUserFromKeytabAndReturnUGI ("client" , keytab .getAbsolutePath ());
1736+ } else {
1737+ proxyUgi = UserGroupInformation .createRemoteUser ("client" );
1738+ }
1739+
1740+ final UserGroupInformation clientUgi = proxyUgi ;
1741+ clientUgi .doAs (new PrivilegedExceptionAction <Void >() {
1742+ @ Override
1743+ public Void run () throws Exception {
1744+
1745+ // authorized proxyuser
1746+ UserGroupInformation fooUgi =
1747+ UserGroupInformation .createProxyUser ("foo" , clientUgi );
1748+ fooUgi .doAs (new PrivilegedExceptionAction <Void >() {
1749+ @ Override
1750+ public Void run () throws Exception {
1751+ KeyProvider kp = new KMSClientProvider (uri , conf );
1752+ Assert .assertNotNull (kp .createKey ("kAA" ,
1753+ new KeyProvider .Options (conf )));
1754+ return null ;
1755+ }
1756+ });
1757+
1758+ // unauthorized proxyuser
1759+ UserGroupInformation foo1Ugi =
1760+ UserGroupInformation .createProxyUser ("foo1" , clientUgi );
1761+ foo1Ugi .doAs (new PrivilegedExceptionAction <Void >() {
1762+ @ Override
1763+ public Void run () throws Exception {
1764+ try {
1765+ KeyProvider kp = new KMSClientProvider (uri , conf );
1766+ kp .createKey ("kBB" , new KeyProvider .Options (conf ));
1767+ Assert .fail ();
1768+ } catch (Exception ex ) {
1769+ Assert .assertTrue (ex .getMessage (), ex .getMessage ().contains ("Forbidden" ));
1770+ }
1771+ return null ;
1772+ }
1773+ });
1774+
1775+ // authorized proxyuser
1776+ UserGroupInformation barUgi =
1777+ UserGroupInformation .createProxyUser ("bar" , clientUgi );
1778+ barUgi .doAs (new PrivilegedExceptionAction <Void >() {
1779+ @ Override
1780+ public Void run () throws Exception {
1781+ KeyProvider kp = new KMSClientProvider (uri , conf );
1782+ Assert .assertNotNull (kp .createKey ("kCC" ,
1783+ new KeyProvider .Options (conf )));
1784+ return null ;
1785+ }
1786+ });
1787+
16521788 return null ;
16531789 }
16541790 });
0 commit comments