Skip to content

Added group name case conversion #26195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.trino.spi.classloader.ThreadContextClassLoader;
import io.trino.spi.security.GroupProvider;
import io.trino.spi.security.GroupProviderFactory;
import io.trino.util.Case;

import java.io.File;
import java.io.IOException;
Expand All @@ -36,18 +37,24 @@
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.isNullOrEmpty;
import static io.airlift.configuration.ConfigurationLoader.loadPropertiesFrom;
import static io.trino.util.Case.KEEP;
import static java.lang.String.format;
import static java.util.Arrays.stream;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toSet;

public class GroupProviderManager
implements GroupProvider
{
private static final Logger log = Logger.get(GroupProviderManager.class);
private static final File GROUP_PROVIDER_CONFIGURATION = new File("etc/group-provider.properties");
private static final String GROUP_PROVIDER_PROPERTY_NAME = "group-provider.name";
private static final String GROUP_PROVIDER_PROPERTY_GROUP_CASE = "group-provider.group-case";
private final Map<String, GroupProviderFactory> groupProviderFactories = new ConcurrentHashMap<>();
private final AtomicReference<Optional<GroupProvider>> configuredGroupProvider = new AtomicReference<>(Optional.empty());
private final SecretsResolver secretsResolver;
private Case groupCase = KEEP;

@Inject
public GroupProviderManager(SecretsResolver secretsResolver)
Expand Down Expand Up @@ -83,6 +90,19 @@ void loadConfiguredGroupProvider(File groupProviderFile)
checkArgument(!isNullOrEmpty(groupProviderName),
"Group provider configuration %s does not contain %s", groupProviderFile.getAbsoluteFile(), GROUP_PROVIDER_PROPERTY_NAME);

final String groupCase = properties.remove(GROUP_PROVIDER_PROPERTY_GROUP_CASE);
if (groupCase != null) {
this.groupCase = stream(Case.values())
.map(Case::toString)
.filter(groupCase::equalsIgnoreCase)
.map(Case::valueOf)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(format("Group provider configuration %s does not contain valid %s. Expected one of: %s",
groupProviderFile.getAbsoluteFile(),
GROUP_PROVIDER_PROPERTY_GROUP_CASE,
stream(Case.values()).map(Case::toString).collect(joining(", ", "[", "]")))));
}

setConfiguredGroupProvider(groupProviderName, properties);
}

Expand Down Expand Up @@ -119,7 +139,7 @@ public Set<String> getGroups(String user)
requireNonNull(user, "user is null");
return configuredGroupProvider.get()
.map(provider -> provider.getGroups(user))
.map(ImmutableSet::copyOf)
.map(groups -> groups.stream().map(this.groupCase::transform).collect(toSet()))
.orElse(ImmutableSet.of());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import io.trino.util.Case;

import java.io.File;
import java.util.List;
Expand All @@ -26,9 +27,8 @@

import static com.google.common.base.Preconditions.checkArgument;
import static io.trino.plugin.base.util.JsonUtils.parseJson;
import static io.trino.server.security.UserMapping.Case.KEEP;
import static io.trino.util.Case.KEEP;
import static java.lang.Boolean.TRUE;
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;

public final class UserMapping
Expand Down Expand Up @@ -86,33 +86,6 @@ public List<Rule> getRules()
}
}

enum Case
{
KEEP {
@Override
public String transform(String value)
{
return value;
}
},
LOWER {
@Override
public String transform(String value)
{
return value.toLowerCase(ENGLISH);
}
},
UPPER {
@Override
public String transform(String value)
{
return value.toUpperCase(ENGLISH);
}
};

public abstract String transform(String value);
}

public static final class Rule
{
private final Pattern pattern;
Expand Down
43 changes: 43 additions & 0 deletions core/trino-main/src/main/java/io/trino/util/Case.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.util;

import static java.util.Locale.ROOT;

public enum Case
{
KEEP {
@Override
public String transform(String value)
{
return value;
}
},
LOWER {
@Override
public String transform(String value)
{
return value.toLowerCase(ROOT);
}
},
UPPER {
@Override
public String transform(String value)
{
return value.toUpperCase(ROOT);
}
};

public abstract String transform(String value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
import io.trino.spi.security.GroupProvider;
import io.trino.spi.security.GroupProviderFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;
import java.nio.file.Files;
import java.util.Map;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TestGroupProviderManager
{
Expand All @@ -46,17 +49,78 @@ public GroupProvider create(Map<String, String> config)
}
};

@Test
public void testGroupProviderIsLoaded()
@ParameterizedTest
@ValueSource(strings = {"", "group-provider.group-case=keep", "group-provider.group-case=KEEP"})
public void testGroupProviderIsLoaded(String additional)
throws IOException
{
try (TempFile tempFile = new TempFile()) {
Files.write(tempFile.path(), "group-provider.name=testGroupProvider".getBytes(UTF_8));
Files.writeString(tempFile.path(), """
group-provider.name=testGroupProvider
""" + additional);

GroupProviderManager groupProviderManager = new GroupProviderManager(new SecretsResolver(ImmutableMap.of()));
groupProviderManager.addGroupProviderFactory(TEST_GROUP_PROVIDER_FACTORY);
groupProviderManager.loadConfiguredGroupProvider(tempFile.file());

assertThat(groupProviderManager.getGroups("Alice")).isEqualTo(ImmutableSet.of("test", "Alice"));
assertThat(groupProviderManager.getGroups("Bob")).isEqualTo(ImmutableSet.of("test", "Bob"));
}
}

@Test
void setTestGroupProviderUpperCase() throws Exception
{
try (TempFile tempFile = new TempFile()) {
Files.writeString(tempFile.path(), """
group-provider.name=testGroupProvider
group-provider.group-case=upper
""");

GroupProviderManager groupProviderManager = new GroupProviderManager(new SecretsResolver(ImmutableMap.of()));
groupProviderManager.addGroupProviderFactory(TEST_GROUP_PROVIDER_FACTORY);
groupProviderManager.loadConfiguredGroupProvider(tempFile.file());
assertThat(groupProviderManager.getGroups("alice")).isEqualTo(ImmutableSet.of("test", "alice"));
assertThat(groupProviderManager.getGroups("bob")).isEqualTo(ImmutableSet.of("test", "bob"));

assertThat(groupProviderManager.getGroups("Alice")).isEqualTo(ImmutableSet.of("TEST", "ALICE"));
assertThat(groupProviderManager.getGroups("Bob")).isEqualTo(ImmutableSet.of("TEST", "BOB"));
}
}

@Test
void setTestGroupProviderLowerCase() throws Exception
{
try (TempFile tempFile = new TempFile()) {
Files.writeString(tempFile.path(), """
group-provider.name=testGroupProvider
group-provider.group-case=lower
""");

GroupProviderManager groupProviderManager = new GroupProviderManager(new SecretsResolver(ImmutableMap.of()));
groupProviderManager.addGroupProviderFactory(TEST_GROUP_PROVIDER_FACTORY);
groupProviderManager.loadConfiguredGroupProvider(tempFile.file());

assertThat(groupProviderManager.getGroups("Alice")).isEqualTo(ImmutableSet.of("test", "alice"));
assertThat(groupProviderManager.getGroups("Bob")).isEqualTo(ImmutableSet.of("test", "bob"));
}
}

@Test
void setTestGroupProviderInvalidCase() throws Exception
{
try (TempFile tempFile = new TempFile()) {
Files.writeString(tempFile.path(), """
group-provider.name=testGroupProvider
group-provider.group-case=invalid
""");

GroupProviderManager groupProviderManager = new GroupProviderManager(new SecretsResolver(ImmutableMap.of()));
groupProviderManager.addGroupProviderFactory(TEST_GROUP_PROVIDER_FACTORY);

assertThatThrownBy(() -> groupProviderManager.loadConfiguredGroupProvider(tempFile.file()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(format(
"Group provider configuration %s does not contain valid group-provider.group-case. Expected one of: [KEEP, LOWER, UPPER]",
tempFile.path().toAbsolutePath()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import java.net.URISyntaxException;
import java.util.Optional;

import static io.trino.server.security.UserMapping.Case.KEEP;
import static io.trino.server.security.UserMapping.createUserMapping;
import static io.trino.util.Case.KEEP;
import static io.trino.util.Case.LOWER;
import static io.trino.util.Case.UPPER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

Expand Down Expand Up @@ -121,15 +123,15 @@ public void testMultipleRule()
public void testLowercaseUsernameRule()
throws UserMappingException
{
UserMapping userMapping = new UserMapping(ImmutableList.of(new Rule("(.*)@EXAMPLE\\.COM", "$1", true, UserMapping.Case.LOWER)));
UserMapping userMapping = new UserMapping(ImmutableList.of(new Rule("(.*)@EXAMPLE\\.COM", "$1", true, LOWER)));
assertThat(userMapping.mapUser("TEST@EXAMPLE.COM")).isEqualTo("test");
}

@Test
public void testUppercaseUsernameRule()
throws UserMappingException
{
UserMapping userMapping = new UserMapping(ImmutableList.of(new Rule("(.*)@example\\.com", "$1", true, UserMapping.Case.UPPER)));
UserMapping userMapping = new UserMapping(ImmutableList.of(new Rule("(.*)@example\\.com", "$1", true, UPPER)));
assertThat(userMapping.mapUser("test@example.com")).isEqualTo("TEST");
}

Expand Down
10 changes: 7 additions & 3 deletions docs/src/main/sphinx/develop/group-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ must be wrapped as a Trino plugin and installed on the cluster.

After a plugin that implements `GroupProviderFactory` has been installed on the coordinator,
it is configured using an `etc/group-provider.properties` file.
All the properties other than `group-provider.name` are specific to
All the properties other than `group-provider.name` and `group-provider.group-case` are specific to
the `GroupProviderFactory` implementation.

The `group-provider.name` property is used by Trino to find a registered
* The `group-provider.name` property is used by Trino to find a registered
`GroupProviderFactory` based on the name returned by `GroupProviderFactory.getName()`.
The remaining properties are passed as a map to
* The `group-provider.group-case` property can be used to transform the case of groups. Allowed values
are `[keep, upper, lower]`. This property is not mandatory and the default is `keep`, which does not
transform the group name.
* The remaining properties are passed as a map to
`GroupProviderFactory.create(Map<String, String>)`.

Example configuration file:

```text
group-provider.name=custom-group-provider
group-provider.group-case=keep
custom-property1=custom-value1
custom-property2=custom-value2
```
Expand Down