Skip to content

Commit

Permalink
Rebase fix, applied spotless
Browse files Browse the repository at this point in the history
Signed-off-by: Nils Bandener <nils.bandener@eliatra.com>
  • Loading branch information
nibix committed Jul 12, 2024
1 parent 45eba2a commit 6bb6de4
Show file tree
Hide file tree
Showing 20 changed files with 162 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@

package org.opensearch.security.configuration;

import java.util.Map;

import org.opensearch.security.securityconf.impl.CType;
import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration;

/**
* Callback function on change particular configuration
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ boolean isAuditConfigDocPresentInIndex() {
return isAuditConfigDocPresentInIndex.get();
}

ConfigurationMap load(final CType<?>[] events, long timeout, TimeUnit timeUnit, boolean acceptInvalid)
throws InterruptedException, TimeoutException {
ConfigurationMap load(final CType<?>[] events, long timeout, TimeUnit timeUnit, boolean acceptInvalid) throws InterruptedException,
TimeoutException {
final CountDownLatch latch = new CountDownLatch(events.length);
ConfigurationMap.Builder result = new ConfigurationMap.Builder();
final boolean isDebugEnabled = log.isDebugEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

package org.opensearch.security.configuration;

import java.util.Set;

import com.google.common.collect.ImmutableMap;

import org.opensearch.security.securityconf.impl.CType;
import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration;

import java.util.Set;

/**
* Allows type safe access of configuration instances via the configuration type
*/
Expand Down Expand Up @@ -60,7 +61,7 @@ public ImmutableMap<CType<?>, SecurityDynamicConfiguration<?>> rawMap() {
return this.map;
}

public static ConfigurationMap of(SecurityDynamicConfiguration<?> ... configs) {
public static ConfigurationMap of(SecurityDynamicConfiguration<?>... configs) {
Builder builder = new Builder();

for (SecurityDynamicConfiguration<?> config : configs) {
Expand All @@ -73,8 +74,7 @@ public static ConfigurationMap of(SecurityDynamicConfiguration<?> ... configs) {
public static class Builder {
private ImmutableMap.Builder<CType<?>, SecurityDynamicConfiguration<?>> map = new ImmutableMap.Builder<>();

public Builder() {
}
public Builder() {}

public <T> Builder with(SecurityDynamicConfiguration<T> config) {
map.put(config.getCType(), config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -585,10 +584,7 @@ private synchronized void notifyAboutChanges(ConfigurationMap typeToConfig) {
* @param logComplianceEvent
* @return
*/
public ConfigurationMap getConfigurationsFromIndex(
Collection<CType<?>> configTypes,
boolean logComplianceEvent
) {
public ConfigurationMap getConfigurationsFromIndex(Collection<CType<?>> configTypes, boolean logComplianceEvent) {
return getConfigurationsFromIndex(configTypes, logComplianceEvent, this.acceptInvalid);
}

Expand Down Expand Up @@ -641,8 +637,7 @@ public ConfigurationMap getConfigurationsFromIndex(
return result;
}

private ConfigurationMap validate(ConfigurationMap conf, int expectedSize)
throws InvalidConfigException {
private ConfigurationMap validate(ConfigurationMap conf, int expectedSize) throws InvalidConfigException {

if (conf == null || conf.size() != expectedSize) {
throw new InvalidConfigException("Retrieved only partial configuration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@
import org.opensearch.security.securityconf.impl.NodesDn;
import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration;
import org.opensearch.security.securityconf.impl.WhitelistingSettings;
import org.opensearch.security.securityconf.impl.v6.ActionGroupsV6;
import org.opensearch.security.securityconf.impl.v6.ConfigV6;
import org.opensearch.security.securityconf.impl.v6.InternalUserV6;
import org.opensearch.security.securityconf.impl.v6.RoleMappingsV6;
import org.opensearch.security.securityconf.impl.v6.RoleV6;
import org.opensearch.security.securityconf.impl.v7.ActionGroupsV7;
import org.opensearch.security.securityconf.impl.v7.ConfigV7;
import org.opensearch.security.securityconf.impl.v7.InternalUserV7;
Expand Down Expand Up @@ -239,55 +236,44 @@ public void onChange(ConfigurationMap typeToConfig) {
final AllowlistingSettings allowlist = cr.getConfiguration(CType.ALLOWLIST).getCEntry("config");
final AuditConfig audit = cr.getConfiguration(CType.AUDIT).getCEntry("config");

if (roles.containsAny(staticRoles)) {
throw new StaticResourceException("Cannot override static roles");
}
if (!roles.add(staticRoles) && !staticRoles.getCEntries().isEmpty()) {
throw new StaticResourceException("Unable to load static roles");
}
if (roles.containsAny(staticRoles)) {
throw new StaticResourceException("Cannot override static roles");
}
if (!roles.add(staticRoles) && !staticRoles.getCEntries().isEmpty()) {
throw new StaticResourceException("Unable to load static roles");
}

log.debug("Static roles loaded ({})", staticRoles.getCEntries().size());
log.debug("Static roles loaded ({})", staticRoles.getCEntries().size());

if (actionGroups.containsAny(staticActionGroups)) {
throw new StaticResourceException("Cannot override static action groups");
}
if (!actionGroups.add(staticActionGroups) && !staticActionGroups.getCEntries().isEmpty()) {
throw new StaticResourceException("Unable to load static action groups");
}
if (actionGroups.containsAny(staticActionGroups)) {
throw new StaticResourceException("Cannot override static action groups");
}
if (!actionGroups.add(staticActionGroups) && !staticActionGroups.getCEntries().isEmpty()) {
throw new StaticResourceException("Unable to load static action groups");
}

log.debug("Static action groups loaded ({})", staticActionGroups.getCEntries().size());
log.debug("Static action groups loaded ({})", staticActionGroups.getCEntries().size());

if (tenants.containsAny(staticTenants)) {
throw new StaticResourceException("Cannot override static tenants");
}
if (!tenants.add(staticTenants) && !staticTenants.getCEntries().isEmpty()) {
throw new StaticResourceException("Unable to load static tenants");
}
if (tenants.containsAny(staticTenants)) {
throw new StaticResourceException("Cannot override static tenants");
}
if (!tenants.add(staticTenants) && !staticTenants.getCEntries().isEmpty()) {
throw new StaticResourceException("Unable to load static tenants");
}

log.debug("Static tenants loaded ({})", staticTenants.getCEntries().size());

log.debug(
"Static configuration loaded (total roles: {}/total action groups: {}/total tenants: {})",
roles.getCEntries().size(),
actionGroups.getCEntries().size(),
tenants.getCEntries().size()
);

log.debug("Static tenants loaded ({})", staticTenants.getCEntries().size());

log.debug(
"Static configuration loaded (total roles: {}/total action groups: {}/total tenants: {})",
roles.getCEntries().size(),
actionGroups.getCEntries().size(),
tenants.getCEntries().size()
);

// rebuild v7 Models
dcm = new DynamicConfigModelV7(getConfigV7(config), opensearchSettings, configPath, iab, this.cih);
ium = new InternalUsersModelV7(
internalusers,
roles,
rolesmapping
);
cm = new ConfigModelV7(
roles,
rolesmapping,
actionGroups,
tenants,
dcm,
opensearchSettings
);
// rebuild v7 Models
dcm = new DynamicConfigModelV7(getConfigV7(config), opensearchSettings, configPath, iab, this.cih);
ium = new InternalUsersModelV7(internalusers, roles, rolesmapping);
cm = new ConfigModelV7(roles, rolesmapping, actionGroups, tenants, dcm, opensearchSettings);

// notify subscribers
eventBus.post(cm);
Expand Down Expand Up @@ -444,9 +430,7 @@ private static class NodesDnModelImpl extends NodesDnModel {

public NodesDnModelImpl(SecurityDynamicConfiguration<NodesDn> configuration) {
super();
this.configuration = null == configuration.getCType()
? SecurityDynamicConfiguration.empty(CType.NODESDN)
: configuration;
this.configuration = null == configuration.getCType() ? SecurityDynamicConfiguration.empty(CType.NODESDN) : configuration;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Set;

import com.fasterxml.jackson.databind.JavaType;

import org.opensearch.common.collect.Tuple;
import org.opensearch.core.common.Strings;
import org.opensearch.security.DefaultObjectMapper;
Expand Down Expand Up @@ -206,13 +207,13 @@ public static SecurityDynamicConfiguration<ActionGroupsV7> migrateActionGroups(S
a7.get_meta().setConfig_version(2);
a7.get_meta().setType("actiongroups");

for (final Entry<String, ?> r6a : r6as.getCEntries().entrySet()) {
if (r6a.getValue() instanceof List) {
a7.putCEntry(r6a.getKey(), new ActionGroupsV7(r6a.getKey(), (List<String>) r6a.getValue()));
} else {
a7.putCEntry(r6a.getKey(), new ActionGroupsV7(r6a.getKey(), (ActionGroupsV6) r6a.getValue()));
}
for (final Entry<String, ?> r6a : r6as.getCEntries().entrySet()) {
if (r6a.getValue() instanceof List) {
a7.putCEntry(r6a.getKey(), new ActionGroupsV7(r6a.getKey(), (List<String>) r6a.getValue()));
} else {
a7.putCEntry(r6a.getKey(), new ActionGroupsV7(r6a.getKey(), (ActionGroupsV6) r6a.getValue()));
}
}

return a7;
}
Expand Down
60 changes: 41 additions & 19 deletions src/main/java/org/opensearch/security/securityconf/impl/CType.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,13 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JavaType;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import org.opensearch.security.DefaultObjectMapper;
import org.opensearch.security.NonValidatingObjectMapper;
Expand All @@ -63,22 +58,52 @@

public class CType<T> implements Comparable<CType<?>> {

public static final CType<ActionGroupsV7> ACTIONGROUPS = new CType<>("action_groups", ActionGroupsV7.class, 0, false, new OldConfigVersion<>(1, ActionGroupsV6.class, ActionGroupsV7::new));
public static final CType<ActionGroupsV7> ACTIONGROUPS = new CType<>(
"action_groups",
ActionGroupsV7.class,
0,
false,
new OldConfigVersion<>(1, ActionGroupsV6.class, ActionGroupsV7::new)
);
public static final CType<AllowlistingSettings> ALLOWLIST = new CType<>("allowlist", AllowlistingSettings.class, 1, true);
public static final CType<AuditConfig> AUDIT = new CType<>("audit", AuditConfig.class, 2, true);
public static final CType<ConfigV7> CONFIG = new CType<>("config", ConfigV7.class, 3, false, new OldConfigVersion<>(1, ConfigV6.class, ConfigV7::new));
public static final CType<InternalUserV7> INTERNALUSERS = new CType<>("internal_users", InternalUserV7.class, 4, false, new OldConfigVersion<>(1, InternalUserV6.class, InternalUserV7::new));
public static final CType<ConfigV7> CONFIG = new CType<>(
"config",
ConfigV7.class,
3,
false,
new OldConfigVersion<>(1, ConfigV6.class, ConfigV7::new)
);
public static final CType<InternalUserV7> INTERNALUSERS = new CType<>(
"internal_users",
InternalUserV7.class,
4,
false,
new OldConfigVersion<>(1, InternalUserV6.class, InternalUserV7::new)
);
public static final CType<NodesDn> NODESDN = new CType<>("nodes_dn", NodesDn.class, 5, true);
public static final CType<RoleV7> ROLES = new CType<>("roles", RoleV7.class, 6, false, new OldConfigVersion<>(1, RoleV6.class, RoleV7::new));
public static final CType<RoleMappingsV7> ROLESMAPPING = new CType<>("roles_mapping", RoleMappingsV7.class, 7, false, new OldConfigVersion<>(1, RoleMappingsV6.class, RoleMappingsV7::new));
public static final CType<RoleV7> ROLES = new CType<>(
"roles",
RoleV7.class,
6,
false,
new OldConfigVersion<>(1, RoleV6.class, RoleV7::new)
);
public static final CType<RoleMappingsV7> ROLESMAPPING = new CType<>(
"roles_mapping",
RoleMappingsV7.class,
7,
false,
new OldConfigVersion<>(1, RoleMappingsV6.class, RoleMappingsV7::new)
);
public static final CType<TenantV7> TENANTS = new CType<>("tenants", TenantV7.class, 8, false);
public static final CType<WhitelistingSettings> WHITELIST = new CType<>("whitelist", WhitelistingSettings.class, 9, true);

private final String name;
private final Class<T> configClass;
private final String configFileName;
private final boolean emptyIfMissing;
private final OldConfigVersion<?, T> [] oldConfigVersions;
private final OldConfigVersion<?, T>[] oldConfigVersions;
private final int ord;

private final static Set<CType<?>> allSet = new HashSet<>();
Expand All @@ -88,7 +113,7 @@ public class CType<T> implements Comparable<CType<?>> {

@SafeVarargs
@SuppressWarnings("varargs")
private CType(String name, Class<T> configClass, int ord, boolean emptyIfMissing, OldConfigVersion<?, T> ... oldConfigVersions) {
private CType(String name, Class<T> configClass, int ord, boolean emptyIfMissing, OldConfigVersion<?, T>... oldConfigVersions) {
this.name = name;
this.configClass = configClass;
this.ord = ord;
Expand Down Expand Up @@ -142,15 +167,11 @@ public static CType<?> fromOrd(int ord) {
}

public static Set<CType<?>> requiredConfigTypes() {
return values().stream()
.filter(Predicate.not(CType::emptyIfMissing))
.collect(Collectors.toUnmodifiableSet());
return values().stream().filter(Predicate.not(CType::emptyIfMissing)).collect(Collectors.toUnmodifiableSet());
}

public static Set<CType<?>> notRequiredConfigTypes() {
return values().stream()
.filter(CType::emptyIfMissing)
.collect(Collectors.toUnmodifiableSet());
return values().stream().filter(CType::emptyIfMissing).collect(Collectors.toUnmodifiableSet());
}

public Path configFile(final Path configDir) {
Expand Down Expand Up @@ -199,7 +220,8 @@ public Function<OldType, NewType> getConversionFunction() {
return conversionFunction;
}

public SecurityDynamicConfiguration<NewType> parseJson(CType<NewType> ctype, String json, boolean acceptInvalid) throws IOException {
public SecurityDynamicConfiguration<NewType> parseJson(CType<NewType> ctype, String json, boolean acceptInvalid)
throws IOException {
JavaType javaType = DefaultObjectMapper.getTypeFactory().constructParametricType(SecurityDynamicConfiguration.class, oldType);

if (acceptInvalid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.security.DefaultObjectMapper;
import org.opensearch.security.NonValidatingObjectMapper;
import org.opensearch.security.securityconf.Hashed;
import org.opensearch.security.securityconf.Hideable;
import org.opensearch.security.securityconf.StaticDefinable;
Expand Down Expand Up @@ -95,8 +94,8 @@ public static <T> SecurityDynamicConfiguration<T> fromJson(
sdc = oldConfigVersion.parseJson(ctype, json, acceptInvalid);
} else {
sdc = DefaultObjectMapper.readValue(
json,
DefaultObjectMapper.getTypeFactory().constructParametricType(SecurityDynamicConfiguration.class, ctype.getConfigClass())
json,
DefaultObjectMapper.getTypeFactory().constructParametricType(SecurityDynamicConfiguration.class, ctype.getConfigClass())
);
}

Expand All @@ -117,8 +116,7 @@ public static <T> SecurityDynamicConfiguration<T> fromJson(
/**
* For testing only
*/
public static <T> SecurityDynamicConfiguration<T> fromMap(Map<String, Object> map, CType<T> ctype)
throws JsonProcessingException {
public static <T> SecurityDynamicConfiguration<T> fromMap(Map<String, Object> map, CType<T> ctype) throws JsonProcessingException {
SecurityDynamicConfiguration<T> result = DefaultObjectMapper.objectMapper.convertValue(
map,
DefaultObjectMapper.getTypeFactory().constructParametricType(SecurityDynamicConfiguration.class, ctype.getConfigClass())
Expand Down Expand Up @@ -150,7 +148,13 @@ public static void validate(SecurityDynamicConfiguration<?> sdc, int version, CT

public static <T> SecurityDynamicConfiguration<T> fromNode(JsonNode json, CType<T> ctype, int version, long seqNo, long primaryTerm)
throws IOException {
return SecurityDynamicConfiguration.<T>fromJson(DefaultObjectMapper.writeValueAsString(json, false), ctype, version, seqNo, primaryTerm);
return SecurityDynamicConfiguration.<T>fromJson(
DefaultObjectMapper.writeValueAsString(json, false),
ctype,
version,
seqNo,
primaryTerm
);
}

// for Jackson
Expand Down
Loading

0 comments on commit 6bb6de4

Please sign in to comment.