use of org.apache.knox.gateway.security.PrimaryPrincipal in project knox by apache.
the class RegexIdentityAssertionFilterTest method testExtractUsernameFromEmail.
@Test
public void testExtractUsernameFromEmail() 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);
RegexIdentityAssertionFilter filter = new RegexIdentityAssertionFilter();
Subject subject = new Subject();
subject.getPrincipals().add(new PrimaryPrincipal("member@us.apache.org"));
subject.getPrincipals().add(new GroupPrincipal("user"));
subject.getPrincipals().add(new GroupPrincipal("admin"));
// First test is with no config. Since the output template is the empty string that should be the result.
filter.init(config);
String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName());
String[] groups = filter.mapGroupPrincipals(actual, subject);
assertThat(actual, is(""));
// means for the caller to use the existing subject groups
assertThat(groups, is(nullValue()));
// Test what is effectively a static mapping
config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(config.getInitParameter("principal.mapping")).andReturn("").anyTimes();
context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();
EasyMock.expect(context.getInitParameter("principal.mapping")).andReturn("").anyTimes();
EasyMock.expect(config.getInitParameter("output")).andReturn("test-output").anyTimes();
EasyMock.replay(config);
EasyMock.replay(context);
filter.init(config);
actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName());
assertEquals(actual, "test-output");
// Test username extraction.
config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(config.getInitParameter("principal.mapping")).andReturn("").anyTimes();
context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();
EasyMock.expect(context.getInitParameter("principal.mapping")).andReturn("").anyTimes();
EasyMock.expect(config.getInitParameter("input")).andReturn("(.*)@.*").anyTimes();
EasyMock.expect(config.getInitParameter("output")).andReturn("prefix_{1}_suffix").anyTimes();
EasyMock.replay(config);
EasyMock.replay(context);
filter.init(config);
actual = filter.mapUserPrincipal("member@us.apache.org");
assertEquals(actual, "prefix_member_suffix");
}
use of org.apache.knox.gateway.security.PrimaryPrincipal in project knox by apache.
the class SwitchCaseIdentityAssertionFilterTest method testDefaultGroupsConfFromUsers.
@Test
public void testDefaultGroupsConfFromUsers() 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(null).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 testNone.
@Test
public void testNone() throws Exception {
FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(config.getInitParameter("principal.case")).andReturn("none").anyTimes();
EasyMock.expect(config.getInitParameter("group.principal.case")).andReturn("none").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(nullValue()));
}
use of org.apache.knox.gateway.security.PrimaryPrincipal in project knox by apache.
the class SwitchCaseIdentityAssertionFilterTest method testDefaultGroupsConfOverride.
@Test
public void testDefaultGroupsConfOverride() 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("none").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(nullValue()));
}
use of org.apache.knox.gateway.security.PrimaryPrincipal in project knox by apache.
the class UsernameFunctionProcessorTest method testResolve.
@Test
public void testResolve() throws Exception {
final UsernameFunctionProcessor processor = new UsernameFunctionProcessor();
assertThat(processor.resolve(null, null), nullValue());
assertThat(processor.resolve(null, Arrays.asList("test-input")), contains("test-input"));
Subject subject = new Subject();
subject.getPrincipals().add(new PrimaryPrincipal("test-username"));
subject.setReadOnly();
Subject.doAs(subject, new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
assertThat(processor.resolve(null, null), contains("test-username"));
assertThat(processor.resolve(null, Arrays.asList("test-ignored")), contains("test-username"));
return null;
}
});
}
Aggregations