forked from nzymedefense/nzyme
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pushing current state. All sorts of work.
- Loading branch information
1 parent
511bad5
commit 0322426
Showing
54 changed files
with
1,089 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/horse/wtf/nzyme/configuration/db/BaseConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package horse.wtf.nzyme.configuration.db; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import org.joda.time.DateTime; | ||
|
||
@AutoValue | ||
public abstract class BaseConfiguration { | ||
|
||
public abstract String tapSecret(); | ||
public abstract DateTime updatedAt(); | ||
|
||
public static BaseConfiguration create(String tapSecret, DateTime updatedAt) { | ||
return builder() | ||
.tapSecret(tapSecret) | ||
.updatedAt(updatedAt) | ||
.build(); | ||
} | ||
|
||
public static Builder builder() { | ||
return new AutoValue_BaseConfiguration.Builder(); | ||
} | ||
|
||
@AutoValue.Builder | ||
public abstract static class Builder { | ||
public abstract Builder tapSecret(String tapSecret); | ||
|
||
public abstract Builder updatedAt(DateTime updatedAt); | ||
|
||
public abstract BaseConfiguration build(); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/horse/wtf/nzyme/configuration/db/BaseConfigurationMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package horse.wtf.nzyme.configuration.db; | ||
|
||
import org.jdbi.v3.core.mapper.RowMapper; | ||
import org.jdbi.v3.core.statement.StatementContext; | ||
import org.joda.time.DateTime; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
public class BaseConfigurationMapper implements RowMapper<BaseConfiguration> { | ||
|
||
@Override | ||
public BaseConfiguration map(ResultSet rs, StatementContext ctx) throws SQLException { | ||
return BaseConfiguration.create( | ||
rs.getString("tap_secret"), | ||
new DateTime(rs.getTimestamp("updated_at")) | ||
); | ||
} | ||
|
||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/horse/wtf/nzyme/configuration/db/BaseConfigurationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package horse.wtf.nzyme.configuration.db; | ||
|
||
import horse.wtf.nzyme.NzymeLeader; | ||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.joda.time.DateTime; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public class BaseConfigurationService { | ||
|
||
private static final Logger LOG = LogManager.getLogger(BaseConfigurationService.class); | ||
|
||
private final NzymeLeader nzyme; | ||
private final AtomicReference<BaseConfiguration> cache; | ||
|
||
private boolean initialized = false; | ||
|
||
public BaseConfigurationService(NzymeLeader nzyme) { | ||
this.nzyme = nzyme; | ||
|
||
this.cache = new AtomicReference<>(null); | ||
} | ||
|
||
public void initialize() { | ||
// There should always only be one row. | ||
long count = nzyme.getDatabase().withHandle(handle -> | ||
handle.createQuery("SELECT COUNT(*) FROM base_configuration") | ||
.mapTo(Long.class) | ||
.first() | ||
); | ||
|
||
if (count == 0) { | ||
// Create initial row. | ||
LOG.info("Creating initial default base configuration."); | ||
|
||
nzyme.getDatabase().useHandle(handle -> | ||
handle.createUpdate("INSERT INTO base_configuration(tap_secret, updated_at) " + | ||
"VALUES(:tap_secret, :timestamp)") | ||
.bind("tap_secret", generateTapSecret()) | ||
.bind("timestamp", DateTime.now()) | ||
.execute() | ||
); | ||
} | ||
|
||
if (count > 1) { | ||
LOG.warn("More than one base configuration detected. This should never happen and can cause issues."); | ||
} | ||
|
||
invalidateAndUpdateCache(); | ||
|
||
initialized = true; | ||
} | ||
|
||
public BaseConfiguration getConfiguration() { | ||
return cache.get(); | ||
} | ||
|
||
public void setTapSecret(String secret) { | ||
nzyme.getDatabase().useHandle(handle -> { | ||
handle.createUpdate("UPDATE base_configuration SET " + | ||
"tap_secret = :tap_secret, " + | ||
"updated_at = (current_timestamp at time zone 'UTC')") | ||
.bind("tap_secret", secret) | ||
.execute(); | ||
}); | ||
|
||
invalidateAndUpdateCache(); | ||
} | ||
|
||
public boolean isInitialized() { | ||
return initialized; | ||
} | ||
|
||
public String generateTapSecret() { | ||
return RandomStringUtils.random(64, true, true); | ||
} | ||
|
||
private void invalidateAndUpdateCache() { | ||
BaseConfiguration config = nzyme.getDatabase().withHandle(handle -> | ||
handle.createQuery("SELECT tap_secret, updated_at FROM base_configuration LIMIT 1") | ||
.mapTo(BaseConfiguration.class) | ||
.one() | ||
); | ||
|
||
if (config == null) { | ||
LOG.fatal("!!!!!!!! No base configuration found. This is an unrecoverable error. !!!!!!!!"); | ||
} | ||
|
||
this.cache.set(config); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.