1313 */
1414package com .facebook .presto .server .security ;
1515
16+ import com .facebook .airlift .http .server .AuthenticationException ;
1617import com .facebook .presto .security .BasicPrincipal ;
1718import com .facebook .presto .server .MockHttpServletRequest ;
1819import com .facebook .presto .spi .security .AccessDeniedException ;
20+ import com .facebook .presto .spi .security .AuthenticatorNotApplicableException ;
1921import com .facebook .presto .spi .security .PrestoAuthenticator ;
2022import com .facebook .presto .spi .security .PrestoAuthenticatorFactory ;
2123import com .google .common .collect .ImmutableList ;
2527import org .testng .annotations .Test ;
2628
2729import java .security .Principal ;
28- import java .util .List ;
2930import java .util .Map ;
30- import java .util .Optional ;
3131
32- import static com .google .common .collect .ImmutableMap .toImmutableMap ;
33- import static java .util .Collections .list ;
3432import static java .util .Objects .requireNonNull ;
33+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
3534import static org .testng .Assert .assertEquals ;
36- import static org .testng .Assert .assertFalse ;
37- import static org .testng .Assert .assertTrue ;
3835
3936public class TestCustomPrestoAuthenticator
4037{
4138 private static final String TEST_HEADER = "test_header" ;
39+ private static final String TEST_INVALID_HEADER = "test_invalid_header" ;
4240 private static final String TEST_HEADER_VALID_VALUE = "VALID" ;
4341 private static final String TEST_HEADER_INVALID_VALUE = "INVALID" ;
4442 private static final String TEST_FACTORY = "test_factory" ;
@@ -47,15 +45,9 @@ public class TestCustomPrestoAuthenticator
4745
4846 @ Test
4947 public void testPrestoAuthenticator ()
48+ throws Exception
5049 {
51- SecurityConfig mockSecurityConfig = new SecurityConfig ();
52- mockSecurityConfig .setAuthenticationTypes (ImmutableList .of (SecurityConfig .AuthenticationType .CUSTOM ));
53- PrestoAuthenticatorManager prestoAuthenticatorManager = new PrestoAuthenticatorManager (mockSecurityConfig );
54- // Add Test Presto Authenticator Factory
55- prestoAuthenticatorManager .addPrestoAuthenticatorFactory (
56- new TestingPrestoAuthenticatorFactory (
57- TEST_FACTORY ,
58- TEST_HEADER_VALID_VALUE ));
50+ PrestoAuthenticatorManager prestoAuthenticatorManager = getPrestoAuthenticatorManager ();
5951
6052 prestoAuthenticatorManager .loadAuthenticator (TEST_FACTORY );
6153
@@ -65,52 +57,77 @@ public void testPrestoAuthenticator()
6557 TEST_REMOTE_ADDRESS ,
6658 ImmutableMap .of ());
6759
68- Optional <Principal > principal = checkAuthentication (prestoAuthenticatorManager .getAuthenticator (), request );
69- assertTrue (principal .isPresent ());
70- assertEquals (principal .get ().getName (), TEST_USER );
60+ CustomPrestoAuthenticator customPrestoAuthenticator = new CustomPrestoAuthenticator (prestoAuthenticatorManager );
61+ Principal principal = customPrestoAuthenticator .authenticate (request );
62+
63+ assertEquals (principal .getName (), TEST_USER );
64+ }
65+
66+ @ Test (expectedExceptions = AuthenticationException .class , expectedExceptionsMessageRegExp = "Access Denied: Authentication Failed!" )
67+ public void testPrestoAuthenticatorFailedAuthentication ()
68+ throws AuthenticationException
69+ {
70+ PrestoAuthenticatorManager prestoAuthenticatorManager = getPrestoAuthenticatorManager ();
71+
72+ prestoAuthenticatorManager .loadAuthenticator (TEST_FACTORY );
7173
7274 // Test failed authentication
73- request = new MockHttpServletRequest (
75+ HttpServletRequest request = new MockHttpServletRequest (
7476 ImmutableListMultimap .of (TEST_HEADER , TEST_HEADER_INVALID_VALUE + ":" + TEST_USER ),
7577 TEST_REMOTE_ADDRESS ,
7678 ImmutableMap .of ());
7779
78- principal = checkAuthentication (prestoAuthenticatorManager . getAuthenticator (), request );
79- assertFalse ( principal . isPresent () );
80+ CustomPrestoAuthenticator customPrestoAuthenticator = new CustomPrestoAuthenticator (prestoAuthenticatorManager );
81+ customPrestoAuthenticator . authenticate ( request );
8082 }
8183
82- private Optional <Principal > checkAuthentication (PrestoAuthenticator authenticator , HttpServletRequest request )
84+ @ Test
85+ public void testPrestoAuthenticatorNotApplicable ()
8386 {
84- try {
85- // Converting HttpServletRequest to Map<String, String>
86- Map <String , List <String >> headers = getHeadersMap (request );
87+ PrestoAuthenticatorManager prestoAuthenticatorManager = getPrestoAuthenticatorManager ();
8788
88- // Passing the headers Map to the authenticator
89- return Optional .of (authenticator .createAuthenticatedPrincipal (headers ));
90- }
91- catch (AccessDeniedException e ) {
92- return Optional .empty ();
93- }
89+ prestoAuthenticatorManager .loadAuthenticator (TEST_FACTORY );
90+
91+ // Test invalid authenticator
92+ HttpServletRequest request = new MockHttpServletRequest (
93+ ImmutableListMultimap .of (TEST_INVALID_HEADER , TEST_HEADER_VALID_VALUE + ":" + TEST_USER ),
94+ TEST_REMOTE_ADDRESS ,
95+ ImmutableMap .of ());
96+
97+ CustomPrestoAuthenticator customPrestoAuthenticator = new CustomPrestoAuthenticator (prestoAuthenticatorManager );
98+
99+ assertThatThrownBy (() -> customPrestoAuthenticator .authenticate (request ))
100+ .isInstanceOf (AuthenticationException .class )
101+ .hasMessage (null );
94102 }
95103
96- private Map < String , List < String >> getHeadersMap ( HttpServletRequest request )
104+ private static PrestoAuthenticatorManager getPrestoAuthenticatorManager ( )
97105 {
98- return list (request .getHeaderNames ())
99- .stream ()
100- .collect (toImmutableMap (
101- headerName -> headerName ,
102- headerName -> list (request .getHeaders (headerName ))));
106+ SecurityConfig mockSecurityConfig = new SecurityConfig ();
107+ mockSecurityConfig .setAuthenticationTypes (ImmutableList .of (SecurityConfig .AuthenticationType .CUSTOM ));
108+ PrestoAuthenticatorManager prestoAuthenticatorManager = new PrestoAuthenticatorManager (mockSecurityConfig );
109+
110+ // Add Test Presto Authenticator Factory
111+ prestoAuthenticatorManager .addPrestoAuthenticatorFactory (
112+ new TestingPrestoAuthenticatorFactory (
113+ TEST_FACTORY ,
114+ TEST_HEADER ,
115+ TEST_HEADER_VALID_VALUE ));
116+
117+ return prestoAuthenticatorManager ;
103118 }
104119
105120 private static class TestingPrestoAuthenticatorFactory
106121 implements PrestoAuthenticatorFactory
107122 {
108123 private final String name ;
124+ private final String validHeaderName ;
109125 private final String validHeaderValue ;
110126
111- TestingPrestoAuthenticatorFactory (String name , String validHeaderValue )
127+ TestingPrestoAuthenticatorFactory (String name , String validHeaderName , String validHeaderValue )
112128 {
113129 this .name = requireNonNull (name , "name is null" );
130+ this .validHeaderName = requireNonNull (validHeaderName , "validHeaderName is null" );
114131 this .validHeaderValue = requireNonNull (validHeaderValue , "validHeaderValue is null" );
115132 }
116133
@@ -124,8 +141,12 @@ public String getName()
124141 public PrestoAuthenticator create (Map <String , String > config )
125142 {
126143 return (headers ) -> {
127- // TEST_HEADER will have value of the form PART1:PART2
128- String [] header = headers .get (TEST_HEADER ).get (0 ).split (":" );
144+ if (!headers .containsKey (this .validHeaderName )) {
145+ throw new AuthenticatorNotApplicableException ("Invalid authenticator: required headers are missing" );
146+ }
147+
148+ // HEADER will have value of the form PART1:PART2
149+ String [] header = headers .get (this .validHeaderName ).get (0 ).split (":" );
129150
130151 if (header [0 ].equals (this .validHeaderValue )) {
131152 return new BasicPrincipal (header [1 ]);
0 commit comments