DefaultImpersonationProvider implements ImpersonationProvider { @Override public void authorize(UserGroupInformation user, String remoteAddress) throws AuthorizationException { if (user == null) { throw new IllegalArgumentException("user is null."); } UserGroupInformation realUser = user.getRealUser(); if (realUser == null) { return; } AccessControlList acl = proxyUserAcl.get(configPrefix + realUser.getShortUserName()); if (acl == null || !acl.isUserAllowed(user)) { throw new AuthorizationException("User: " + realUser.getUserName() + " is not allowed to impersonate " + user.getUserName()); } MachineList MachineList = proxyHosts.get( getProxySuperuserIpConfKey(realUser.getShortUserName())); if(MachineList == null || !MachineList.includes(remoteAddress)) { throw new AuthorizationException("Unauthorized connection for super-user: " + realUser.getUserName() + " from IP " + remoteAddress); } }  static synchronized DefaultImpersonationProvider getTestProvider(); @Override void setConf(Configuration conf); @Override void init(String configurationPrefix); @Override Configuration getConf(); @Override void authorize(UserGroupInformation user, 
      String remoteAddress); String getProxySuperuserUserConfKey(String userName); String getProxySuperuserGroupConfKey(String userName); String getProxySuperuserIpConfKey(String userName); @VisibleForTesting Map<String, Collection<String>> getProxyGroups(); @VisibleForTesting Map<String, Collection<String>> getProxyHosts();  }
@Test public void testAuthorizationSuccess() throws AuthorizationException { proxyUser = "fakeuser"; user = "dummyUser"; when(realUserUGI.getShortUserName()).thenReturn(proxyUser); when(userGroupInformation.getRealUser()).thenReturn(realUserUGI); provider.authorize(userGroupInformation, "2.2.2.2"); user = "somerandomuser"; proxyUser = "test.user"; when(realUserUGI.getShortUserName()).thenReturn(proxyUser); when(userGroupInformation.getRealUser()).thenReturn(realUserUGI); provider.authorize(userGroupInformation, "2.2.2.2"); }
@Test public void testAuthorizationFailure() throws Exception { user = "dummyUser"; proxyUser = "test user2"; when(realUserUGI.getShortUserName()).thenReturn(proxyUser); when(realUserUGI.getUserName()).thenReturn(proxyUser); when(userGroupInformation.getUserName()).thenReturn(user); when(userGroupInformation.getRealUser()).thenReturn(realUserUGI); LambdaTestUtils.intercept(AuthorizationException.class, "User: " + proxyUser + " is not allowed to impersonate " + user, () -> provider.authorize(userGroupInformation, "2.2.2.2")); }