How to use addSecrets method of org.testcontainers.vault.VaultContainer class

Best Testcontainers-java code snippet using org.testcontainers.vault.VaultContainer.addSecrets

Source:VaultContainer.java Github

copy

Full Screen

...46 withExposedPorts(port);47 }48 @Override49 protected void containerIsStarted(InspectContainerResponse containerInfo) {50 addSecrets();51 runInitCommands();52 }53 private void addSecrets() {54 if (!secretsMap.isEmpty()) {55 try {56 this.execInContainer(buildExecCommand(secretsMap)).getStdout().contains("Success");57 } catch (IOException | InterruptedException e) {58 logger().error("Failed to add these secrets {} into Vault via exec command. Exception message: {}", secretsMap, e.getMessage());59 }60 }61 }62 private String[] buildExecCommand(Map<String, List<String>> map) {63 StringBuilder stringBuilder = new StringBuilder();64 map.forEach((path, secrets) -> {65 stringBuilder.append(" && vault kv put " + path);66 secrets.forEach(item -> stringBuilder.append(" " + item));67 });68 return new String[]{"/bin/sh", "-c", stringBuilder.toString().substring(4)};69 }70 private void runInitCommands() {71 if (!initCommands.isEmpty()) {72 String commands = initCommands.stream()73 .map(command -> "vault " + command)74 .collect(Collectors.joining(" && "));75 try {76 ExecResult execResult = this.execInContainer(new String[]{"/bin/sh", "-c", commands});77 if (execResult.getExitCode() != 0) {78 logger().error("Failed to execute these init commands {}. Exit code {}. Stdout {}. Stderr {}",79 initCommands, execResult.getExitCode(), execResult.getStdout(), execResult.getStderr());80 }81 } catch (IOException | InterruptedException e) {82 logger().error("Failed to execute these init commands {}. Exception message: {}", initCommands, e.getMessage());83 }84 }85 }86 /**87 * Sets the Vault root token for the container so application tests can source secrets using the token88 *89 * @param token the root token value to set for Vault.90 * @return this91 */92 public SELF withVaultToken(String token) {93 withEnv("VAULT_DEV_ROOT_TOKEN_ID", token);94 withEnv("VAULT_TOKEN", token);95 return self();96 }97 /**98 * Sets the Vault port in the container as well as the port bindings for the host to reach the container over HTTP.99 *100 * @param port the port number you want to have the Vault container listen on for tests.101 * @return this102 * @deprecated the exposed port will be randomized automatically. As calling this method provides no additional value, you are recommended to remove the call. getFirstMappedPort() may be used to obtain the listening vault port.103 */104 @Deprecated105 public SELF withVaultPort(int port) {106 this.port = port;107 return self();108 }109 /**110 * Sets the logging level for the Vault server in the container.111 * Logs can be consumed through {@link #withLogConsumer(Consumer)}.112 *113 * @param level the logging level to set for Vault.114 * @return this115 */116 public SELF withLogLevel(VaultLogLevel level) {117 return withEnv("VAULT_LOG_LEVEL", level.config);118 }119 /**120 * Pre-loads secrets into Vault container. User may specify one or more secrets and all will be added to each path121 * that is specified. Thus this can be called more than once for multiple paths to be added to Vault.122 * <p>123 * The secrets are added to vault directly after the container is up via the124 * {@link #addSecrets() addSecrets}, called from {@link #containerIsStarted(InspectContainerResponse) containerIsStarted}125 *126 * @param path specific Vault path to store specified secrets127 * @param firstSecret first secret to add to specifed path128 * @param remainingSecrets var args list of secrets to add to specified path129 * @return this130 */131 public SELF withSecretInVault(String path, String firstSecret, String... remainingSecrets) {132 List<String> list = new ArrayList<>();133 list.add(firstSecret);134 for (String secret : remainingSecrets) {135 list.add(secret);136 }137 if (secretsMap.containsKey(path)) {138 list.addAll(list);...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful