Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(FSADT1-1281): adding encryption to the connection #1341

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
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
18 changes: 9 additions & 9 deletions backend/src/main/java/ca/bc/gov/app/ApplicationConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public final class ApplicationConstant {
btc.business_type_code as business_type,
sd.incorporation_number,
sd.client_number,
sd.organization_name,
pgp_sym_decrypt(sd.organization_name, current_setting('cryptic.key')) as organization_name,
ctc.client_type_code as client_type,
ctc.description as client_type_desc,
sd.good_standing_ind as good_standing,
sd.birthdate,
pgp_sym_decrypt(sd.birthdate, current_setting('cryptic.key'))::date as birthdate,
dc.district_code as district,
dc.district_code || ' - ' || dc.description as district_desc
FROM nrfc.submission s
Expand All @@ -73,10 +73,10 @@ public final class ApplicationConstant {
ROW_NUMBER() OVER (order by sc.submission_contact_id ) AS index,
sc.contact_type_code,
ctc.description as contact_desc,
sc.first_name,
sc.last_name,
sc.business_phone_number,
sc.email_address,
pgp_sym_decrypt(sc.first_name, current_setting('cryptic.key')) as first_name,
pgp_sym_decrypt(sc.last_name, current_setting('cryptic.key')) as last_name,
pgp_sym_decrypt(sc.business_phone_number, current_setting('cryptic.key')) as business_phone_number,
pgp_sym_decrypt(sc.email_address, current_setting('cryptic.key')) as email_address,
(
select STRING_AGG(sl.location_name,', ') as locations
from nrfc.submission_location sl
Expand All @@ -94,13 +94,13 @@ select STRING_AGG(sl.location_name,', ') as locations
public static final String SUBMISSION_LOCATION_QUERY = """
SELECT
ROW_NUMBER() OVER (order by sl.submission_location_id ) AS index,
sl.street_address,
pgp_sym_decrypt(sl.street_address, current_setting('cryptic.key')) as street_address,
sl.country_code,
cc.description as country_desc,
sl.province_code,
pc.description as province_desc,
sl.city_name,
sl.postal_code,
pgp_sym_decrypt(sl.city_name, current_setting('cryptic.key')) as city_name,
pgp_sym_decrypt(sl.postal_code, current_setting('cryptic.key')) as postal_code,
sl.location_name
FROM nrfc.submission_location sl
left join nrfc.country_code cc on cc.country_code = sl.country_code
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package ca.bc.gov.app.converters;

import ca.bc.gov.app.entity.client.SubmissionContactEntity;
import ca.bc.gov.app.util.DatabaseCryptoUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.data.r2dbc.mapping.event.AfterConvertCallback;
import org.springframework.data.r2dbc.mapping.event.BeforeConvertCallback;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class SubmissionContactEntityConverter implements
BeforeConvertCallback<SubmissionContactEntity>,
AfterConvertCallback<SubmissionContactEntity> {

private final DatabaseCryptoUtil cryptoUtil;

@Override
public Publisher<SubmissionContactEntity> onAfterConvert(
SubmissionContactEntity entity,
SqlIdentifier table
) {
return cryptoUtil
.decryptAs(entity.getOriginalFirstName(), String.class)
.map(entity::withFirstName)
.defaultIfEmpty(entity)
.flatMap(updatedEntity ->
cryptoUtil
.decryptAs(updatedEntity.getOriginalLastName(), String.class)
.map(updatedEntity::withLastName)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil
.decryptAs(updatedEntity.getOriginalBusinessPhoneNumber(), String.class)
.map(updatedEntity::withBusinessPhoneNumber)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil
.decryptAs(updatedEntity.getOriginalSecondaryPhoneNumber(), String.class)
.map(updatedEntity::withSecondaryPhoneNumber)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil
.decryptAs(updatedEntity.getOriginalFaxNumber(), String.class)
.map(updatedEntity::withFaxNumber)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil
.decryptAs(updatedEntity.getOriginalEmailAddress(), String.class)
.map(updatedEntity::withEmailAddress)
.defaultIfEmpty(updatedEntity)
);
}

@Override
public Publisher<SubmissionContactEntity> onBeforeConvert(
SubmissionContactEntity entity,
SqlIdentifier table
) {
return cryptoUtil
.encryptFromString(entity.getFirstName())
.map(entity::withOriginalFirstName)
.defaultIfEmpty(entity)
.flatMap(updatedEntity ->
cryptoUtil
.encryptFromString(updatedEntity.getLastName())
.map(updatedEntity::withOriginalLastName)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil
.encryptFromString(updatedEntity.getBusinessPhoneNumber())
.map(updatedEntity::withOriginalBusinessPhoneNumber)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil
.encryptFromString(updatedEntity.getSecondaryPhoneNumber())
.map(updatedEntity::withOriginalSecondaryPhoneNumber)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil
.encryptFromString(updatedEntity.getFaxNumber())
.map(updatedEntity::withOriginalFaxNumber)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil
.encryptFromString(updatedEntity.getEmailAddress())
.map(updatedEntity::withOriginalEmailAddress)
.defaultIfEmpty(updatedEntity)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package ca.bc.gov.app.converters;

import ca.bc.gov.app.entity.client.SubmissionDetailEntity;
import ca.bc.gov.app.util.DatabaseCryptoUtil;
import java.time.LocalDate;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.data.r2dbc.mapping.event.AfterConvertCallback;
import org.springframework.data.r2dbc.mapping.event.BeforeConvertCallback;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class SubmissionDetailEntityConverter implements
BeforeConvertCallback<SubmissionDetailEntity>,
AfterConvertCallback<SubmissionDetailEntity> {

private final DatabaseCryptoUtil cryptoUtil;

@Override
public Publisher<SubmissionDetailEntity> onAfterConvert(
SubmissionDetailEntity entity,
SqlIdentifier table
) {
return cryptoUtil
.decryptAs(entity.getOriginalOrganizationName(), String.class)
.map(entity::withOrganizationName)
.defaultIfEmpty(entity)
.flatMap(updatedEntity ->
cryptoUtil.decryptAs(updatedEntity.getOriginalClientIdentification(), String.class)
.map(updatedEntity::withClientIdentification)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.decryptAs(updatedEntity.getOriginalLastName(), String.class)
.map(updatedEntity::withLastName)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.decryptAs(updatedEntity.getOriginalMiddleName(), String.class)
.map(updatedEntity::withMiddleName)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.decryptAs(updatedEntity.getOriginalFirstName(), String.class)
.map(updatedEntity::withFirstName)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.decryptAs(updatedEntity.getOriginalBirthdate(), LocalDate.class)
.map(updatedEntity::withBirthdate)
.defaultIfEmpty(updatedEntity)
);
}

@Override
public Publisher<SubmissionDetailEntity> onBeforeConvert(
SubmissionDetailEntity entity,
SqlIdentifier table
) {
return cryptoUtil
.encryptFromString(entity.getOrganizationName())
.map(entity::withOriginalOrganizationName)
.defaultIfEmpty(entity)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromString(updatedEntity.getClientIdentification())
.map(updatedEntity::withOriginalClientIdentification)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromString(updatedEntity.getLastName())
.map(updatedEntity::withOriginalLastName)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromString(updatedEntity.getMiddleName())
.map(updatedEntity::withOriginalMiddleName)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromString(updatedEntity.getFirstName())
.map(updatedEntity::withOriginalFirstName)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromLocalDate(updatedEntity.getBirthdate())
.map(updatedEntity::withOriginalBirthdate)
.defaultIfEmpty(updatedEntity)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package ca.bc.gov.app.converters;

import ca.bc.gov.app.entity.client.SubmissionLocationEntity;
import ca.bc.gov.app.util.DatabaseCryptoUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.data.r2dbc.mapping.event.AfterConvertCallback;
import org.springframework.data.r2dbc.mapping.event.BeforeConvertCallback;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class SubmissionLocationEntityConverter implements
BeforeConvertCallback<SubmissionLocationEntity>,
AfterConvertCallback<SubmissionLocationEntity>
{

private final DatabaseCryptoUtil cryptoUtil;

@Override
public Publisher<SubmissionLocationEntity> onAfterConvert(
SubmissionLocationEntity entity,
SqlIdentifier table
) {
return cryptoUtil
.decryptAs(entity.getOriginalStreetAddress(), String.class)
.map(entity::withStreetAddress)
.defaultIfEmpty(entity)
.flatMap(updatedEntity ->
cryptoUtil.decryptAs(updatedEntity.getOriginalComplementaryAddress1(), String.class)
.map(updatedEntity::withComplementaryAddress1)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.decryptAs(updatedEntity.getOriginalComplementaryAddress2(), String.class)
.map(updatedEntity::withComplementaryAddress2)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.decryptAs(updatedEntity.getOriginalCityName(), String.class)
.map(updatedEntity::withCityName)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.decryptAs(updatedEntity.getOriginalPostalCode(), String.class)
.map(updatedEntity::withPostalCode)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil
.decryptAs(updatedEntity.getOriginalBusinessPhoneNumber(), String.class)
.map(updatedEntity::withBusinessPhoneNumber)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil
.decryptAs(updatedEntity.getOriginalSecondaryPhoneNumber(), String.class)
.map(updatedEntity::withSecondaryPhoneNumber)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil
.decryptAs(updatedEntity.getOriginalFaxNumber(), String.class)
.map(updatedEntity::withFaxNumber)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil
.decryptAs(updatedEntity.getOriginalEmailAddress(), String.class)
.map(updatedEntity::withEmailAddress)
.defaultIfEmpty(updatedEntity)
);
}

@Override
public Publisher<SubmissionLocationEntity> onBeforeConvert(SubmissionLocationEntity entity,
SqlIdentifier table) {
return
cryptoUtil
.encryptFromString(entity.getStreetAddress())
.map(entity::withOriginalStreetAddress)
.defaultIfEmpty(entity)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromString(updatedEntity.getComplementaryAddress1())
.map(updatedEntity::withOriginalComplementaryAddress1)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromString(updatedEntity.getComplementaryAddress2())
.map(updatedEntity::withOriginalComplementaryAddress2)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromString(updatedEntity.getCityName())
.map(updatedEntity::withOriginalCityName)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromString(updatedEntity.getPostalCode())
.map(updatedEntity::withOriginalPostalCode)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromString(updatedEntity.getBusinessPhoneNumber())
.map(updatedEntity::withOriginalBusinessPhoneNumber)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromString(updatedEntity.getSecondaryPhoneNumber())
.map(updatedEntity::withOriginalSecondaryPhoneNumber)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromString(updatedEntity.getFaxNumber())
.map(updatedEntity::withOriginalFaxNumber)
.defaultIfEmpty(updatedEntity)
)
.flatMap(updatedEntity ->
cryptoUtil.encryptFromString(updatedEntity.getEmailAddress())
.map(updatedEntity::withOriginalEmailAddress)
.defaultIfEmpty(updatedEntity)
);
}
}
Loading
Loading