-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use regex expression for lb authorization
- Loading branch information
Showing
3 changed files
with
122 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
gateway-ha/src/test/java/io/trino/gateway/ha/security/TestLbAuthorizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package io.trino.gateway.ha.security; | ||
|
||
import io.trino.gateway.ha.config.AuthorizationConfiguration; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Optional; | ||
import java.util.regex.PatternSyntaxException; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
@Slf4j | ||
public class TestLbAuthorizer { | ||
private static final String USER = "username"; | ||
private static LbPrincipal principal; | ||
private static LbAuthorizer authorizer; | ||
private static AuthorizationConfiguration configuration; | ||
private static final String ADMIN_ROLE = "ADMIN"; | ||
private static final String USER_ROLE = "USER"; | ||
private static final String API_ROLE = "API"; | ||
private static final String UNKNOWN_ROLE = "UNKNOWN"; | ||
|
||
private static final String PVFX_DATA_31 = "PVFX_DATA_31"; | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
configuration = new AuthorizationConfiguration(); | ||
} | ||
static void match(String member_of, String conf, String role, boolean expectedToMatch) { | ||
principal = new LbPrincipal(USER, Optional.of(member_of)); | ||
if (role.equalsIgnoreCase(ADMIN_ROLE)){ | ||
configuration.setAdmin(conf); | ||
authorizer = new LbAuthorizer(configuration); | ||
} | ||
|
||
if (role.equalsIgnoreCase(USER_ROLE)) { | ||
configuration.setUser(conf); | ||
authorizer = new LbAuthorizer(configuration); | ||
} | ||
if (role.equalsIgnoreCase(API_ROLE)) { | ||
configuration.setApi(conf); | ||
authorizer = new LbAuthorizer(configuration); | ||
} | ||
// UNKNOWN ROLE should always return FALSE | ||
|
||
if (expectedToMatch) { | ||
assertTrue(authorizer.authorize(principal, role)); | ||
} else { | ||
assertFalse(authorizer.authorize(principal, role)); | ||
} | ||
} | ||
|
||
static void assertMatch(String member_of, String conf, String role) { | ||
match(member_of, conf, role, true); | ||
} | ||
static void assertNotMatch(String member_of, String conf, String role) { | ||
match(member_of, conf, role, false); | ||
} | ||
|
||
static void assertBadPattern(String member_of, String conf, String role) { | ||
log.info("Compile bad pattern {}", conf); | ||
try { | ||
match(member_of, conf, role, false); | ||
} catch (PatternSyntaxException e) { | ||
log.info("Failed to compile ==> OKAY"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testBasic() throws Exception { | ||
assertMatch(PVFX_DATA_31, PVFX_DATA_31, ADMIN_ROLE); | ||
assertNotMatch(PVFX_DATA_31, PVFX_DATA_31, UNKNOWN_ROLE); | ||
assertNotMatch(PVFX_DATA_31, "PVFX", USER_ROLE); | ||
assertNotMatch(PVFX_DATA_31, "DATA", API_ROLE); | ||
assertNotMatch(PVFX_DATA_31, "31", ADMIN_ROLE); | ||
} | ||
|
||
@Test | ||
public void testZeroOrMoreCharacters() throws Exception { | ||
assertMatch(PVFX_DATA_31, "PVFX(.*)", ADMIN_ROLE); | ||
assertMatch(PVFX_DATA_31, "(?i)pvfx(.*)", USER_ROLE); | ||
assertMatch(PVFX_DATA_31, "(.*)", API_ROLE); | ||
assertMatch(PVFX_DATA_31, "PVFX_DATA_31(.*)", ADMIN_ROLE); | ||
assertMatch(PVFX_DATA_31, "(.*)_31", USER_ROLE); | ||
assertMatch(PVFX_DATA_31, "(.*)DATA(.*)", API_ROLE); | ||
assertMatch(PVFX_DATA_31, "^.+$", ADMIN_ROLE); | ||
assertNotMatch(PVFX_DATA_31, "^.+$", UNKNOWN_ROLE); | ||
assertNotMatch(PVFX_DATA_31, "(.*)DATA", USER_ROLE); | ||
assertNotMatch(PVFX_DATA_31, "PVFX__(.*)", API_ROLE); | ||
} | ||
|
||
@Test | ||
public void testBadPatterns() throws Exception { | ||
assertBadPattern(PVFX_DATA_31, "^[a-zA--Z]+$", ADMIN_ROLE); // bad range | ||
assertBadPattern(PVFX_DATA_31, "^[a-zA-Z+$", USER_ROLE); // missing ] | ||
assertBadPattern(PVFX_DATA_31, "^[a-zA-Z]+$\\", API_ROLE); // nothing to escape | ||
} | ||
} |