com.amazon.opendistroforelasticsearch.security.securityconf.Migration Maven / Gradle / Ivy
package com.amazon.opendistroforelasticsearch.security.securityconf;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import com.amazon.opendistroforelasticsearch.security.securityconf.impl.CType;
import com.amazon.opendistroforelasticsearch.security.securityconf.impl.Meta;
import com.amazon.opendistroforelasticsearch.security.securityconf.impl.SecurityDynamicConfiguration;
import com.amazon.opendistroforelasticsearch.security.securityconf.impl.v6.*;
import com.amazon.opendistroforelasticsearch.security.securityconf.impl.v7.*;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
public class Migration {
public static Tuple,SecurityDynamicConfiguration> migrateRoles(SecurityDynamicConfiguration r6cs, SecurityDynamicConfiguration rms6) throws MigrationException {
final SecurityDynamicConfiguration r7 = SecurityDynamicConfiguration.empty();
r7.setCType(r6cs.getCType());
r7.set_meta(new Meta());
r7.get_meta().setConfig_version(2);
r7.get_meta().setType("roles");
final SecurityDynamicConfiguration t7 = SecurityDynamicConfiguration.empty();
t7.setCType(CType.TENANTS);
t7.set_meta(new Meta());
t7.get_meta().setConfig_version(2);
t7.get_meta().setType("tenants");
Set dedupTenants = new HashSet<>();
for(final Entry r6e: r6cs.getCEntries().entrySet()) {
final String roleName = r6e.getKey();
final RoleV6 r6 = r6e.getValue();
if(r6 == null) {
RoleV7 noPermRole = new RoleV7();
noPermRole.setDescription("Migrated from v6, was empty");
r7.putCEntry(roleName, noPermRole);
continue;
}
r7.putCEntry(roleName, new RoleV7(r6));
for(Entry tenant: r6.getTenants().entrySet()) {
dedupTenants.add(tenant.getKey());
}
}
if(rms6 != null) {
for(final Entry r6m: rms6.getCEntries().entrySet()) {
final String roleName = r6m.getKey();
//final RoleMappingsV6 r6 = r6m.getValue();
if(!r7.exists(roleName)) {
//rolemapping but role does not exists
RoleV7 noPermRole = new RoleV7();
noPermRole.setDescription("Migrated from v6, was in rolemappings but no role existed");
r7.putCEntry(roleName, noPermRole);
}
}
}
for(String tenantName: dedupTenants) {
TenantV7 entry = new TenantV7();
entry.setDescription("Migrated from v6");
t7.putCEntry(tenantName, entry);
}
return new Tuple, SecurityDynamicConfiguration>(r7, t7);
}
public static SecurityDynamicConfiguration migrateConfig(SecurityDynamicConfiguration r6cs) throws MigrationException {
final SecurityDynamicConfiguration c7 = SecurityDynamicConfiguration.empty();
c7.setCType(r6cs.getCType());
c7.set_meta(new Meta());
c7.get_meta().setConfig_version(2);
c7.get_meta().setType("config");
if(r6cs.getCEntries().size() != 1) {
throw new MigrationException("Unable to migrate config because expected size was 1 but actual size is "+r6cs.getCEntries().size());
}
if(r6cs.getCEntries().get("opendistro_security") == null) {
throw new MigrationException("Unable to migrate config because 'opendistro_security' key not found");
}
for(final Entry r6c: r6cs.getCEntries().entrySet()) {
c7.putCEntry("config", new ConfigV7(r6c.getValue()));
}
return c7;
}
public static SecurityDynamicConfiguration migrateInternalUsers(SecurityDynamicConfiguration r6is) throws MigrationException {
final SecurityDynamicConfiguration i7 = SecurityDynamicConfiguration.empty();
i7.setCType(r6is.getCType());
i7.set_meta(new Meta());
i7.get_meta().setConfig_version(2);
i7.get_meta().setType("internalusers");
for(final Entry r6i: r6is.getCEntries().entrySet()) {
final String username = !Strings.isNullOrEmpty(r6i.getValue().getUsername())?r6i.getValue().getUsername():r6i.getKey();
i7.putCEntry(username, new InternalUserV7(r6i.getValue()));
}
return i7;
}
public static SecurityDynamicConfiguration migrateActionGroups(SecurityDynamicConfiguration> r6as) throws MigrationException {
final SecurityDynamicConfiguration a7 = SecurityDynamicConfiguration.empty();
a7.setCType(r6as.getCType());
a7.set_meta(new Meta());
a7.get_meta().setConfig_version(2);
a7.get_meta().setType("actiongroups");
if(r6as.getImplementingClass().isAssignableFrom(List.class)) {
for(final Entry r6a: r6as.getCEntries().entrySet()) {
a7.putCEntry(r6a.getKey(), new ActionGroupsV7(r6a.getKey(), (List) r6a.getValue()));
}
} else {
for(final Entry r6a: r6as.getCEntries().entrySet()) {
a7.putCEntry(r6a.getKey(), new ActionGroupsV7(r6a.getKey(), (ActionGroupsV6)r6a.getValue()));
}
}
return a7;
}
public static SecurityDynamicConfiguration migrateRoleMappings(SecurityDynamicConfiguration r6rms) throws MigrationException {
final SecurityDynamicConfiguration rms7 = SecurityDynamicConfiguration.empty();
rms7.setCType(r6rms.getCType());
rms7.set_meta(new Meta());
rms7.get_meta().setConfig_version(2);
rms7.get_meta().setType("rolesmapping");
for(final Entry r6m: r6rms.getCEntries().entrySet()) {
rms7.putCEntry(r6m.getKey(), new RoleMappingsV7(r6m.getValue()));
}
return rms7;
}
}