Search in sources :

Example 1 with PasswordAuthenticator

use of io.trino.spi.security.PasswordAuthenticator in project trino by trinodb.

the class PasswordAuthenticatorManager method loadAuthenticator.

private PasswordAuthenticator loadAuthenticator(File configFile) {
    Map<String, String> properties;
    try {
        properties = new HashMap<>(loadPropertiesFrom(configFile.getPath()));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    String name = properties.remove(NAME_PROPERTY);
    checkState(!isNullOrEmpty(name), "Password authenticator configuration %s does not contain '%s'", configFile, NAME_PROPERTY);
    log.info("-- Loading password authenticator --");
    PasswordAuthenticatorFactory factory = factories.get(name);
    checkState(factory != null, "Password authenticator '%s' is not registered", name);
    PasswordAuthenticator authenticator;
    try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(factory.getClass().getClassLoader())) {
        authenticator = factory.create(ImmutableMap.copyOf(properties));
    }
    log.info("-- Loaded password authenticator %s --", name);
    return authenticator;
}
Also used : PasswordAuthenticatorFactory(io.trino.spi.security.PasswordAuthenticatorFactory) PasswordAuthenticator(io.trino.spi.security.PasswordAuthenticator) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ThreadContextClassLoader(io.trino.spi.classloader.ThreadContextClassLoader)

Example 2 with PasswordAuthenticator

use of io.trino.spi.security.PasswordAuthenticator in project trino by trinodb.

the class TestPasswordAuthenticatorManager method testMultipleConfigFiles.

@Test
public void testMultipleConfigFiles() throws Exception {
    Path config1 = createTempFile("passwordConfig", "1");
    Path config2 = createTempFile("passwordConfig", "2");
    Files.write(config1, ImmutableList.of("password-authenticator.name=type1"));
    Files.write(config2, ImmutableList.of("password-authenticator.name=type2"));
    PasswordAuthenticatorManager manager = new PasswordAuthenticatorManager(new PasswordAuthenticatorConfig().setPasswordAuthenticatorFiles(config1.toAbsolutePath() + "," + config2.toAbsolutePath()));
    manager.setRequired();
    manager.addPasswordAuthenticatorFactory(new TestingPasswordAuthenticatorFactory("type1", "password1"));
    manager.addPasswordAuthenticatorFactory(new TestingPasswordAuthenticatorFactory("type2", "password2"));
    manager.loadPasswordAuthenticator();
    List<PasswordAuthenticator> authenticators = manager.getAuthenticators();
    assertThat(login(authenticators, "password1")).isTrue();
    assertThat(login(authenticators, "password2")).isTrue();
    assertThat(login(authenticators, "wrong_password")).isFalse();
}
Also used : Path(java.nio.file.Path) PasswordAuthenticator(io.trino.spi.security.PasswordAuthenticator) Test(org.testng.annotations.Test)

Example 3 with PasswordAuthenticator

use of io.trino.spi.security.PasswordAuthenticator in project trino by trinodb.

the class PasswordManagerFormAuthenticator method isValidCredential.

@Override
public Optional<String> isValidCredential(String username, String password, boolean secure) {
    if (username == null) {
        return Optional.empty();
    }
    if (!secure) {
        return Optional.of(username).filter(user -> insecureAuthenticationOverHttpAllowed && password == null);
    }
    List<PasswordAuthenticator> authenticators = passwordAuthenticatorManager.getAuthenticators();
    for (PasswordAuthenticator authenticator : authenticators) {
        try {
            Principal principal = authenticator.createAuthenticatedPrincipal(username, password);
            String authenticatedUser = userMapping.mapUser(principal.toString());
            return Optional.of(authenticatedUser);
        } catch (AccessDeniedException | UserMappingException e) {
        // Try another one
        } catch (RuntimeException e) {
            log.debug(e, "Error authenticating user for Web UI");
        }
    }
    return Optional.empty();
}
Also used : AccessDeniedException(io.trino.spi.security.AccessDeniedException) PasswordAuthenticator(io.trino.spi.security.PasswordAuthenticator) UserMappingException(io.trino.server.security.UserMappingException) Principal(java.security.Principal)

Example 4 with PasswordAuthenticator

use of io.trino.spi.security.PasswordAuthenticator in project trino by trinodb.

the class PasswordAuthenticatorManager method loadPasswordAuthenticator.

public void loadPasswordAuthenticator() {
    if (!required.get()) {
        return;
    }
    ImmutableList.Builder<PasswordAuthenticator> authenticators = ImmutableList.builder();
    for (File configFile : configFiles) {
        authenticators.add(loadAuthenticator(configFile.getAbsoluteFile()));
    }
    this.authenticators.set(authenticators.build());
}
Also used : PasswordAuthenticator(io.trino.spi.security.PasswordAuthenticator) ImmutableList(com.google.common.collect.ImmutableList) File(java.io.File)

Aggregations

PasswordAuthenticator (io.trino.spi.security.PasswordAuthenticator)4 ImmutableList (com.google.common.collect.ImmutableList)1 UserMappingException (io.trino.server.security.UserMappingException)1 ThreadContextClassLoader (io.trino.spi.classloader.ThreadContextClassLoader)1 AccessDeniedException (io.trino.spi.security.AccessDeniedException)1 PasswordAuthenticatorFactory (io.trino.spi.security.PasswordAuthenticatorFactory)1 File (java.io.File)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Path (java.nio.file.Path)1 Principal (java.security.Principal)1 Test (org.testng.annotations.Test)1