Skip to content
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

Optimize by avoid creating wildcard matchers for every request #902

Merged
merged 3 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -1100,47 +1100,52 @@ public Map<String, Boolean> mapTenants(final User user, Set<String> roles) {
private class RoleMappingHolder {

private ListMultimap<String, String> users;
private ListMultimap<Set<String>, String> abars;
private ListMultimap<Collection<WildcardMatcher>, String> abars;
sujithvm marked this conversation as resolved.
Show resolved Hide resolved
private ListMultimap<String, String> bars;
private ListMultimap<String, String> hosts;
private final String hostResolverMode;

private Collection<WildcardMatcher> userMatchers;
private Collection<WildcardMatcher> barMatchers;
private Collection<WildcardMatcher> hostMatchers;

private RoleMappingHolder(final SecurityDynamicConfiguration<RoleMappingsV6> rolesMapping, final String hostResolverMode) {

this.hostResolverMode = hostResolverMode;

if (rolesMapping != null) {

final ListMultimap<String, String> users_ = ArrayListMultimap.create();
final ListMultimap<Set<String>, String> abars_ = ArrayListMultimap.create();
final ListMultimap<String, String> bars_ = ArrayListMultimap.create();
final ListMultimap<String, String> hosts_ = ArrayListMultimap.create();
users = ArrayListMultimap.create();
abars = ArrayListMultimap.create();
bars = ArrayListMultimap.create();
hosts = ArrayListMultimap.create();

for (final Entry<String, RoleMappingsV6> roleMap : rolesMapping.getCEntries().entrySet()) {
final String roleMapKey = roleMap.getKey();
final RoleMappingsV6 roleMapValue = roleMap.getValue();

for (String u : roleMap.getValue().getUsers()) {
users_.put(u, roleMap.getKey());
for (String u : roleMapValue.getUsers()) {
users.put(u, roleMapKey);
}

final Set<String> abar = new HashSet<String>(roleMap.getValue().getAndBackendroles());
final Set<String> abar = new HashSet<>(roleMapValue.getAndBackendroles());

if (!abar.isEmpty()) {
abars_.put(abar, roleMap.getKey());
abars.put(WildcardMatcher.matchers(abar), roleMapKey);
}

for (String bar : roleMap.getValue().getBackendroles()) {
bars_.put(bar, roleMap.getKey());
for (String bar : roleMapValue.getBackendroles()) {
bars.put(bar, roleMapKey);
}

for (String host : roleMap.getValue().getHosts()) {
hosts_.put(host, roleMap.getKey());
for (String host : roleMapValue.getHosts()) {
hosts.put(host, roleMapKey);
}
}

users = users_;
abars = abars_;
bars = bars_;
hosts = hosts_;
userMatchers = WildcardMatcher.matchers(users.keySet());
barMatchers = WildcardMatcher.matchers(bars.keySet());
hostMatchers = WildcardMatcher.matchers(hosts.keySet());
}
}

Expand All @@ -1150,7 +1155,7 @@ private Set<String> map(final User user, final TransportAddress caller) {
return Collections.emptySet();
}

final Set<String> securityRoles = new TreeSet<String>();
final Set<String> securityRoles = new HashSet<>();

if (rolesMappingResolution == ConfigConstants.RolesMappingResolution.BOTH
|| rolesMappingResolution == ConfigConstants.RolesMappingResolution.BACKENDROLES_ONLY) {
Expand All @@ -1163,16 +1168,16 @@ private Set<String> map(final User user, final TransportAddress caller) {
if (((rolesMappingResolution == ConfigConstants.RolesMappingResolution.BOTH
|| rolesMappingResolution == ConfigConstants.RolesMappingResolution.MAPPING_ONLY))) {

for (String p : WildcardMatcher.getAllMatchingPatterns(WildcardMatcher.matchers(users.keySet()), user.getName())) {
for (String p : WildcardMatcher.getAllMatchingPatterns(userMatchers, user.getName())) {
securityRoles.addAll(users.get(p));
}

for (String p : WildcardMatcher.getAllMatchingPatterns(WildcardMatcher.matchers(bars.keySet()), user.getRoles())) {
for (String p : WildcardMatcher.getAllMatchingPatterns(barMatchers, user.getRoles())) {
securityRoles.addAll(bars.get(p));
}

for (Set<String> patterns : abars.keySet()) {
if (patterns.stream().allMatch(p -> WildcardMatcher.from(p).matchAny(user.getRoles()))) {
for (Collection<WildcardMatcher> patterns : abars.keySet()) {
if (patterns.stream().allMatch(p -> p.matchAny(user.getRoles()))) {
securityRoles.addAll(abars.get(patterns));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1074,47 +1074,52 @@ public Map<String, Boolean> mapTenants(final User user, Set<String> roles) {
private class RoleMappingHolder {

private ListMultimap<String, String> users;
private ListMultimap<Set<String>, String> abars;
private ListMultimap<Collection<WildcardMatcher>, String> abars;
private ListMultimap<String, String> bars;
private ListMultimap<String, String> hosts;
private final String hostResolverMode;

private Collection<WildcardMatcher> userMatchers;
private Collection<WildcardMatcher> barMatchers;
private Collection<WildcardMatcher> hostMatchers;

private RoleMappingHolder(final SecurityDynamicConfiguration<RoleMappingsV7> rolemappings, final String hostResolverMode) {

this.hostResolverMode = hostResolverMode;

if (roles != null) {

final ListMultimap<String, String> users_ = ArrayListMultimap.create();
final ListMultimap<Set<String>, String> abars_ = ArrayListMultimap.create();
final ListMultimap<String, String> bars_ = ArrayListMultimap.create();
final ListMultimap<String, String> hosts_ = ArrayListMultimap.create();
users = ArrayListMultimap.create();
abars = ArrayListMultimap.create();
bars = ArrayListMultimap.create();
hosts = ArrayListMultimap.create();

for (final Entry<String, RoleMappingsV7> roleMap : rolemappings.getCEntries().entrySet()) {
final String roleMapKey = roleMap.getKey();
final RoleMappingsV7 roleMapValue = roleMap.getValue();

for (String u : roleMap.getValue().getUsers()) {
users_.put(u, roleMap.getKey());
for (String u : roleMapValue.getUsers()) {
users.put(u, roleMapKey);
}

final Set<String> abar = new HashSet<String>(roleMap.getValue().getAnd_backend_roles());
final Set<String> abar = new HashSet<>(roleMapValue.getAnd_backend_roles());

if (!abar.isEmpty()) {
abars_.put(abar, roleMap.getKey());
abars.put(WildcardMatcher.matchers(abar), roleMapKey);
}

for (String bar : roleMap.getValue().getBackend_roles()) {
bars_.put(bar, roleMap.getKey());
for (String bar : roleMapValue.getBackend_roles()) {
bars.put(bar, roleMapKey);
}

for (String host : roleMap.getValue().getHosts()) {
hosts_.put(host, roleMap.getKey());
for (String host : roleMapValue.getHosts()) {
hosts.put(host, roleMapKey);
}
}

users = users_;
abars = abars_;
bars = bars_;
hosts = hosts_;
userMatchers = WildcardMatcher.matchers(users.keySet());
barMatchers = WildcardMatcher.matchers(bars.keySet());
hostMatchers = WildcardMatcher.matchers(hosts.keySet());
}
}

Expand All @@ -1124,7 +1129,7 @@ private Set<String> map(final User user, final TransportAddress caller) {
return Collections.emptySet();
}

final Set<String> securityRoles = new TreeSet<String>(user.getOpenDistroSecurityRoles());
final Set<String> securityRoles = new HashSet<>(user.getOpenDistroSecurityRoles());

if (rolesMappingResolution == ConfigConstants.RolesMappingResolution.BOTH
|| rolesMappingResolution == ConfigConstants.RolesMappingResolution.BACKENDROLES_ONLY) {
Expand All @@ -1137,15 +1142,15 @@ private Set<String> map(final User user, final TransportAddress caller) {
if (((rolesMappingResolution == ConfigConstants.RolesMappingResolution.BOTH
|| rolesMappingResolution == ConfigConstants.RolesMappingResolution.MAPPING_ONLY))) {

for (String p : WildcardMatcher.getAllMatchingPatterns(WildcardMatcher.matchers(users.keySet()), user.getName())) {
for (String p : WildcardMatcher.getAllMatchingPatterns(userMatchers, user.getName())) {
securityRoles.addAll(users.get(p));
}
for (String p : WildcardMatcher.getAllMatchingPatterns(WildcardMatcher.matchers(bars.keySet()), user.getRoles())) {
for (String p : WildcardMatcher.getAllMatchingPatterns(barMatchers, user.getRoles())) {
securityRoles.addAll(bars.get(p));
}

for (Set<String> patterns : abars.keySet()) {
if (patterns.stream().allMatch(p -> WildcardMatcher.from(p).matchAny(user.getRoles()))) {
for (Collection<WildcardMatcher> patterns : abars.keySet()) {
if (patterns.stream().allMatch(p -> p.matchAny(user.getRoles()))) {
securityRoles.addAll(abars.get(patterns));
}
}
Expand All @@ -1154,7 +1159,6 @@ private Set<String> map(final User user, final TransportAddress caller) {
//IPV4 or IPv6 (compressed and without scope identifiers)
final String ipAddress = caller.getAddress();

List<WildcardMatcher> hostMatchers = WildcardMatcher.matchers(hosts.keySet());
for (String p : WildcardMatcher.getAllMatchingPatterns(hostMatchers, ipAddress)) {
securityRoles.addAll(hosts.get(p));
}
Expand Down