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

🔐 Marketplace: Encrypt Order#delivery_address! #1169

Merged
merged 2 commits into from
Mar 3, 2023
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
2 changes: 2 additions & 0 deletions app/furniture/marketplace/cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Cart < Record
has_many :cart_products, dependent: :destroy, inverse_of: :cart
has_many :products, through: :cart_products, inverse_of: :carts

has_encrypted :delivery_address, migrating: true

enum status: {
pre_checkout: "pre_checkout",
paid: "paid"
Expand Down
2 changes: 1 addition & 1 deletion app/furniture/marketplace/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Order < Record
has_many :ordered_products, inverse_of: :order, foreign_key: :cart_id
has_many :products, through: :ordered_products, inverse_of: :orders

attribute :delivery_address, :string
has_encrypted :delivery_address, migrating: true

enum status: {
pre_checkout: "pre_checkout",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MarketplaceEncryptOrderDeliveryAddress < ActiveRecord::Migration[7.0]
def change
add_column :marketplace_orders, :delivery_address_ciphertext, :text
Copy link
Contributor

@KellyAH KellyAH Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lockbox gem says it's possible to encrypt the existing column here.... but maybe it's statement is deceptive. It's steps looks like you'd still need to create a new column for encrypted data. migrate the data, and drop the column of unencrypted data. 🙄

Copy link
Member Author

@zspencer zspencer Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the migrating syntax is neat! I didn't realize it existed; but yea, that would save us the rename_column step and having to write the find_each!

It's also likely far safer in high-throughput cases; because you would still be able to access the delivery_address while it's being migrated; where as with this there is a moment in time where delivery_address is nil until the data migration completes.

I'll definitely do it next time!

end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_03_02_024315) do
ActiveRecord::Schema[7.0].define(version: 2023_03_02_202459) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -136,6 +136,7 @@
t.string "stripe_session_id"
t.string "delivery_address"
t.string "contact_email"
t.text "delivery_address_ciphertext"
t.index ["marketplace_id"], name: "index_marketplace_orders_on_marketplace_id"
t.index ["shopper_id"], name: "index_marketplace_orders_on_shopper_id"
end
Expand Down
1 change: 1 addition & 0 deletions lib/tasks/release.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace :release do
desc "Ensures any post-release / pre-deploy behavior has occurred"
task after_build: [:environment, "db:prepare"] do
Lockbox.migrate(Marketplace::Order)
SystemTestSpace.prepare
end
end