<?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 Maxime Le Segretain on Medium]]></title>
        <description><![CDATA[Stories by Maxime Le Segretain on Medium]]></description>
        <link>https://medium.com/@maxime.ls?source=rss-87f0d6ea486a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*RG1JkXwgGIEfv9qI</url>
            <title>Stories by Maxime Le Segretain on Medium</title>
            <link>https://medium.com/@maxime.ls?source=rss-87f0d6ea486a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 19:30:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maxime.ls/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[Encrypt files on S3 with ActiveStorage]]></title>
            <link>https://medium.com/go-electra/encrypt-files-on-s3-with-activestorage-9df7fa9ff603?source=rss-87f0d6ea486a------2</link>
            <guid isPermaLink="false">https://medium.com/p/9df7fa9ff603</guid>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[s3]]></category>
            <category><![CDATA[encryption]]></category>
            <dc:creator><![CDATA[Maxime Le Segretain]]></dc:creator>
            <pubDate>Mon, 27 Nov 2023 09:58:06 GMT</pubDate>
            <atom:updated>2023-11-27T11:02:20.174Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*z913P8j8Xt69jUpXM5G01g.jpeg" /></figure><p>When it comes to storing files using ActiveStorage on a S3 Bucket, nothing is easier.</p><p>You just have to:</p><ul><li>create your S3 bucket</li><li>use the “aws-sdk-s3”</li><li>specify inside your config/storage.yml the S3 Service with your bucket’s name</li><li>And then ActiveStorage, with the aws-sdk-s3, is taking care of the rest.</li></ul><p>But when it comes to storing sensitive informations that your users upload on your app, you’d want to take extra care of how you store it. Even though S3 is using by default a layer of encryption to encrypt the keys of the files, the files are themselves stored clearly inside the bucket.</p><p>What if some day, for some reasons, your S3 bucket has leaked, and all the sensitive files of your users fall under the hands of malicious persons ? That’s something you’d surely want to avoid.</p><p>That’s why we decided to encrypt all the sensitive files on the server’s side at the documents upload, and decrypt a file through a dedicated controller when we want to access it.</p><h4>Step 1 — Create a EncryptedS3Service</h4><p>To be able to store a file on S3 through ActiveStorage, you’ll need the “<em>aws-sdk-s3</em>”, so add this line to your Gemfile</p><pre>gem &quot;aws-sdk-s3&quot;, &quot;~&gt; 1.134.0&quot;, require: false</pre><p>Then, you’ll have to write a custom S3Service using the Aws::S3::EncryptionV2::Client, in a lib/active_storage/service folder.</p><p>This service will take all the necessary arguments to encrypt/decrypt a file before uploading/downloading it.</p><p>In our case, we decided to have an <em>encryption_key</em> stored in our AWS account, but you can perfectly use KMS by passing a <em>kms_key_id</em>.</p><pre>## lib/active_storage/service/encrypted_s3_service.rb<br><br>require &quot;active_storage/service/s3_service&quot;<br><br>module ActiveStorage<br>  class Service<br>    class EncryptedS3Service &lt; Service::S3Service<br><br>      attr_reader :encryption_client<br><br>      KEYS_OPTIONS = {<br>        key_wrap_schema: :aes_gcm,<br>        content_encryption_schema: :aes_gcm_no_padding,<br>        security_profile: :v2, # rubocop:disable Naming/VariableNumber<br>      }.freeze<br><br>      def initialize(bucket:, upload: {}, **options)<br>        super_options = options.except(:kms_key_id, :encryption_key)<br>        super(bucket:, upload:, **super_options)<br>        @encryption_client = Aws::S3::EncryptionV2::Client.new(options.merge(KEYS_OPTIONS))<br>      end<br><br>      ## more to come here...<br>    end<br>  end<br>end</pre><p>Let’s dive in what’s going on here !<br>The <em>KEY_OPTIONS</em> constant will add all the encryption options that the <em>Aws::S3::EncryptionV2::Client</em> needs. Here’s are the possible values to use:</p><ul><li><strong>key_wrap_schema</strong> -&gt; the key used to wrap the schema, which should match the type of key used. In our case, :aes_gcm !</li><li><strong>content_encryption_schema</strong> -&gt; :aes_gcm_no_padding is the only option available. Given what is specified in the documentation, more options will be available in future releases.</li><li><strong>security_profile</strong> -&gt; if you’ve never encrypted files with Aws::S3::Encryption::Client, you’d better use :v2 argument, otherwise, you could use :v2_and_legacy</li></ul><p>We then initialize a <em>Aws::S3::EncryptionV2::Client</em> with the S3Service native options + the <em>KEY_OPTIONS</em>.</p><p>Finally, we’ll override uploads and downloads method to encrypt/decrypt a file.</p><pre>## lib/active_storage/service/encrypted_s3_service.rb<br>require &quot;active_storage/service/s3_service&quot;<br><br>module ActiveStorage<br>  class Service<br>    class EncryptedS3Service &lt; Service::S3Service<br><br>      ### …initialization code…<br><br>     def upload(key, io, checksum: nil, **)<br>        instrument(:upload, key:, checksum:) do<br>          encryption_client.put_object(<br>            upload_options.merge(<br>              body: io,<br>              bucket: bucket.name,<br>              key:<br>            )<br>          )<br>        rescue Aws::S3::Errors::BadDigest<br>          raise ActiveStorage::IntegrityError<br>        end<br>      end<br><br>      def download(key)<br>        raise NotImplementedError, &quot;#get_object with :range not supported yet&quot; if block_given?<br><br>        instrument(:download, key:) do<br>          encryption_client.get_object(<br>            bucket: bucket.name,<br>            key:<br>          ).body.string.force_encoding(Encoding::BINARY)<br>        end<br>      end<br><br>      def download_chunk(key, range)<br>        raise NotImplementedError, &quot;#get_object with :range not supported yet&quot;<br>      end<br><br>    end<br>  end<br>end</pre><h4>Step2 — Call the EncryptedS3Service inside your config/storage.yml</h4><p>You now want to define all the options inside you config/storage.yml.</p><p>To do so, you’ll need:</p><ul><li>a bucket name</li><li>the region where your bucket is</li><li>an encryption_key (or a kms_key_id)</li></ul><p>Your storage.yml should look like this:</p><pre>encrypted_active_storage:<br>  service: EncryptedS3<br>  bucket: your-bucket-name<br>  region: eu-west-3<br>  encryption_key: ENV[&quot;your-securely-stored-encryption-key&quot;]</pre><p>Then when it comes to call this <em>EncryptedS3Service</em>, you’ll specify inside your model what service you need to use.</p><pre>class MyModel &lt; ::ApplicationRecord<br>   has_many_attached :id_documents, service: :encrypted_active_storage<br>end</pre><p>And voila! You can now upload an encrypted file on S3.</p><h4>Step3 — Download the file</h4><p>To download an encrypted file, there’s a few gotchas that you have to keep in mind.</p><p>You’d naturally assume that inside your view, you’ll use the method <em>rails_blob_path / rails_blob_url</em></p><pre>&lt;%= <br> link_to(&quot;My Encrypted File&quot;,<br> rails_blob_path(my_model.my_file, disposition: &quot;attachment&quot;),<br> target= &quot;_blank&quot;<br> )<br>%&gt;</pre><p>This method will “successfuly” download a file, but the file will be <strong>corrupted</strong>, so it doesn’t properly work since this helper method automatically pass a block to stream the download and yield the blob in chunks.</p><p>Remember, the file has been <strong>encrypted</strong> as a whole, so the decryption should be as well on the whole file.</p><p>The reason for that is that downloading large files <a href="https://apidock.com/rails/v5.2.3/ActiveStorage/Blob/download">will use a lot of RAM</a>, and since the encryption/decryption is expensive, you should wisely decide what files should be encrypted or not with this kind of pattern.<br>Anyway, we decided to bypass this behavior by creating our own routes to download an encrypted file.</p><p>First, you’ll need to define your route</p><pre># config/routes.rb<br>Rails.application.routes.draw do<br>   # some routes…<br>   resources :decrypted_blobs, only: :show<br>end</pre><p>Second, you’ll have to create its associated controller</p><pre>class DecryptedBlobsController &lt; BaseController<br><br>    def show<br>      attachment = ::ActiveStorage::Attachment.find(params[:id])<br><br>      decrypted_io = attachment.download<br><br>      send_data decrypted_io,<br>        filename: attachment.filename.to_s,<br>        type: attachment.content_type,<br>        disposition: &quot;attachment&quot;<br>    rescue ActiveStorage::FileNotFoundError<br>      redirect_back(fallback_location: backoffice_home_path, flash: { alert: &quot;Something unexpected happened&quot; })<br>    end<br><br>end</pre><p>And then you can call your new <em>decrypted_blob_url</em> like this</p><pre>&lt;%= link_to(attachment.filename, decrypted_blob_url(https://medium.com/attachment,%20attachment.id)) %&gt;</pre><p>So that’s it, you can now encrypt and decrypt files stored on AWS S3 by using ActiveStorage.</p><p>One caveat to keep in mind: this method uses server-side uploads, which can be costly in backend resources.</p><p>Consider mixing it with <a href="https://aws.amazon.com/blogs/compute/uploading-to-amazon-s3-directly-from-a-web-or-mobile-application/">client-side uploads</a> or even client-side encryption if you expect a high volume of uploads.</p><p>We also thought of handling the encryptions by chunks instead of encrypting the whole file, and then decrypting those chunks, to not overload the server by download the whole file into memory.</p><p>But this article was written because it lacked some contexts and documentations on the internet about this matter, and using ActiveStorage is simple, so encryption should be simple as well.</p><p>Finally, this article has been widely inspired by this <a href="https://ankane.org/activestorage-s3-encryption">article</a> wrote by Andrew Kane. Unfortunately, the <em>Aws::S3::Encryption::Client</em> has been deprecated by AWS, so we had to freshen up its tutorial a bit !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9df7fa9ff603" width="1" height="1" alt=""><hr><p><a href="https://medium.com/go-electra/encrypt-files-on-s3-with-activestorage-9df7fa9ff603">Encrypt files on S3 with ActiveStorage</a> was originally published in <a href="https://medium.com/go-electra">Electra</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>