Skip to content

Commit

Permalink
Rebase fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Nils Bandener <[email protected]>
  • Loading branch information
nibix committed Aug 31, 2024
1 parent 582e303 commit 06c942f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 903 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,36 @@
*/
package org.opensearch.security.util;

import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.opensearch.Version;
import org.opensearch.cluster.metadata.AliasMetadata;
import org.opensearch.cluster.metadata.DataStream;
import org.opensearch.cluster.metadata.IndexAbstraction;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.metadata.Metadata;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.index.Index;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Creates mocks of org.opensearch.cluster.metadata.IndexAbstraction maps. Useful for unit testing code which
* operates on index metadata.
*/
public class MockIndexMetadataBuilder {

private static final Settings INDEX_SETTINGS = Settings.builder()
.put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), Version.CURRENT)
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
.build();

private Metadata.Builder delegate = new Metadata.Builder();
private Map<String, IndexMetadata.Builder> nameToIndexMetadataBuilderMap = new HashMap<>();

private Map<String, IndexAbstraction> nameToIndexAbstractionMap = new HashMap<>();
private Map<String, IndexMetadata> nameToIndexMetadataMap = new HashMap<>();
private Map<String, Set<String>> indicesToAliases = new HashMap<>();
Expand All @@ -58,58 +65,16 @@ public static MockIndexMetadataBuilder dataStreams(String... dataStreams) {
return builder;
}

public ImmutableMap<String, IndexAbstraction> build() {
Map<String, AliasMetadata> aliasMetadataMap = new HashMap<>();

for (Map.Entry<String, Set<String>> aliasEntry : this.aliasesToIndices.entrySet()) {
String alias = aliasEntry.getKey();
AliasMetadata aliasMetadata = AliasMetadata.builder(alias).build();
aliasMetadataMap.put(alias, aliasMetadata);
}

for (Map.Entry<String, Set<String>> indexEntry : this.indicesToAliases.entrySet()) {
String index = indexEntry.getKey();
Set<String> aliases = indexEntry.getValue();

IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(index)
.settings(Settings.builder().put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), Version.CURRENT))
.numberOfShards(1)
.numberOfReplicas(1);

for (String alias : aliases) {
indexMetadataBuilder.putAlias(aliasMetadataMap.get(alias));
}

IndexMetadata indexMetadata = indexMetadataBuilder.build();
nameToIndexMetadataMap.put(index, indexMetadata);
nameToIndexAbstractionMap.put(index, new IndexAbstraction.Index(indexMetadata));
}

for (Map.Entry<String, Set<String>> aliasEntry : this.aliasesToIndices.entrySet()) {
String alias = aliasEntry.getKey();
Set<String> indices = aliasEntry.getValue();
AliasMetadata aliasMetadata = aliasMetadataMap.get(alias);

String firstIndex = indices.iterator().next();
indices.remove(firstIndex);

IndexMetadata firstIndexMetadata = nameToIndexMetadataMap.get(firstIndex);
IndexAbstraction.Alias indexAbstraction = new IndexAbstraction.Alias(aliasMetadata, firstIndexMetadata);

for (String index : indices) {
indexAbstraction.getIndices().add(nameToIndexMetadataMap.get(index));
}

nameToIndexAbstractionMap.put(alias, indexAbstraction);
public Metadata build() {
for (IndexMetadata.Builder indexMetadataBuilder : nameToIndexMetadataBuilderMap.values()) {
this.delegate.put(indexMetadataBuilder);
}

return ImmutableMap.copyOf(this.nameToIndexAbstractionMap);
return this.delegate.build();
}

public MockIndexMetadataBuilder index(String index) {
if (!this.indicesToAliases.containsKey(index)) {
this.indicesToAliases.put(index, new HashSet<>());
}
public MockIndexMetadataBuilder index(String indexName) {
getIndexMetadataBuilder(indexName);
return this;
}

Expand All @@ -127,21 +92,11 @@ public MockIndexMetadataBuilder dataStream(String dataStream, int generations) {
for (int i = 1; i <= generations; i++) {
String backingIndexName = DataStream.getDefaultBackingIndexName(dataStream, i);
backingIndices.add(new Index(backingIndexName, backingIndexName));
getIndexMetadata(backingIndexName);
}

DataStream dataStreamMetadata = new DataStream(dataStream, new DataStream.TimestampField("@timestamp"), backingIndices);
IndexAbstraction.DataStream dataStreamIndexAbstraction = new IndexAbstraction.DataStream(
dataStreamMetadata,
backingIndices.stream().map(i -> getIndexMetadata(i.getName())).collect(Collectors.toList())
);
this.nameToIndexAbstractionMap.put(dataStream, dataStreamIndexAbstraction);

for (Index backingIndex : backingIndices) {
this.nameToIndexAbstractionMap.put(
backingIndex.getName(),
new IndexAbstraction.Index(getIndexMetadata(backingIndex.getName()), dataStreamIndexAbstraction)
);
}
this.delegate.put(dataStreamMetadata);

return this;
}
Expand All @@ -161,26 +116,51 @@ private IndexMetadata getIndexMetadata(String index) {
return result;
}

private IndexMetadata.Builder getIndexMetadataBuilder(String indexName) {
IndexMetadata.Builder result = this.nameToIndexMetadataBuilderMap.get(indexName);

if (result != null) {
return result;
}

result = new IndexMetadata.Builder(indexName).settings(INDEX_SETTINGS);

this.nameToIndexMetadataBuilderMap.put(indexName, result);

return result;
}

public class AliasBuilder {
private String alias;
private String aliasName;

private AliasBuilder(String alias) {
this.alias = alias;
this.aliasName = alias;
}

public MockIndexMetadataBuilder of(String firstIndex, String... moreIndices) {
MockIndexMetadataBuilder.this.indicesToAliases.computeIfAbsent(firstIndex, (k) -> new HashSet<>()).add(this.alias);
public MockIndexMetadataBuilder of(String... indices) {
AliasMetadata aliasMetadata = new AliasMetadata.Builder(aliasName).build();

for (String index : indices) {
IndexMetadata.Builder indexMetadataBuilder = getIndexMetadataBuilder(index);
indexMetadataBuilder.putAlias(aliasMetadata);
}

/*
MockIndexMetadataBuilder.this.delegate.put(aliasMetadata);
MockIndexMetadataBuilder.this.indicesToAliases.computeIfAbsent(firstIndex, (k) -> new HashSet<>()).add(this.aliasName);
Set<String> indices = new HashSet<>();
indices.add(firstIndex);
for (String index : moreIndices) {
MockIndexMetadataBuilder.this.indicesToAliases.computeIfAbsent(index, (k) -> new HashSet<>()).add(this.alias);
MockIndexMetadataBuilder.this.indicesToAliases.computeIfAbsent(index, (k) -> new HashSet<>()).add(this.aliasName);
indices.add(index);
}
MockIndexMetadataBuilder.this.aliasesToIndices.put(this.alias, indices);

MockIndexMetadataBuilder.this.aliasesToIndices.put(this.aliasName, indices);
*/
return MockIndexMetadataBuilder.this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@
import org.opensearch.client.Client;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.index.query.QueryBuilder;
Expand Down Expand Up @@ -168,14 +166,6 @@ public TestSecurityConfig users(User... users) {
return this;
}

public TestSecurityConfig users(User... users) {
for (User user : users) {
this.user(user);
}
return this;
}


public TestSecurityConfig withRestAdminUser(final String name, final String... permissions) {
if (!internalUsers.containsKey(name)) {
user(new User(name, "REST Admin with permissions: " + Arrays.toString(permissions)).reserved(true));
Expand Down
Loading

0 comments on commit 06c942f

Please sign in to comment.