<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sharon Mayberg on Medium]]></title>
        <description><![CDATA[Stories by Sharon Mayberg on Medium]]></description>
        <link>https://medium.com/@sharonm_91047?source=rss-6d43c204162a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*TZ2uEl0tQEeBDEODB54Jlg.jpeg</url>
            <title>Stories by Sharon Mayberg on Medium</title>
            <link>https://medium.com/@sharonm_91047?source=rss-6d43c204162a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 02:14:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sharonm_91047/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How To Use Ruby Active Storage S3 With Client-Side Encryption]]></title>
            <link>https://medium.com/tailor-tech/how-to-use-ruby-active-storage-s3-with-client-side-encryption-b12024d4f14?source=rss-6d43c204162a------2</link>
            <guid isPermaLink="false">https://medium.com/p/b12024d4f14</guid>
            <category><![CDATA[active-storage]]></category>
            <category><![CDATA[encryption]]></category>
            <category><![CDATA[client-side-encryption]]></category>
            <category><![CDATA[s3]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Sharon Mayberg]]></dc:creator>
            <pubDate>Thu, 27 Apr 2023 12:38:06 GMT</pubDate>
            <atom:updated>2025-08-18T14:05:03.110Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>How To Use Rails Active Storage S3 With Client-Side Encryption</strong></h3><p>Data security is a crucial consideration for any web application that handles user content. <br>While cloud storage services offer a convenient way to store and manage files, simply storing the data in the cloud is not enough to guarantee its safety.<br>In order to fully secure user data, it is important to encrypt it before storing it in the cloud.<br>In this article, we’ll explore how to implement client side encryption with Ruby on Rails’ Active Storage, providing another layer of security for the user data stored in S3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PWm9oNzVJLWQufXHBCZ8lA.jpeg" /></figure><p><strong>What is client-side encryption?<br></strong>Client-side encryption is the act of encrypting your data locally before uploading it, to ensure its security as it passes to the cloud service. <br>This approach is often used to enhance the security and privacy of sensitive data, by ensuring it is not accessible to unauthorized parties even if the network or the remote server is compromised.<br>Encrypting our files before uploading them to the cloud is an extra step of security we want to take, so no one except for us can see our sensitive data.</p><p>To learn more about Amazon S3 client side encryption, see <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingClientSideEncryption.html">Protecting data using client-side encryption</a></p><p><strong>What is Active Storage?<br></strong>Active Storage makes it easy to upload files to a cloud storage service (Amazon S3, Google Cloud Storage, or Microsoft Azure Storage) and attach those files to Active Record objects. <br>With Active Storage, developers can easily and quickly attach files to database records (like images or documents), without having to manage the storage themselves.</p><p>For more information about how to use Active storage, see <a href="https://edgeguides.rubyonrails.org/active_storage_overview.html">Active Storage Documentation</a>.</p><p><strong>Creating an encryption key<br></strong>In client side encryption, an encryption key is a secret code used to encrypt and decrypt data on the client side. The encryption key is generated by the developer and is kept secret from anyone who is not authorized to access the data.<br>You can either create your own encryption key or use a key managed by AWS.<br>We chose to use the AWS managed kms service for the encryption key. We manage these resources through Terraform but you can create it by yourself following <a href="https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html">this guide</a>.</p><p><strong>Code implementation<br></strong>To get started, make sure you are using a recent version of <a href="https://rubygems.org/gems/aws-sdk-s3/versions/1.114.0">aws-sdk-s3 gem</a>, a library that provides a simple interface for developers to interact with Amazon Web Services.</p><p>First, we will create a new service that extends the built-in ActiveStorage S3 service and implements client-side encryption.<br>To do so — Create a new file <em>lib/active_storage/service/encrypted_s3_service.rb </em>With the following content:</p><pre># frozen_string_literal: true<br><br>require &quot;active_storage/service/s3_service&quot;<br><br>module ActiveStorage<br>  class Service::EncryptedS3Service &lt; Service::S3Service<br>    attr_reader :encryption_client<br><br>    def initialize(bucket:, upload: {}, **options)<br>      super_options = options.except(:kms_key_id, :encryption_key, :key_wrap_schema, :content_encryption_schema, :security_profile)<br>      super(bucket:, upload:, **super_options)<br>      @encryption_client = Aws::S3::EncryptionV2::Client.new(options)<br>    end<br><br>    def upload(key, io, checksum: nil, **)<br>      instrument :upload, key:, checksum: do<br>        @encryption_client.put_object(<br>          upload_options.merge(body: io, bucket: bucket.name, key:)<br>        )<br>      rescue Aws::S3::Errors::BadDigest<br>        raise ActiveStorage::IntegrityError<br>      end<br>    end<br><br>    def download(key)<br>      instrument :download, key: do<br>        @encryption_client.get_object(bucket: bucket.name, key:).body.string.force_encoding(Encoding::BINARY)<br>      end<br>    end<br>  end<br>end</pre><p>After creating the above file, add the following service to your <em>config/storage.yml</em> file which declares all the services your application uses:</p><pre>amazon_encrypted:<br>  service: EncryptedS3<br>  region: &lt;region&gt;<br>  bucket: &lt;bucket_name&gt;<br>  kms_key_id: alias/&lt;kms_key_id&gt;<br>  key_wrap_schema: :kms_context<br>  content_encryption_schema: :aes_gcm_no_padding<br>  security_profile: :v2</pre><p>Common pitfalls:</p><ul><li>Make sure you do not include the <em>service</em> suffix in <em>storage.yml </em>(in service property) since active storage concatenates it automatically.</li><li>You should keep the <em>alias/</em> prefix in the <em>kms_key_id</em>.</li></ul><p>Properties explained:</p><ul><li><em>kms_key_id</em> — After generating the encryption key in AWS you will get a kms_key_id.</li><li><em>key_wrap_schema</em> — The key wrapping schema. It must match the type of key configured. In our case: <em>:kms_context</em>.</li><li><em>content_encryption_schema</em> — the only supported value currently is <em>:aes_gcm_no_padding</em>.</li><li><em>security_profile</em> — Determines the support for reading objects written using older key wrap or content encryption schemas.<br>In this example we are using The <em>EncryptionV2::Client</em> (V2 Client) which provides improved security over the <em>Encryption::Client</em> (V1 Client) by using more modern and secure algorithms.</li></ul><p><strong>Using multiple buckets<br></strong>At this point, our project was already using Active Storage with S3, but without client side encryption. In order to not harm the fetching/retrieving of the already stored (not sensitive) data, we decided to create a separate bucket for sensitive data that will be used with encryption. This is how we defined our storage file:</p><pre>amazon:<br>  service: S3<br>  region: &lt;region&gt;<br>  bucket: &lt;bucket_name&gt;<br><br>amazon_encrypted:<br>  service: EncryptedS3<br>  region: &lt;region&gt;<br>  bucket: &lt;bucket_name&gt;<br>  kms_key_id: alias/&lt;kms_key_id&gt;<br>  key_wrap_schema: :kms_context<br>  content_encryption_schema: :aes_gcm_no_padding<br>  security_profile: :v2</pre><p>When using multiple storage services, we define a default storage service, which in our project was called <em>amazon</em>, declared as the default one in the application configuration file like this:</p><pre>config.active_storage.service = :amazon</pre><p>When attaching files to records that will be stored in the encrypted service, we would need to override the default service for a specific attachment, we can do so using the service option:</p><pre>class User &lt; ApplicationRecord<br>  has_one_attached :avatar, service: :amazon_encrypted<br>end</pre><p><strong>Testing with multiple buckets</strong></p><p>When testing our code, we might want to test models that are attached to the non-default bucket. In order to do so, we need to create a new configuration file for the test environment (also, it is strongly suggested that you have separate ones for development and production environments as well so you don’t mix development attachments with production ones).</p><pre>test:<br>  service: Disk<br>  root: &lt;%= Rails.root.join(&quot;tmp/storage&quot;) %&gt;<br><br>amazon:<br>  service: Disk<br>  root: &lt;%= Rails.root.join(&quot;tmp/storage&quot;) %&gt;<br><br>amazon_encrypted:<br>  service: Disk<br>  root: &lt;%= Rails.root.join(&quot;tmp/storage&quot;) %&gt;</pre><p>This tells Active Storage where to “upload” the test files to, so it should be a local temporary directory.</p><p>Thank you for taking the time to read this article. I hope you found it helpful.<br>Leave a comment below if you have any questions.</p><p>Special thanks to Andrew Kane, for his article <a href="https://ankane.org/activestorage-s3-encryption">Active Storage S3 Client-Side Encryption guide</a> which demonstrates client side encryption with client v1.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b12024d4f14" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tailor-tech/how-to-use-ruby-active-storage-s3-with-client-side-encryption-b12024d4f14">How To Use Ruby Active Storage S3 With Client-Side Encryption</a> was originally published in <a href="https://medium.com/tailor-tech">Tailor Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>