use of org.apache.knox.gateway.security.PrimaryPrincipal in project knox by apache.
the class SwitchCaseIdentityAssertionFilterTest method testUpperPrincipalAndGroups.
@Test
public void testUpperPrincipalAndGroups() throws Exception {
FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(config.getInitParameter("principal.case")).andReturn("Upper").anyTimes();
EasyMock.expect(config.getInitParameter("group.principal.case")).andReturn("Upper").anyTimes();
EasyMock.expect(config.getInitParameter("principal.mapping")).andReturn("").anyTimes();
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();
EasyMock.expect(context.getInitParameter("principal.mapping")).andReturn("").anyTimes();
EasyMock.replay(config);
EasyMock.replay(context);
SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter();
Subject subject = new Subject();
subject.getPrincipals().add(new PrimaryPrincipal("Member@us.apache.org"));
subject.getPrincipals().add(new GroupPrincipal("users"));
subject.getPrincipals().add(new GroupPrincipal("Admin"));
filter.init(config);
String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName());
String[] groups = filter.mapGroupPrincipals(actual, subject);
assertThat(actual, is("MEMBER@US.APACHE.ORG"));
assertThat(groups, is(arrayContainingInAnyOrder("ADMIN", "USERS")));
}
use of org.apache.knox.gateway.security.PrimaryPrincipal in project knox by apache.
the class SwitchCaseIdentityAssertionFilterTest method testLowerPrincipalAndGroups.
@Test
public void testLowerPrincipalAndGroups() throws Exception {
FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(config.getInitParameter("principal.case")).andReturn("lower").anyTimes();
EasyMock.expect(config.getInitParameter("group.principal.case")).andReturn("LOWER").anyTimes();
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();
EasyMock.expect(context.getInitParameter("principal.mapping")).andReturn("").anyTimes();
EasyMock.replay(config);
EasyMock.replay(context);
SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter();
Subject subject = new Subject();
subject.getPrincipals().add(new PrimaryPrincipal("Member@us.apache.org"));
subject.getPrincipals().add(new GroupPrincipal("users"));
subject.getPrincipals().add(new GroupPrincipal("Admin"));
filter.init(config);
String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName());
String[] groups = filter.mapGroupPrincipals(actual, subject);
assertThat(actual, is("member@us.apache.org"));
assertThat(groups, is(arrayContainingInAnyOrder("admin", "users")));
}
use of org.apache.knox.gateway.security.PrimaryPrincipal in project knox by apache.
the class DefaultIdentityAssertionFilterTest method testInitParameters.
@Test
public void testInitParameters() throws Exception {
FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(config.getInitParameter("principal.mapping")).andReturn("").anyTimes();
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();
EasyMock.expect(context.getInitParameter("principal.mapping")).andReturn("").anyTimes();
EasyMock.replay(config);
EasyMock.replay(context);
IdentityAsserterFilter filter = new IdentityAsserterFilter();
Subject subject = new Subject();
subject.getPrincipals().add(new PrimaryPrincipal("lmccay"));
subject.getPrincipals().add(new GroupPrincipal("users"));
subject.getPrincipals().add(new GroupPrincipal("admin"));
filter.init(config);
String username = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName());
String[] groups = filter.mapGroupPrincipals(username, subject);
assertEquals("lmccay", username);
// means for the caller to use the existing subject groups
assertNull(groups);
config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(config.getInitParameter("principal.mapping")).andReturn("lmccay,kminder=hdfs;newuser=mapred").anyTimes();
EasyMock.expect(config.getInitParameter("group.principal.mapping")).andReturn("kminder=group1;lmccay=mrgroup,mrducks").anyTimes();
context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();
EasyMock.replay(config);
filter.init(config);
username = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName());
String[] mappedGroups = filter.mapGroupPrincipals(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName(), subject);
assertEquals("hdfs", username);
assertTrue("mrgroup not found in groups: " + mappedGroups, groupFoundIn("mrgroup", mappedGroups));
assertTrue("mrducks not found in groups: " + mappedGroups, groupFoundIn("mrducks", mappedGroups));
assertFalse("group1 WAS found in groups: " + mappedGroups, groupFoundIn("group1", mappedGroups));
subject = new Subject();
subject.getPrincipals().add(new PrimaryPrincipal("kminder"));
subject.getPrincipals().add(new GroupPrincipal("users"));
subject.getPrincipals().add(new GroupPrincipal("admin"));
config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(config.getInitParameter("principal.mapping")).andReturn("lmccay,kminder=hdfs;newuser=mapred").anyTimes();
EasyMock.expect(config.getInitParameter("group.principal.mapping")).andReturn("kminder=group1;lmccay=mrgroup,mrducks").anyTimes();
context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();
EasyMock.replay(config);
filter.init(config);
username = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName());
mappedGroups = filter.mapGroupPrincipals(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName(), subject);
assertEquals("hdfs", username);
assertTrue("group1 not found in groups: " + mappedGroups, groupFoundIn("group1", mappedGroups));
}
use of org.apache.knox.gateway.security.PrimaryPrincipal in project knox by apache.
the class TokenServiceResourceTest method testValidClientCert.
@Test
public void testValidClientCert() throws Exception {
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(context.getInitParameter("knox.token.client.cert.required")).andReturn("true");
EasyMock.expect(context.getInitParameter("knox.token.allowed.principals")).andReturn("CN=localhost, OU=Test, O=Hadoop, L=Test, ST=Test, C=US");
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(request.getServletContext()).andReturn(context).anyTimes();
X509Certificate trustedCertMock = EasyMock.createMock(X509Certificate.class);
EasyMock.expect(trustedCertMock.getSubjectDN()).andReturn(new PrimaryPrincipal("CN=localhost, OU=Test, O=Hadoop, L=Test, ST=Test, C=US")).anyTimes();
ArrayList<X509Certificate> certArrayList = new ArrayList<X509Certificate>();
certArrayList.add(trustedCertMock);
X509Certificate[] certs = {};
EasyMock.expect(request.getAttribute("javax.servlet.request.X509Certificate")).andReturn(certArrayList.toArray(certs)).anyTimes();
Principal principal = EasyMock.createNiceMock(Principal.class);
EasyMock.expect(principal.getName()).andReturn("alice").anyTimes();
EasyMock.expect(request.getUserPrincipal()).andReturn(principal).anyTimes();
GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(context.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services);
JWTokenAuthority authority = new TestJWTokenAuthority(publicKey, privateKey);
EasyMock.expect(services.getService(GatewayServices.TOKEN_SERVICE)).andReturn(authority);
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.expect(response.getWriter()).andReturn(printWriter);
EasyMock.replay(principal, services, context, request, response, trustedCertMock);
TokenResource tr = new TokenResource();
tr.request = request;
tr.response = response;
tr.context = context;
tr.init();
// Issue a token
Response retResponse = tr.doGet();
assertEquals(200, retResponse.getStatus());
// Parse the response
String retString = writer.toString();
String accessToken = getTagValue(retString, "access_token");
assertNotNull(accessToken);
String expiry = getTagValue(retString, "expires_in");
assertNotNull(expiry);
// Verify the token
JWT parsedToken = new JWTToken(accessToken);
assertEquals("alice", parsedToken.getSubject());
assertTrue(authority.verifyToken(parsedToken));
}
use of org.apache.knox.gateway.security.PrimaryPrincipal in project knox by apache.
the class PreAuthFederationFilter method continueChainAsPrincipal.
/**
* Recreate the current Subject based upon the provided mappedPrincipal
* and look for the groups that should be associated with the new Subject.
* Upon finding groups mapped to the principal - add them to the new Subject.
* @param mappedPrincipalName
* @throws ServletException
* @throws IOException
*/
protected void continueChainAsPrincipal(final ServletRequest request, final ServletResponse response, final FilterChain chain, String principal) throws IOException, ServletException {
Subject subject = null;
Principal primaryPrincipal = null;
// do some check to ensure that the extracted identity matches any existing security context
// if not, there is may be someone tampering with the request - consult config to determine
// how we are to handle it
// TODO: make sure that this makes sense with existing sessions or lack thereof
Subject currentSubject = Subject.getSubject(AccessController.getContext());
if (currentSubject != null) {
primaryPrincipal = (PrimaryPrincipal) currentSubject.getPrincipals(PrimaryPrincipal.class).toArray()[0];
if (primaryPrincipal != null) {
if (!primaryPrincipal.getName().equals(principal)) {
}
}
}
subject = new Subject();
subject.getPrincipals().add(primaryPrincipal);
doAs(request, response, chain, subject);
}
Aggregations