Skip to content

Commit

Permalink
Add support for Bitnami environment variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottfrederick committed Nov 22, 2023
1 parent 82d4bdf commit 6fe15ac
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CassandraEnvironment {
private final String datacenter;

CassandraEnvironment(Map<String, String> env) {
this.datacenter = env.getOrDefault("CASSANDRA_DC", "datacenter1");
this.datacenter = env.getOrDefault("CASSANDRA_DC", env.getOrDefault("CASSANDRA_DATACENTER", "datacenter1"));
}

String getDatacenter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class MariaDbEnvironment {

Expand All @@ -52,7 +53,7 @@ private String extractPassword(Map<String, String> env) {
Assert.state(!env.containsKey("MYSQL_RANDOM_ROOT_PASSWORD"), "MYSQL_RANDOM_ROOT_PASSWORD is not supported");
Assert.state(!env.containsKey("MARIADB_ROOT_PASSWORD_HASH"), "MARIADB_ROOT_PASSWORD_HASH is not supported");
boolean allowEmpty = env.containsKey("MARIADB_ALLOW_EMPTY_PASSWORD")
|| env.containsKey("MYSQL_ALLOW_EMPTY_PASSWORD");
|| env.containsKey("MYSQL_ALLOW_EMPTY_PASSWORD") || env.containsKey("ALLOW_EMPTY_PASSWORD");
String password = env.get("MARIADB_PASSWORD");
password = (password != null) ? password : env.get("MYSQL_PASSWORD");
password = (password != null) ? password : env.get("MARIADB_ROOT_PASSWORD");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class MongoEnvironment {

Expand All @@ -40,8 +41,8 @@ class MongoEnvironment {
"MONGO_INITDB_ROOT_USERNAME_FILE is not supported");
Assert.state(!env.containsKey("MONGO_INITDB_ROOT_PASSWORD_FILE"),
"MONGO_INITDB_ROOT_PASSWORD_FILE is not supported");
this.username = env.get("MONGO_INITDB_ROOT_USERNAME");
this.password = env.get("MONGO_INITDB_ROOT_PASSWORD");
this.username = env.getOrDefault("MONGO_INITDB_ROOT_USERNAME", env.get("MONGO_ROOT_USERNAME"));
this.password = env.getOrDefault("MONGO_INITDB_ROOT_PASSWORD", env.get("MONGO_ROOT_PASSWORD"));
this.database = env.get("MONGO_INITDB_DATABASE");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class MySqlEnvironment {

Expand All @@ -44,7 +45,7 @@ class MySqlEnvironment {

private String extractPassword(Map<String, String> env) {
Assert.state(!env.containsKey("MYSQL_RANDOM_ROOT_PASSWORD"), "MYSQL_RANDOM_ROOT_PASSWORD is not supported");
boolean allowEmpty = env.containsKey("MYSQL_ALLOW_EMPTY_PASSWORD");
boolean allowEmpty = env.containsKey("MYSQL_ALLOW_EMPTY_PASSWORD") || env.containsKey("ALLOW_EMPTY_PASSWORD");
String password = env.get("MYSQL_PASSWORD");
password = (password != null) ? password : env.get("MYSQL_ROOT_PASSWORD");
Assert.state(StringUtils.hasLength(password) || allowEmpty, "No MySQL password found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class PostgresEnvironment {

Expand All @@ -37,14 +38,14 @@ class PostgresEnvironment {
private final String database;

PostgresEnvironment(Map<String, String> env) {
this.username = env.getOrDefault("POSTGRES_USER", "postgres");
this.username = env.getOrDefault("POSTGRES_USER", env.getOrDefault("POSTGRESQL_USER", "postgres"));
this.password = extractPassword(env);
this.database = env.getOrDefault("POSTGRES_DB", this.username);
this.database = env.getOrDefault("POSTGRES_DB", env.getOrDefault("POSTGRESQL_DB", this.username));
}

private String extractPassword(Map<String, String> env) {
String password = env.get("POSTGRES_PASSWORD");
Assert.state(StringUtils.hasLength(password), "No POSTGRES_PASSWORD defined");
String password = env.getOrDefault("POSTGRES_PASSWORD", env.get("POSTGRESQL_PASSWORD"));
Assert.state(StringUtils.hasLength(password), "PostgreSQL password must be provided");
return password;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class RabbitEnvironment {

Expand All @@ -32,8 +33,8 @@ class RabbitEnvironment {
private final String password;

RabbitEnvironment(Map<String, String> env) {
this.username = env.getOrDefault("RABBITMQ_DEFAULT_USER", "guest");
this.password = env.getOrDefault("RABBITMQ_DEFAULT_PASS", "guest");
this.username = env.getOrDefault("RABBITMQ_DEFAULT_USER", env.getOrDefault("RABBITMQ_USERNAME", "guest"));
this.password = env.getOrDefault("RABBITMQ_DEFAULT_PASS", env.getOrDefault("RABBITMQ_PASSWORD", "guest"));
}

String getUsername() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void runCreatesConnectionDetails() {
assertThat(node.port()).isGreaterThan(0);
assertThat(connectionDetails.getUsername()).isNull();
assertThat(connectionDetails.getPassword()).isNull();
assertThat(connectionDetails.getLocalDatacenter()).isEqualTo("dc1");
assertThat(connectionDetails.getLocalDatacenter()).isEqualTo("testdc1");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void runCreatesConnectionDetails() {
assertThat(node.port()).isGreaterThan(0);
assertThat(connectionDetails.getUsername()).isNull();
assertThat(connectionDetails.getPassword()).isNull();
assertThat(connectionDetails.getLocalDatacenter()).isEqualTo("dc1");
assertThat(connectionDetails.getLocalDatacenter()).isEqualTo("testdc1");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ void getDatacenterWhenDatacenterIsNotSet() {
assertThat(environment.getDatacenter()).isEqualTo("datacenter1");
}

@Test
void getDatacenterWhenDcIsSet() {
CassandraEnvironment environment = new CassandraEnvironment(Map.of("CASSANDRA_DC", "testdc1"));
assertThat(environment.getDatacenter()).isEqualTo("testdc1");
}

@Test
void getDatacenterWhenDatacenterIsSet() {
CassandraEnvironment environment = new CassandraEnvironment(Map.of("CASSANDRA_DC", "dc1"));
assertThat(environment.getDatacenter()).isEqualTo("dc1");
CassandraEnvironment environment = new CassandraEnvironment(Map.of("CASSANDRA_DATACENTER", "testdc1"));
assertThat(environment.getDatacenter()).isEqualTo("testdc1");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @author Andy Wilkinson
* @author Phillip Webb
* @author Jinseong Hwang
* @author Scott Frederick
*/
class MariaDbEnvironmentTests {

Expand Down Expand Up @@ -130,6 +131,13 @@ void getPasswordWhenHasMariadbPasswordAndMysqlRootPassword() {
assertThat(environment.getPassword()).isEqualTo("secret");
}

@Test
void getPasswordWhenHasNoPasswordAndAllowEmptyPassword() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Map.of("ALLOW_EMPTY_PASSWORD", "true", "MARIADB_DATABASE", "db"));
assertThat(environment.getPassword()).isEmpty();
}

@Test
void getPasswordWhenHasNoPasswordAndMariadbAllowEmptyPassword() {
MariaDbEnvironment environment = new MariaDbEnvironment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @author Andy Wilkinson
* @author Phillip Webb
* @author Jinseong Hwang
* @author Scott Frederick
*/
class MySqlEnvironmentTests {

Expand Down Expand Up @@ -86,6 +87,13 @@ void getPasswordWhenHasNoPasswordAndMysqlAllowEmptyPassword() {
assertThat(environment.getPassword()).isEmpty();
}

@Test
void getPasswordWhenHasNoPasswordAndAllowEmptyPassword() {
MySqlEnvironment environment = new MySqlEnvironment(
Map.of("ALLOW_EMPTY_PASSWORD", "true", "MYSQL_DATABASE", "db"));
assertThat(environment.getPassword()).isEmpty();
}

@Test
void getDatabaseWhenHasMysqlDatabase() {
MySqlEnvironment environment = new MySqlEnvironment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class PostgresEnvironmentTests {

@Test
void createWhenNoPostgresPasswordThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new PostgresEnvironment(Collections.emptyMap()))
.withMessage("No POSTGRES_PASSWORD defined");
.withMessage("PostgreSQL password must be provided");
}

@Test
Expand All @@ -45,37 +46,76 @@ void getUsernameWhenNoPostgresUser() {
assertThat(environment.getUsername()).isEqualTo("postgres");
}

@Test
void getUsernameWhenNoPostgresqlUser() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("postgres");
}

@Test
void getUsernameWhenHasPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRES_USER", "me", "POSTGRES_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("me");
}

@Test
void getUsernameWhenHasPostgresqlUser() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRESQL_USER", "me", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("me");
}

@Test
void getPasswordWhenHasPostgresPassword() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_PASSWORD", "secret"));
assertThat(environment.getPassword()).isEqualTo("secret");
}

@Test
void getPasswordWhenHasPostgresqlPassword() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getPassword()).isEqualTo("secret");
}

@Test
void getDatabaseWhenNoPostgresDbOrPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("postgres");
}

@Test
void getDatabaseWhenNoPostgresqlDbOrPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("postgres");
}

@Test
void getDatabaseWhenNoPostgresDbAndPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRES_USER", "me", "POSTGRES_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("me");
}

@Test
void getDatabaseWhenNoPostgresqlDbAndPostgresUser() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRESQL_USER", "me", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("me");
}

@Test
void getDatabaseWhenHasPostgresDb() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRES_DB", "db", "POSTGRES_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("db");
}

@Test
void getDatabaseWhenHasPostgresqlDb() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRESQL_DB", "db", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("db");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class RabbitEnvironmentTests {

Expand All @@ -44,6 +45,12 @@ void getUsernameWhenHasRabbitmqDefaultUser() {
assertThat(environment.getUsername()).isEqualTo("me");
}

@Test
void getUsernameWhenHasRabbitmqUsername() {
RabbitEnvironment environment = new RabbitEnvironment(Map.of("RABBITMQ_USERNAME", "me"));
assertThat(environment.getUsername()).isEqualTo("me");
}

@Test
void getUsernameWhenNoRabbitmqDefaultPass() {
RabbitEnvironment environment = new RabbitEnvironment(Collections.emptyMap());
Expand All @@ -56,4 +63,10 @@ void getUsernameWhenHasRabbitmqDefaultPass() {
assertThat(environment.getPassword()).isEqualTo("secret");
}

@Test
void getUsernameWhenHasRabbitmqPassword() {
RabbitEnvironment environment = new RabbitEnvironment(Map.of("RABBITMQ_PASSWORD", "secret"));
assertThat(environment.getPassword()).isEqualTo("secret");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ services:
- '9042'
environment:
- 'CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch'
- 'CASSANDRA_DC=dc1'
- 'CASSANDRA_DATACENTER=testdc1'
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ services:
- 'HEAP_NEWSIZE=128M'
- 'MAX_HEAP_SIZE=1024M'
- 'CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch'
- 'CASSANDRA_DC=dc1'
- 'CASSANDRA_DC=testdc1'
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ services:
ports:
- '27017'
environment:
- 'MONGO_INITDB_ROOT_USERNAME=root'
- 'MONGO_INITDB_ROOT_PASSWORD=secret'
- 'MONGO_INITDB_DATABASE=mydatabase'
- 'MONGO_ROOT_USERNAME=root'
- 'MONGO_ROOT_PASSWORD=secret'
- 'MONGO_DATABASE=mydatabase'
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ services:
ports:
- '5432'
environment:
- 'POSTGRES_USER=myuser'
- 'POSTGRES_DB=mydatabase'
- 'POSTGRES_PASSWORD=secret'
- 'POSTGRESQL_USER=myuser'
- 'POSTGRESQL_DB=mydatabase'
- 'POSTGRESQL_PASSWORD=secret'

0 comments on commit 6fe15ac

Please sign in to comment.