Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Docker/pg-Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ghcr.io/supabase/postgres:17.11.0.004

ENV POSTGRES_DB ''
ENV POSTGRES_USER ''
ENV POSTGRES_PASSWORD ''

COPY z-load-extensions.sh /docker-entrypoint-initdb.d/
File renamed without changes.
9 changes: 9 additions & 0 deletions Docker/vault-key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -euo pipefail

KEY_FILE=./pgsodium_root.key

if [[ ! -f "${KEY_FILE}" ]]; then
head -c 32 /dev/urandom | od -A n -t x1 | tr -d ' \n' > "${KEY_FILE}"
fi
9 changes: 9 additions & 0 deletions Docker/z-load-extensions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Named "z-load-extensions.sh" because postgres docker runs these scripts in alphabetical order,
# and it needs to run after supabase's other scripts have run.

createdb --username="$POSTGRES_USER" "user_transactions"

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" -d user_transactions <<EOF
CREATE EXTENSION supabase_vault CASCADE;
select * FROM pg_extension;
EOF
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.suffolk.litlab.efsp.stdlib;

public record SensitiveInfo<T>(T field) {
public String toString() {
return "SensitiveInfo[value=***]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ public static Optional<String> GetEnv(String envVarName) {
public static boolean exists(String val) {
return val != null && !val.isBlank();
}

public static String chooseNotBlank(String val, String val2) {
if (exists(val)) {
return val;
} else {
return val2;
}
}
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ These instructions are written for Linux (specifically Ubuntu 22.04), steps for
```bash
git clone https://github.com/SuffolkLITLab/EfileProxyServer
cd EfileProxyServer
# Need to run this for the database setup to work
./Docker/run_vault.sh
docker compose -f dev-compose.yml up --watch # For a development build
docker compose build && docker compose up -d # For a production-like build
```
Expand Down Expand Up @@ -73,4 +75,4 @@ different modules for better defined interfaces. Those modules include:
* [TylerEfmClient](TylerEfmClient/README.md)
* [TylerEcf4](TylerEcf4/README.md)
* [TylerEcf5](TylerEcf5/README.md)
* [TrueFilingEcf4](TrueFilingEcf4/README.md)
* [TrueFilingEcf4](TrueFilingEcf4/README.md)
7 changes: 5 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ services:
limits:
memory: 4G
db:
image: "postgres:15"
build:
context: Docker
dockerfile: pg-Dockerfile
shm_size: 256m
env_file: .env
volumes:
- data-volume:/var/lib/postgresql/data
- ./config/postgres-config:/etc/postgresql
# run Docker/vault-key.sh before starting the docker compose database
- ./Docker/pgsodium_root.key:/etc/postgresql-custom/pgsodium_root.key:Z

volumes:
data-volume:
4 changes: 2 additions & 2 deletions proxyserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This server can also be run directly with java, for remote debugging or speed of
* Edit your `.env` file, particularly `POSTGRES_URL` and `POSTGRES_PORT`, to match how you will be running the db
(`POSTGRES_URL` can be `localhost` if just running it locally / through a docker container).
* If you still want to use docker for this, run
`docker run -p 5432:5432 --env-file .env --volume efileproxyserver_data-volume:/var/lib/postgresql/data postgres:15`.
`docker run -p 5432:5432 --env-file .env --volume efileproxyserver_data-volume:/var/lib/postgresql/data ghcr.io/supabase/postgres:15.1.1.58`.
* `mvn package` to build the output JAR artifact.
* `cd proxyserver` (if not already there), and then `./run_server.sh`

Expand All @@ -35,4 +35,4 @@ If using VS Code, add the below configuration to your `.vscode/launch.json`:
...
]
}
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@
* going back to extremely old versions of the code, this class should be completely self contained,
* only using SQL queries from here.
*
* <p>To add a new version:
*
* <ol>
* <li>Increment CURRENT_VERSION
* <li>Add an else-if to the end of updateDatabase() for the new version.
* <li>Write your actual upgrade function (upgradeFromYYToZZ()).
* </ol>
*
* @author brycew
*/
public class DatabaseVersion {

static final int CURRENT_VERSION = 11;
static final int CURRENT_VERSION = 12;
private static Logger log = LoggerFactory.getLogger(DatabaseVersion.class);
private final Connection codeConn;
private final Connection userConn;
Expand Down Expand Up @@ -152,6 +160,8 @@ private boolean updateDatabase(int onDiskVersion) throws NoSuchAlgorithmExceptio
update9To10();
} else if (onDiskVersion == 10) {
update10To11();
} else if (onDiskVersion == 11) {
update11To12();
}
setSchemaVersion(onDiskVersion + 1);
userConn.commit();
Expand Down Expand Up @@ -608,4 +618,30 @@ public void update10To11() throws SQLException {
}
codeConn.commit();
}

public void update11To12() throws SQLException {
final String alterMsgTable =
"""
ALTER TABLE message_settings
ADD COLUMN sendgrid_vault_id uuid,
ADD COLUMN is_default boolean DEFAULT true,
DROP CONSTRAINT message_settings_pkey
""";
final String newPkey =
"""
ALTER TABLE message_settings
ADD PRIMARY KEY (server_id, from_email);
""";
final String newSubmittedColumn =
"""
ALTER TABLE submitted_filings
ADD COLUMN from_email text
""";
try (Statement st = userConn.createStatement()) {
st.executeUpdate(alterMsgTable);
st.executeUpdate(newPkey);
st.executeUpdate(newSubmittedColumn);
}
userConn.commit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public void createTablesIfAbsent() throws SQLException {
String createQuery =
"""
CREATE TABLE message_settings (
"server_id" uuid PRIMARY KEY, "from_email" text, "subject_line" text,
"email_template" text, "email_confirmation" text, "confirmation_subject_line" text)\
"server_id" uuid, "from_email" text, "is_default" boolean DEFAULT true, "sendgrid_vault_id" uuid, "subject_line" text,
"email_template" text, "email_confirmation" text, "confirmation_subject_line" text, PRIMARY KEY(server_id, from_email))\
""";
try (Statement createSt = conn.createStatement()) {
int retVal = createSt.executeUpdate(createQuery);
Expand All @@ -60,17 +60,19 @@ CREATE TABLE message_settings (

public void updateTable(MessageInfo info) throws SQLException {
updateTable(
info.serverId,
info.fromEmail,
info.subjectLine,
info.emailResponseTemplate,
info.confirmationSubjectLine,
info.emailConfirmation);
info.serverId(),
info.fromEmail(),
false,
info.subjectLine(),
info.emailResponseTemplate(),
info.confirmationSubjectLine(),
info.emailConfirmation());
}

public void updateTable(
UUID serverId,
String toEmail,
boolean isDefault,
String subjectLine,
String emailTemplate,
String confirmationSubjectLine,
Expand All @@ -84,24 +86,25 @@ public void updateTable(
String insertIntoTable =
"""
INSERT INTO message_settings (
"server_id", "from_email", "subject_line", "email_template", "confirmation_subject_line", "email_confirmation"
"server_id", "from_email", "is_default", "subject_line", "email_template", "confirmation_subject_line", "email_confirmation"
) VALUES (
?, ?, ?, ?, ?, ?) ON CONFLICT (server_id) DO UPDATE SET from_email=?, subject_line=?,
?, ?, ?, ?, ?, ?, ?) ON CONFLICT (server_id, from_email) DO UPDATE SET is_default=?, subject_line=?,
email_template=?, confirmation_subject_line=?, email_confirmation=?
""";

try (PreparedStatement insertSt = conn.prepareStatement(insertIntoTable)) {
insertSt.setObject(1, serverId);
insertSt.setString(2, toEmail);
insertSt.setString(3, subjectLine);
insertSt.setString(4, emailTemplate);
insertSt.setString(5, confirmationSubjectLine);
insertSt.setString(6, emailConfirmation);
insertSt.setString(7, toEmail);
insertSt.setString(8, subjectLine);
insertSt.setString(9, emailTemplate);
insertSt.setString(10, confirmationSubjectLine);
insertSt.setString(11, emailConfirmation);
insertSt.setBoolean(3, isDefault);
insertSt.setString(4, subjectLine);
insertSt.setString(5, emailTemplate);
insertSt.setString(6, confirmationSubjectLine);
insertSt.setString(7, emailConfirmation);
insertSt.setBoolean(8, isDefault);
insertSt.setString(9, subjectLine);
insertSt.setString(10, emailTemplate);
insertSt.setString(11, confirmationSubjectLine);
insertSt.setString(12, emailConfirmation);
insertSt.executeUpdate();
}
}
Expand All @@ -114,19 +117,24 @@ public Optional<MessageInfo> findMessageInfo(UUID serverId) {

String query =
"""
SELECT server_id, from_email, subject_line, email_template, confirmation_subject_line,
SELECT server_id, from_email, sendgrid_vault_id, subject_line, email_template, confirmation_subject_line,
email_confirmation
FROM message_settings
WHERE server_id=?\
WHERE server_id=? AND is_default=true\
""";
try (PreparedStatement st = conn.prepareStatement(query)) {
st.setObject(1, serverId);
ResultSet rs = st.executeQuery();
if (rs.next()) {
String sendgridKey = null;
if (rs.getObject(3) != null) {
sendgridKey = findSendgridKey((UUID) rs.getObject(3)).orElse(null);
}
MessageInfo info =
new MessageInfo(
(UUID) rs.getObject(1),
rs.getString(2),
sendgridKey,
rs.getString(3),
rs.getString(4),
rs.getString(5),
Expand All @@ -140,4 +148,72 @@ public Optional<MessageInfo> findMessageInfo(UUID serverId) {
log.warn("No settings for {}", serverId);
return Optional.empty();
}

public Optional<MessageInfo> findMessageInfo(UUID serverId, String fromEmail) {
if (conn == null) {
log.error("Connection in addToTable wasn't open yet!");
return Optional.empty();
}

String query =
"""
SELECT server_id, from_email, sendgrid_vault_id, subject_line, email_template, confirmation_subject_line,
email_confirmation
FROM message_settings
WHERE server_id=? AND from_email=?\
""";
try (PreparedStatement st = conn.prepareStatement(query)) {
st.setObject(1, serverId);
st.setString(2, fromEmail);
ResultSet rs = st.executeQuery();
if (rs.next()) {
String sendgridKey = null;
if (rs.getObject(3) != null) {
sendgridKey = findSendgridKey((UUID) rs.getObject(3)).orElse(null);
}
MessageInfo info =
new MessageInfo(
(UUID) rs.getObject(1),
rs.getString(2),
sendgridKey,
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getString(6));
return Optional.of(info);
}
} catch (SQLException e) {
log.error("Exception getting message settings", e);
return Optional.empty();
}
log.warn("No settings for {} with from email {}, falling back to default settings", serverId);
return findMessageInfo(serverId);
}

private Optional<String> findSendgridKey(UUID vaultId) {
if (conn == null) {
log.error("Connection in addToTable wasn't open yet!");
return Optional.empty();
}

String query =
"""
SELECT id, decrypted_secret
FROM vault.decrypted_secrets
WHERE id=?\
""";
try (PreparedStatement st = conn.prepareStatement(query)) {
st.setObject(1, vaultId);
ResultSet rs = st.executeQuery();
if (rs.next()) {
if (rs.getString(2) != null) {
return Optional.of(rs.getString(2));
}
}
return Optional.empty();
} catch (SQLException e) {
log.error("Exception getting message settings", e);
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ CREATE TABLE submitted_filings (
"neutral_msg_template" text,
"neutral_msg_subject" text,
"case_title" text,
"envelope_id" text)\
"envelope_id" text,
"from_email" text)\
""";
try (Statement createSt = conn.createStatement()) {
log.info("Full statement: {}", createQuery);
Expand Down Expand Up @@ -140,12 +141,13 @@ INSERT INTO submitted_filings (
"neutral_msg_template",
"neutral_msg_subject",
"case_title",
"envelope_id"
"envelope_id",
"from_email"
) VALUES (
?, ?,
?, ?, ?, ?,
?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?)\
?, ?, ?, ?, ?, ?, ?, ?, ?)\
""";
try (PreparedStatement insertSt = conn.prepareStatement(insertIntoTable)) {
insertSt.setObject(1, filingPartyId);
Expand All @@ -167,6 +169,7 @@ INSERT INTO submitted_filings (
insertSt.setString(16, tmpls.neutralSubject());
insertSt.setString(17, caseTitle);
insertSt.setString(18, envelopeId);
insertSt.setString(19, tmpls.fromEmail());
insertSt.executeUpdate();
}
}
Expand Down
Loading
Loading