Skip to content

Commit

Permalink
user and roles with mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmosmann committed Jan 18, 2023
1 parent 3790180 commit 99bc0ca
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
9 changes: 5 additions & 4 deletions Howto.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ try (TransitionWalker.ReachedState<RunningMongodProcess> runningMongod = mongod.
mongo.getDatabase("admin").runCommand(new Document("replSetInitiate", new Document()));
}

com.mongodb.ServerAddress x;
Mongos mongos = new Mongos() {
@Override public Start<MongosArguments> mongosArguments() {
return Start.to(MongosArguments.class).initializedWith(MongosArguments.defaults()
Expand All @@ -292,8 +291,8 @@ try (TransitionWalker.ReachedState<RunningMongodProcess> runningMongod = mongod.
};

try (TransitionWalker.ReachedState<RunningMongosProcess> runningMongos = mongos.start(version)) {
try (MongoClient mongo = new MongoClient(serverAddress(runningMongod.current().getServerAddress()))) {
assertThat(mongo.listDatabaseNames()).contains("admin", "config", "local");
try (MongoClient mongo = new MongoClient(serverAddress(runningMongos.current().getServerAddress()))) {
assertThat(mongo.listDatabaseNames()).contains("admin", "config");
}
}
}
Expand Down Expand Up @@ -466,11 +465,13 @@ public abstract class EnableAuthentication {
// if success there will be no answer, the connection just closes..
runCommand(
clientAdmin.getDatabase("admin"),
new Document("shutdown", 1)
new Document("shutdown", 1).append("force", true)
);
} catch (MongoSocketReadException mx) {
LOGGER.debug("shutdown completed by closing stream");
}

running.shutDownCommandAlreadyExecuted();
}
})
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ private static List<String> getCommandLine(
if (config.auth()) {
LOGGER.info(
"\n---------------------------------------\n"
+ "hint: auth==true starts mongod with authorization enabled, which, if started from scratch must fail, as a "
+ "connect is only possible for known user.\n"
+ "hint: auth==true starts mongod with authorization enabled.\n"
+ "---------------------------------------\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public abstract class RunningMongoProcess extends RunningProcessImpl {
private final InetAddress serverAddress;
private final int port;

private boolean shutDownCommandAlreadyExecuted=false;

protected RunningMongoProcess(
String commandName,
ProcessControl process,
Expand All @@ -62,7 +64,6 @@ protected RunningMongoProcess(
Net net,
StreamProcessor commandOutput,
int mongoProcessId
// boolean withAuthEnabled
) {
super(process, pidFile, timeout, onStop);
this.commandName = commandName;
Expand Down Expand Up @@ -91,7 +92,7 @@ public int stop() {
private void stopInternal() {
if (isAlive()) {
LOGGER.debug("try to stop "+commandName);
if (!sendStopToMongoInstance()) {
if (!shutDownCommandAlreadyExecuted && !sendStopToMongoInstance()) {
LOGGER.warn("could not stop "+commandName+" with db command, try next");
if (!sendKillToProcess()) {
LOGGER.warn("could not stop "+commandName+", try next");
Expand Down Expand Up @@ -130,6 +131,10 @@ protected final boolean sendStopToMongoInstance() {
|| Mongod.sendShutdown(serverAddress, port);
}

public void shutDownCommandAlreadyExecuted() {
this.shutDownCommandAlreadyExecuted = true;
}

interface InstanceFactory<T extends RunningMongoProcess> {
T create(ProcessControl process, Path pidFile, long timeout, Runnable closeAllOutputs, SupportConfig supportConfig, Platform platform, Net net, StreamProcessor commands, int pid);
}
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/de/flapdoodle/embed/mongo/doc/HowToDocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ public Transition<MongodArguments> mongodArguments() {
mongo.getDatabase("admin").runCommand(new Document("replSetInitiate", new Document()));
}

com.mongodb.ServerAddress x;
Mongos mongos = new Mongos() {
@Override public Start<MongosArguments> mongosArguments() {
return Start.to(MongosArguments.class).initializedWith(MongosArguments.defaults()
Expand All @@ -367,8 +366,8 @@ public Transition<MongodArguments> mongodArguments() {
};

try (TransitionWalker.ReachedState<RunningMongosProcess> runningMongos = mongos.start(version)) {
try (MongoClient mongo = new MongoClient(serverAddress(runningMongod.current().getServerAddress()))) {
assertThat(mongo.listDatabaseNames()).contains("admin", "config", "local");
try (MongoClient mongo = new MongoClient(serverAddress(runningMongos.current().getServerAddress()))) {
assertThat(mongo.listDatabaseNames()).contains("admin", "config");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ public Listener withRunningMongod() {
// if success there will be no answer, the connection just closes..
runCommand(
clientAdmin.getDatabase("admin"),
new Document("shutdown", 1)
new Document("shutdown", 1).append("force", true)
);
} catch (MongoSocketReadException mx) {
LOGGER.debug("shutdown completed by closing stream");
}

running.shutDownCommandAlreadyExecuted();
}
})
.build();
Expand Down

0 comments on commit 99bc0ca

Please sign in to comment.