Skip to content

Commit

Permalink
fixed missing schema in encryption issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mekilis committed Dec 7, 2024
1 parent a0c9343 commit 06df51f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion database/postgres/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func NewEndpointRepo(db database.Database, ca cache.Cache) datastore.EndpointRep

// checkEncryptionStatus checks if any row is already encrypted.
func checkEncryptionStatus(db *sqlx.DB) (bool, error) {
checkQuery := "SELECT is_encrypted FROM endpoints WHERE is_encrypted=TRUE LIMIT 1;"
checkQuery := "SELECT is_encrypted FROM convoy.endpoints WHERE is_encrypted=TRUE LIMIT 1;"
var isEncrypted bool
err := db.Get(&isEncrypted, checkQuery)
if err != nil && err.Error() != "sql: no rows in result set" {
Expand Down
10 changes: 5 additions & 5 deletions internal/pkg/keys/encrypter_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func InitEncryption(db database.Database, km KeyManager, encryptionKey string) e
// checkEncryptionStatus checks if the column is already encrypted.
func checkEncryptionStatus(tx *sqlx.Tx, table string) (bool, error) {
checkQuery := fmt.Sprintf(
"SELECT is_encrypted FROM %s WHERE is_encrypted=TRUE LIMIT 1;", table,
"SELECT is_encrypted FROM convoy.%s WHERE is_encrypted=TRUE LIMIT 1;", table,
)
var isEncrypted bool
err := tx.Get(&isEncrypted, checkQuery)
Expand All @@ -83,7 +83,7 @@ func lockTable(tx *sqlx.Tx, table string) error {
return fmt.Errorf("failed to set statement timeout: %w", err)
}

lockQuery := fmt.Sprintf("LOCK TABLE %s IN ACCESS EXCLUSIVE MODE;", table)
lockQuery := fmt.Sprintf("LOCK TABLE convoy.%s IN ACCESS EXCLUSIVE MODE;", table)
_, err = tx.Exec(lockQuery)
if err != nil {
return fmt.Errorf("failed to lock table %s: %w", table, err)
Expand All @@ -95,7 +95,7 @@ func lockTable(tx *sqlx.Tx, table string) error {
func encryptColumn(tx *sqlx.Tx, table, column, cipherColumn, encryptionKey string) error {
// Encrypt the column data and store it in the _cipher column
encryptQuery := fmt.Sprintf(
"UPDATE %s SET %s = pgp_sym_encrypt(%s::text, $1), %s = %s WHERE %s IS NOT NULL;",
"UPDATE convoy.%s SET %s = pgp_sym_encrypt(%s::text, $1), %s = %s WHERE %s IS NOT NULL;",
table, cipherColumn, column, column, getColumnZero(tx, table, column), column,
)
_, err := tx.Exec(encryptQuery, encryptionKey)
Expand All @@ -107,7 +107,7 @@ func encryptColumn(tx *sqlx.Tx, table, column, cipherColumn, encryptionKey strin
}

func getColumnZero(tx *sqlx.Tx, table, column string) string {
query := `SELECT is_nullable, data_type FROM information_schema.columns WHERE table_name = $1 AND column_name = $2;`
query := `SELECT is_nullable, data_type FROM convoy.information_schema.columns WHERE table_name = $1 AND column_name = $2;`
var isNullable, columnType string
err := tx.QueryRow(query, table, column).Scan(&isNullable, &columnType)
if err != nil {
Expand Down Expand Up @@ -137,7 +137,7 @@ func getColumnZero(tx *sqlx.Tx, table, column string) string {
// markTableEncrypted sets the `is_encrypted` column to true.
func markTableEncrypted(tx *sqlx.Tx, table string) error {
markQuery := fmt.Sprintf(
"UPDATE %s SET is_encrypted = TRUE;", table,
"UPDATE convoy.%s SET is_encrypted = TRUE;", table,
)
_, err := tx.Exec(markQuery)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/keys/encrypter_revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func RevertEncryption(db database.Database, km KeyManager, encryptionKey string)
func decryptAndRestoreColumn(tx *sqlx.Tx, table, column, cipherColumn, encryptionKey string) error {
// Decrypt the cipher column and update the plain column, casting as needed
revertQuery := fmt.Sprintf(
"UPDATE %s SET %s = pgp_sym_decrypt(%s::bytea, $1)::%s WHERE %s IS NOT NULL;",
"UPDATE convoy.%s SET %s = pgp_sym_decrypt(%s::bytea, $1)::%s WHERE %s IS NOT NULL;",
table, column, cipherColumn, getColumnType(tx, table, column), cipherColumn,
)
_, err := tx.Exec(revertQuery, encryptionKey)
Expand All @@ -69,7 +69,7 @@ func decryptAndRestoreColumn(tx *sqlx.Tx, table, column, cipherColumn, encryptio

// Clear the cipher column
clearCipherQuery := fmt.Sprintf(
"UPDATE %s SET %s = NULL WHERE %s IS NOT NULL;",
"UPDATE convoy.%s SET %s = NULL WHERE %s IS NOT NULL;",
table, cipherColumn, cipherColumn,
)
_, err = tx.Exec(clearCipherQuery)
Expand All @@ -81,7 +81,7 @@ func decryptAndRestoreColumn(tx *sqlx.Tx, table, column, cipherColumn, encryptio
}

func getColumnType(tx *sqlx.Tx, table, column string) string {
query := `SELECT data_type FROM information_schema.columns WHERE table_name = $1 AND column_name = $2;`
query := `SELECT data_type FROM convoy.information_schema.columns WHERE table_name = $1 AND column_name = $2;`
var columnType string
err := tx.Get(&columnType, query, table, column)
if err != nil {
Expand All @@ -94,7 +94,7 @@ func getColumnType(tx *sqlx.Tx, table, column string) string {
// markTableDecrypted sets the `is_encrypted` column to false.
func markTableDecrypted(tx *sqlx.Tx, table string) error {
markQuery := fmt.Sprintf(
"UPDATE %s SET is_encrypted = FALSE;", table,
"UPDATE convoy.%s SET is_encrypted = FALSE;", table,
)
_, err := tx.Exec(markQuery)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/keys/encrypter_rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func RotateEncryptionKey(db database.Database, km KeyManager, oldKey, newKey str
func reEncryptColumn(tx *sqlx.Tx, table, cipherColumn, oldKey, newKey string) error {
// Re-encrypt the cipher column with the new key
reEncryptQuery := fmt.Sprintf(
"UPDATE %s SET %s = pgp_sym_encrypt(pgp_sym_decrypt(%s::bytea, $1), $2) WHERE %s IS NOT NULL;",
"UPDATE convoy.%s SET %s = pgp_sym_encrypt(pgp_sym_decrypt(%s::bytea, $1), $2) WHERE %s IS NOT NULL;",
table, cipherColumn, cipherColumn, cipherColumn,
)
_, err := tx.Exec(reEncryptQuery, oldKey, newKey)
Expand Down

0 comments on commit 06df51f

Please sign in to comment.