Skip to content

Commit

Permalink
[Backport 2.x] Move REST API tests into integration tests (Part 1) (o…
Browse files Browse the repository at this point in the history
  • Loading branch information
willyborankin authored May 6, 2024
1 parent a14666e commit db3c4d9
Show file tree
Hide file tree
Showing 16 changed files with 763 additions and 506 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,53 @@
*/
package org.opensearch.security;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Objects;

class ConfigurationFiles {
import org.opensearch.core.common.Strings;
import org.opensearch.security.securityconf.impl.CType;

public static void createRoleMappingFile(File destination) {
String resource = "roles_mapping.yml";
copyResourceToFile(resource, destination);
}
public class ConfigurationFiles {

public static Path createConfigurationDirectory() {
try {
Path tempDirectory = Files.createTempDirectory("test-security-config");
String[] configurationFiles = {
"config.yml",
"action_groups.yml",
"internal_users.yml",
"nodes_dn.yml",
"roles.yml",
"roles_mapping.yml",
CType.ACTIONGROUPS.configFileName(),
CType.CONFIG.configFileName(),
CType.INTERNALUSERS.configFileName(),
CType.NODESDN.configFileName(),
CType.ROLES.configFileName(),
CType.ROLESMAPPING.configFileName(),
"security_tenants.yml",
"tenants.yml",
"whitelist.yml" };
CType.TENANTS.configFileName(),
CType.WHITELIST.configFileName() };
for (String fileName : configurationFiles) {
Path configFileDestination = tempDirectory.resolve(fileName);
copyResourceToFile(fileName, configFileDestination.toFile());
copyResourceToFile(fileName, tempDirectory.resolve(fileName));
}
return tempDirectory.toAbsolutePath();
} catch (IOException ex) {
throw new RuntimeException("Cannot create directory with security plugin configuration.", ex);
}
}

private static void copyResourceToFile(String resource, File destination) {
public static void writeToConfig(final CType cType, final Path configFolder, final String content) throws IOException {
if (Strings.isNullOrEmpty(content)) return;
try (final var out = Files.newOutputStream(cType.configFile(configFolder), StandardOpenOption.APPEND)) {
out.write(content.getBytes(StandardCharsets.UTF_8));
out.flush();
}
}

public static void copyResourceToFile(String resource, Path destination) {
try (InputStream input = ConfigurationFiles.class.getClassLoader().getResourceAsStream(resource)) {
Objects.requireNonNull(input, "Cannot find source resource " + resource);
try (OutputStream output = new FileOutputStream(destination)) {
try (final var output = Files.newOutputStream(destination)) {
input.transferTo(output);
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.runner.RunWith;

import org.opensearch.client.Client;
import org.opensearch.security.securityconf.impl.CType;
import org.opensearch.test.framework.AsyncActions;
import org.opensearch.test.framework.TestSecurityConfig.Role;
import org.opensearch.test.framework.TestSecurityConfig.User;
Expand Down Expand Up @@ -225,8 +226,8 @@ public void shouldAccessIndexWithPlaceholder_negative() {
@Test
public void shouldUseSecurityAdminTool() throws Exception {
SecurityAdminLauncher securityAdminLauncher = new SecurityAdminLauncher(cluster.getHttpPort(), cluster.getTestCertificates());
File rolesMapping = configurationDirectory.newFile("roles_mapping.yml");
ConfigurationFiles.createRoleMappingFile(rolesMapping);
File rolesMapping = configurationDirectory.newFile(CType.ROLESMAPPING.configFileName());
ConfigurationFiles.copyResourceToFile(CType.ROLESMAPPING.configFileName(), rolesMapping.toPath());

int exitCode = securityAdminLauncher.updateRoleMappings(rolesMapping);

Expand Down
Loading

0 comments on commit db3c4d9

Please sign in to comment.