Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lets run postgres with another user #147

Closed
wants to merge 10 commits into from
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>com.opentable</groupId>
<artifactId>otj-parent-spring</artifactId>
<version>174</version>
<version>249</version>
</parent>

<scm>
Expand Down Expand Up @@ -56,7 +56,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<version>3.0.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
Expand Down Expand Up @@ -86,13 +86,13 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
<version>1.20</version>
</dependency>

<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.5</version>
<version>1.8</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,21 @@ public class EmbeddedPostgres implements Closeable
private final ProcessBuilder.Redirect errorRedirector;
private final ProcessBuilder.Redirect outputRedirector;

private final Optional<String> userSO;

EmbeddedPostgres(File parentDirectory, File dataDirectory, boolean cleanDataDirectory,
Map<String, String> postgresConfig, Map<String, String> localeConfig, int port, Map<String, String> connectConfig,
PgBinaryResolver pgBinaryResolver, ProcessBuilder.Redirect errorRedirector, ProcessBuilder.Redirect outputRedirector) throws IOException
{
this(parentDirectory, dataDirectory, cleanDataDirectory, postgresConfig, localeConfig, port, connectConfig,
pgBinaryResolver, errorRedirector, outputRedirector, DEFAULT_PG_STARTUP_WAIT, Optional.empty());
pgBinaryResolver, errorRedirector, outputRedirector, DEFAULT_PG_STARTUP_WAIT, Optional.empty(), Optional.empty());
}

EmbeddedPostgres(File parentDirectory, File dataDirectory, boolean cleanDataDirectory,
Map<String, String> postgresConfig, Map<String, String> localeConfig, int port, Map<String, String> connectConfig,
PgBinaryResolver pgBinaryResolver, ProcessBuilder.Redirect errorRedirector,
ProcessBuilder.Redirect outputRedirector, Duration pgStartupWait,
Optional<File> overrideWorkingDirectory) throws IOException
Optional<File> overrideWorkingDirectory, Optional<String> userSO) throws IOException
{
this.cleanDataDirectory = cleanDataDirectory;
this.postgresConfig = new HashMap<>(postgresConfig);
Expand All @@ -134,6 +136,7 @@ public class EmbeddedPostgres implements Closeable
this.errorRedirector = errorRedirector;
this.outputRedirector = outputRedirector;
this.pgStartupWait = pgStartupWait;
this.userSO = userSO;
Objects.requireNonNull(this.pgStartupWait, "Wait time cannot be null");

if (parentDirectory != null) {
Expand Down Expand Up @@ -498,6 +501,8 @@ public static class Builder
private ProcessBuilder.Redirect errRedirector = ProcessBuilder.Redirect.PIPE;
private ProcessBuilder.Redirect outRedirector = ProcessBuilder.Redirect.PIPE;

private Optional<String> userSO = Optional.empty();

Builder() {
config.put("timezone", "UTC");
config.put("synchronous_commit", "off");
Expand Down Expand Up @@ -572,6 +577,11 @@ public Builder setPgBinaryResolver(PgBinaryResolver pgBinaryResolver) {
return this;
}

public Builder setUserSO(String user) {
this.userSO = Optional.of(user);
return this;
}

public EmbeddedPostgres start() throws IOException {
if (builderPort == 0)
{
Expand All @@ -582,7 +592,7 @@ public EmbeddedPostgres start() throws IOException {
}
return new EmbeddedPostgres(parentDirectory, builderDataDirectory, builderCleanDataDirectory, config,
localeConfig, builderPort, connectConfig, pgBinaryResolver, errRedirector, outRedirector,
pgStartupWait, overrideWorkingDirectory);
pgStartupWait, overrideWorkingDirectory, userSO);
}

@Override
Expand Down Expand Up @@ -616,7 +626,25 @@ public int hashCode() {
private void system(String... command)
{
try {
final ProcessBuilder builder = new ProcessBuilder(command);
final List<String> commandToExecute;
if (this.userSO.isPresent()) {
commandToExecute = new ArrayList<>();
commandToExecute.add("chmod");
commandToExecute.add("+x");
commandToExecute.add(command[0]);
commandToExecute.add("&&");
commandToExecute.add("su");
commandToExecute.add("-");
commandToExecute.add(this.userSO.get());
commandToExecute.add("-c");
commandToExecute.add("\"");
commandToExecute.addAll(Arrays.asList(command));
commandToExecute.add("\"");
} else {
commandToExecute = Arrays.asList(command);
}

final ProcessBuilder builder = new ProcessBuilder(commandToExecute);
builder.redirectErrorStream(true);
builder.redirectError(errorRedirector);
builder.redirectOutput(outputRedirector);
Expand All @@ -628,7 +656,7 @@ private void system(String... command)
ProcessOutputLogger.logOutput(LoggerFactory.getLogger(LOG_PREFIX + "init-" + instanceId + ":" + FilenameUtils.getName(command[0])), process);
}
if (0 != process.waitFor()) {
throw new IllegalStateException(String.format("Process %s failed%n%s", Arrays.asList(command), IOUtils.toString(process.getErrorStream())));
throw new IllegalStateException(String.format("Process %s failed%n%s", Arrays.asList(commandToExecute), IOUtils.toString(process.getErrorStream())));
}
} catch (final RuntimeException e) { // NOPMD
throw e;
Expand Down