Skip to content

Commit

Permalink
Merge pull request #426 from MrCreosote/dev-retry_writes
Browse files Browse the repository at this point in the history
Add mongo-retrywrites to config.
  • Loading branch information
MrCreosote authored Feb 15, 2024
2 parents 060e222 + 93446ef commit 4fb4758
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
are now located in the `build` directory, including the `manage_auth` script.
* The MongoDB clients have been updated to the most recent version and the service tested
against Mongo 7.
* Added the ``mongo-retrywrites`` configuration setting in ``deploy.cfg``, defaulting to
``false``.
* The docker-compose file has been updated to start an auth server in test mode.

## 0.6.0
Expand Down
3 changes: 3 additions & 0 deletions deploy.cfg.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
mongo-host =
# The name of the mongo database to be used as auth storage.
mongo-db =
# Whether to enable ('true') the MongoDB retryWrites parameter or not (anything other than 'true').
# See https://www.mongodb.com/docs/manual/core/retryable-writes/
mongo-retrywrites = false
# If the mongo database is authenticated, the user name of a read/write account.
mongo-user =
# If the mongo data base is authenticated, the password for the given username.
Expand Down
1 change: 1 addition & 0 deletions deployment/conf/.templates/deployment.cfg.templ
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[authserv2]
mongo-host={{ default .Env.mongo_host "ci-mongo" }}
mongo-db={{ default .Env.mongo_db "auth2" }}
mongo-retrywrites={{ default .Env.mongo_retrywrites "false" }}
mongo-user={{ default .Env.mongo_user "" }}
mongo-pwd={{ default .Env.mongo_pwd "" }}
# The name of the cookie in which tokens should be stored in the browser.
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/us/kbase/auth2/kbase/KBaseAuthConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class KBaseAuthConfig implements AuthStartupConfig {
private static final String KEY_LOG_NAME = "log-name";
private static final String KEY_MONGO_HOST = "mongo-host";
private static final String KEY_MONGO_DB = "mongo-db";
private static final String KEY_MONGO_RETRY_WRITES = "mongo-retrywrites";
private static final String KEY_MONGO_USER = "mongo-user";
private static final String KEY_MONGO_PWD = "mongo-pwd";
private static final String KEY_COOKIE_NAME = "token-cookie-name";
Expand Down Expand Up @@ -68,6 +69,7 @@ public class KBaseAuthConfig implements AuthStartupConfig {
private final SLF4JAutoLogger logger;
private final String mongoHost;
private final String mongoDB;
private final boolean mongoRetryWrites;
private final Optional<String> mongoUser;
private final Optional<char[]> mongoPwd;
private final String cookieName;
Expand Down Expand Up @@ -98,6 +100,7 @@ public KBaseAuthConfig(final Path filepath, final boolean nullLogger)
templateDir = Paths.get(getString(KEY_TEMPLATE_DIR, cfg, true));
mongoHost = getString(KEY_MONGO_HOST, cfg, true);
mongoDB = getString(KEY_MONGO_DB, cfg, true);
mongoRetryWrites = TRUE.equals(getString(KEY_MONGO_RETRY_WRITES, cfg));
mongoUser = Optional.ofNullable(getString(KEY_MONGO_USER, cfg));
Optional<String> mongop = Optional.ofNullable(getString(KEY_MONGO_PWD, cfg));
if (mongoUser.isPresent() ^ mongop.isPresent()) {
Expand Down Expand Up @@ -342,6 +345,11 @@ public String getMongoDatabase() {
return mongoDB;
}

@Override
public boolean getMongoRetryWrites() {
return mongoRetryWrites;
}

@Override
public Optional<String> getMongoUser() {
return mongoUser;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/us/kbase/auth2/service/AuthBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ public AuthBuilder(

private MongoClient buildMongo(final AuthStartupConfig c) throws StorageInitException {
//TODO ZLATER MONGO handle shards & replica sets
final MongoClientSettings.Builder mongoBuilder = MongoClientSettings.builder().applyToClusterSettings(
builder -> builder.hosts(Arrays.asList(new ServerAddress(c.getMongoHost()))));

final MongoClientSettings.Builder mongoBuilder = MongoClientSettings.builder()
.retryWrites(c.getMongoRetryWrites())
.applyToClusterSettings(builder -> builder.hosts(
Arrays.asList(new ServerAddress(c.getMongoHost()))));
try {
if (c.getMongoUser().isPresent()) {
final MongoCredential creds = MongoCredential.createCredential(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface AuthStartupConfig {
Set<IdentityProviderConfig> getIdentityProviderConfigs();
String getMongoHost();
String getMongoDatabase();
boolean getMongoRetryWrites();
// note both or neither for user & pwd
Optional<String> getMongoUser();
Optional<char[]> getMongoPwd();
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/us/kbase/test/auth2/TestConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ public String getMongoHost() {
public String getMongoDatabase() {
return mongoDatabase == null ? System.getProperty(MONGO_DB_KEY) : mongoDatabase;
}

@Override
public boolean getMongoRetryWrites() {
return false;
}

@Override
public Optional<String> getMongoUser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void minimalConfigFromProp() throws Exception {

assertThat("incorrect mongo host", cfg.getMongoHost(), is("localhost:50000"));
assertThat("incorrect mongo db", cfg.getMongoDatabase(), is("mydb"));
assertThat("incorrect retry writes", cfg.getMongoRetryWrites(), is(false));
assertThat("incorrect mongo user", cfg.getMongoUser(), is(Optional.empty()));
assertThat("incorrect mongo pwd", cfg.getMongoPwd(), is(Optional.empty()));
assertThat("incorrect test mode", cfg.isTestModeEnabled(), is(false));
Expand All @@ -96,6 +97,7 @@ public void maximalConfigFromEnv() throws Exception {
"[authserv2]",
"mongo-host= localhost:50000 ",
"mongo-db = mydb ",
"mongo-retrywrites = \t true \t ",
"mongo-user= muser",
"mongo-pwd = mpwd",
"test-mode-enabled= true",
Expand Down Expand Up @@ -141,6 +143,7 @@ public void maximalConfigFromEnv() throws Exception {

assertThat("incorrect mongo host", cfg.getMongoHost(), is("localhost:50000"));
assertThat("incorrect mongo db", cfg.getMongoDatabase(), is("mydb"));
assertThat("incorrect retry writes", cfg.getMongoRetryWrites(), is(true));
assertThat("incorrect mongo user", cfg.getMongoUser(), is(Optional.of("muser")));
assertThat("incorrect mongo pwd",
Arrays.equals(cfg.getMongoPwd().get(), "mpwd".toCharArray()), is(true));
Expand Down Expand Up @@ -224,6 +227,7 @@ public void minimalConfigFromFileNullLogger() throws Exception {

assertThat("incorrect mongo host", cfg.getMongoHost(), is("localhost:50000"));
assertThat("incorrect mongo db", cfg.getMongoDatabase(), is("mydb"));
assertThat("incorrect retry writes", cfg.getMongoRetryWrites(), is(false));
assertThat("incorrect mongo user", cfg.getMongoUser(), is(Optional.empty()));
assertThat("incorrect mongo pwd", cfg.getMongoPwd(), is(Optional.empty()));
assertThat("incorrect test mode", cfg.isTestModeEnabled(), is(false));
Expand All @@ -242,6 +246,7 @@ public void maximalConfigFromFileStdLogger() throws Exception {
"[authserv2]",
"mongo-host= localhost:50000 ",
"mongo-db = mydb ",
"mongo-retrywrites=true",
"mongo-user= muser",
"mongo-pwd = mpwd",
"test-mode-enabled= true",
Expand Down Expand Up @@ -273,6 +278,7 @@ public void maximalConfigFromFileStdLogger() throws Exception {

assertThat("incorrect mongo host", cfg.getMongoHost(), is("localhost:50000"));
assertThat("incorrect mongo db", cfg.getMongoDatabase(), is("mydb"));
assertThat("incorrect retry writes", cfg.getMongoRetryWrites(), is(true));
assertThat("incorrect mongo user", cfg.getMongoUser(), is(Optional.of("muser")));
assertThat("incorrect mongo pwd",
Arrays.equals(cfg.getMongoPwd().get(), "mpwd".toCharArray()), is(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public String getMongoHost() {
public String getMongoDatabase() {
return DB_NAME;
}

@Override
public boolean getMongoRetryWrites() {
return false;
}

@Override
public Optional<String> getMongoUser() {
Expand Down

0 comments on commit 4fb4758

Please sign in to comment.