Skip to content

Commit

Permalink
Merge branch 'rel_0.10.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
golovnin committed Jul 8, 2018
2 parents fe46a54 + fc55814 commit 9343f2c
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 40 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repositories {
```
Add a Gradle compile dependency to the `build.gradle` file of your project:
```groovy
testCompile 'com.github.golovnin:embedded-vault:0.10.1.0'
testCompile 'com.github.golovnin:embedded-vault:0.10.3.0'
```

### Usage
Expand All @@ -26,20 +26,21 @@ VaultServerConfig config = new VaultServerConfig.Builder()
VaultServerStarter starter = VaultServerStarter.getDefaultInstance();
VaultServerExecutable executable = starter.prepare(config);
VaultServerProcess process = executable.start();
String unsealKey = process.getUnsealKey();

// Execute your tests here

process.stop();
```
Here is the example of how to launch the Vault instance using a custom version:
```java
IVersion v0_7_2 = () -> "0.7.2";
VaultServerConfig config = new VaultServerConfig.Builder()
.version(v0_7_2)
.version("0.7.2")
.build();
VaultServerStarter starter = VaultServerStarter.getDefaultInstance();
VaultServerExecutable executable = starter.prepare(config);
VaultServerProcess process = executable.start();
String unsealKey = process.getUnsealKey();

// Execute your tests here

Expand All @@ -48,9 +49,9 @@ process.stop();

### Supported Vault versions and platforms

Versions: 0.10.1 and any custom
Versions: 0.10.3 and any custom

Platforms: Mac OS X, FreeBSD, Linux, Solaris and Windows


Copyright (c) 2017, Andrej Golovnin
Copyright (c) 2018, Andrej Golovnin
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ apply plugin: 'maven-publish'
apply plugin: 'java-library-distribution'

ext {
productVersion = '0.10.1.0'
productVersion = '0.10.3.0'
product = 'embedded-vault'
javaTarget = JavaVersion.VERSION_1_8
libraries = [
flapdoodleProcess: 'de.flapdoodle.embed:de.flapdoodle.embed.process:2.0.1',
flapdoodleProcess: 'de.flapdoodle.embed:de.flapdoodle.embed.process:2.0.5',
junit: 'junit:junit:4.12'
]
}
Expand Down Expand Up @@ -120,5 +120,5 @@ bintray {
}

task wrapper(type: Wrapper) {
gradleVersion = '4.7'
gradleVersion = '4.8.1'
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private VaultBuilders() {
// NOP
}

static DownloadConfigBuilder downloadConfigBuilder() {
private static DownloadConfigBuilder downloadConfigBuilder() {
return new DownloadConfigBuilder()
.fileNaming(new UUIDTempNaming())
.downloadPath("https://releases.hashicorp.com/vault/")
Expand All @@ -69,7 +69,7 @@ static RuntimeConfigBuilder runtimeConfigBuilder() {
.artifactStore(storeBuilder().build());
}

static ExtractedArtifactStoreBuilder storeBuilder() {
private static ExtractedArtifactStoreBuilder storeBuilder() {
return new ExtractedArtifactStoreBuilder()
.extractDir(new UserHome(".embedded-vault/extracted"))
.extractExecutableNaming(new OriginNaming())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2018, Andrej Golovnin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of fontviewer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.github.golovnin.embedded.vault;

import java.util.function.Consumer;

import de.flapdoodle.embed.process.io.IStreamProcessor;

import static java.util.Objects.requireNonNull;

/**
* @author <a href="mailto:[email protected]">Andrej Golovnin</a>
*/
final class VaultOutputProcessor implements IStreamProcessor {

private final IStreamProcessor delegate;
private final Consumer<String> outputProcessor;

VaultOutputProcessor(IStreamProcessor delegate, Consumer<String> outputProcessor) {
this.delegate = requireNonNull(delegate, "delegate may not be null");
this.outputProcessor = requireNonNull(outputProcessor, "outputProcessor may not be null");
}

@Override
public void process(String block) {
outputProcessor.accept(block);
delegate.process(block);
}

@Override
public void onProcessed() {
delegate.onProcessed();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@

package com.github.golovnin.embedded.vault;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import de.flapdoodle.embed.process.builder.AbstractBuilder;
import de.flapdoodle.embed.process.builder.TypedProperty;
import de.flapdoodle.embed.process.config.IExecutableProcessConfig;
import de.flapdoodle.embed.process.config.ISupportConfig;
import de.flapdoodle.embed.process.distribution.IVersion;
import de.flapdoodle.embed.process.runtime.Network;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static java.util.Objects.requireNonNull;

/**
* @author Andrej Golovnin
Expand All @@ -57,11 +60,14 @@ public final class VaultServerConfig implements IExecutableProcessConfig {
private final String clusterName;
private final String defaultLeaseTTL;
private final String maxLeaseTTL;
private final Consumer<String> outConsumer;
private final Consumer<String> errConsumer;

VaultServerConfig(IVersion version, long startupTimeout,
String listenerHost, int listenerPort, String rootTokenID,
VaultLogLevel logLevel, String clusterName, String defaultLeaseTTL,
String maxLeaseTTL)
String maxLeaseTTL, Consumer<String> outConsumer,
Consumer<String> errConsumer)
{
this.version = version;
this.startupTimeout = startupTimeout;
Expand All @@ -72,12 +78,16 @@ public final class VaultServerConfig implements IExecutableProcessConfig {
this.clusterName = clusterName;
this.defaultLeaseTTL = defaultLeaseTTL;
this.maxLeaseTTL = maxLeaseTTL;
this.outConsumer = outConsumer;
this.errConsumer = errConsumer;
}

public static final class Builder extends AbstractBuilder<VaultServerConfig> {

private static final String DEFAULT_ADDRESS = "127.0.0.1";

private static final Consumer<String> NOP_CONSUMER = s -> {};

private static final TypedProperty<IVersion> VERSION =
TypedProperty.with("version", IVersion.class);

Expand Down Expand Up @@ -105,8 +115,14 @@ public static final class Builder extends AbstractBuilder<VaultServerConfig> {
private static final TypedProperty<String> MAX_LEASE_TTL =
TypedProperty.with("max-lease-ttl", String.class);

private static final TypedProperty<Consumer> OUT_CONSUMER =
TypedProperty.with("out-consumer", Consumer.class);

private static final TypedProperty<Consumer> ERR_CONSUMER =
TypedProperty.with("err-consumer", Consumer.class);

public Builder() {
property(VERSION).setDefault(VaultVersion.V0_10_1);
property(VERSION).setDefault(VaultVersion.V0_10_3);
property(STARTUP_TIMEOUT).setDefault(60000L);
property(LISTENER_HOST).setDefault(DEFAULT_ADDRESS);
property(LISTENER_PORT).setDefault(8200);
Expand All @@ -115,13 +131,19 @@ public Builder() {
property(CLUSTER_NAME).setDefault("dev");
property(DEFAULT_LEASE_TTL).setDefault("768h");
property(MAX_LEASE_TTL).setDefault("768h");
property(OUT_CONSUMER).setDefault(NOP_CONSUMER);
property(ERR_CONSUMER).setDefault(NOP_CONSUMER);
}

public Builder version(IVersion version) {
property(VERSION).set(version);
return this;
}

public Builder version(String version) {
return version(() -> version);
}

public Builder startupTimeout(long startupTimeout, TimeUnit unit) {
property(STARTUP_TIMEOUT).set(unit.toMillis(startupTimeout));
return this;
Expand Down Expand Up @@ -178,6 +200,17 @@ public Builder maxLeaseTTL(String ttl) {
return this;
}

public Builder outConsumer(Consumer<String> consumer) {
property(OUT_CONSUMER).set(requireNonNull(consumer));
return this;
}

public Builder errConsumer(Consumer<String> consumer) {
property(ERR_CONSUMER).set(requireNonNull(consumer));
return this;
}

@SuppressWarnings("unchecked")
@Override
public VaultServerConfig build() {
return new VaultServerConfig(
Expand All @@ -189,7 +222,9 @@ public VaultServerConfig build() {
property(LOG_LEVEL).get(),
property(CLUSTER_NAME).get(),
property(DEFAULT_LEASE_TTL).get(),
property(MAX_LEASE_TTL).get());
property(MAX_LEASE_TTL).get(),
(Consumer<String>) property(OUT_CONSUMER).get(),
(Consumer<String>) property(ERR_CONSUMER).get());
}

}
Expand Down Expand Up @@ -226,6 +261,14 @@ public String getMaxLeaseTTL() {
return maxLeaseTTL;
}

public Consumer<String> getOutConsumer() {
return outConsumer;
}

public Consumer<String> getErrConsumer() {
return errConsumer;
}

@Override
public IVersion version() {
return version;
Expand All @@ -237,13 +280,11 @@ public ISupportConfig supportConfig() {
}

String toJson() {
return new StringBuilder()
.append("{\n")
.append("\t\"cluster_name\": \"").append(clusterName).append("\",\n")
.append("\t\"default_lease_ttl\": \"").append(defaultLeaseTTL).append("\",\n")
.append("\t\"max_lease_ttl\": \"").append(maxLeaseTTL).append("\"\n")
.append("}\n")
.toString();
return "{\n" +
"\t\"cluster_name\": \"" + clusterName + "\",\n" +
"\t\"default_lease_ttl\": \"" + defaultLeaseTTL + "\",\n" +
"\t\"max_lease_ttl\": \"" + maxLeaseTTL + "\"\n" +
"}\n";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import de.flapdoodle.embed.process.config.IRuntimeConfig;
import de.flapdoodle.embed.process.config.io.ProcessOutput;
Expand All @@ -63,9 +64,12 @@ public final class VaultServerProcess
private static final Set<String> KNOWN_FAILURE_MESSAGES =
Collections.singleton("Error ");

private static final String UNSEAL_KEY = "Unseal Key:";

private File configFile;

private Consumer<String> outConsumer;
private Consumer<String> errConsumer;
private String unsealKey;

VaultServerProcess(Distribution distribution, VaultServerConfig config,
IRuntimeConfig runtimeConfig, VaultServerExecutable executable)
Expand All @@ -85,6 +89,11 @@ protected List<String> getCommandLine(Distribution distribution,
) {
writer.write(config.toJson());
}

Consumer<String> unsealKeyConsumer = this::extractUnsealKey;
this.outConsumer = unsealKeyConsumer.andThen(config.getOutConsumer());
this.errConsumer = config.getErrConsumer();

String listenerHost = config.getListenerHost();
String listenerPort = String.valueOf(config.getListenerPort());
String rootTokenID = config.getRootTokenID();
Expand All @@ -107,9 +116,16 @@ protected void onAfterProcessStart(ProcessControl process,
LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor(
SUCCESS_MESSAGE, KNOWN_FAILURE_MESSAGES,
StreamToLineProcessor.wrap(outputConfig.getOutput()));
Processors.connect(process.getReader(), logWatch);
Processors.connect(process.getError(),
StreamToLineProcessor.wrap(outputConfig.getError()));

Processors.connect(
process.getReader(),
new VaultOutputProcessor(logWatch, outConsumer));
Processors.connect(
process.getError(),
new VaultOutputProcessor(
StreamToLineProcessor.wrap(outputConfig.getError()),
errConsumer));

logWatch.waitForResult(getConfig().getStartupTimeout());
if (logWatch.isInitWithSuccess()) {
setProcessId(getProcessId());
Expand Down Expand Up @@ -143,4 +159,15 @@ protected void cleanupInternal() {
Files.forceDelete(configFile);
}

public String getUnsealKey() {
return unsealKey;
}

private void extractUnsealKey(String block) {
int i = block.indexOf(UNSEAL_KEY);
if (i >= 0) {
unsealKey = block.substring(i + UNSEAL_KEY.length()).trim();
}
}

}
Loading

0 comments on commit 9343f2c

Please sign in to comment.