<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Anag</title>
    <description>The latest articles on Forem by Anag (@mrnagoo).</description>
    <link>https://forem.com/mrnagoo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F922033%2F3f78800d-6132-4839-b14e-3c7cd84aa1f3.jpeg</url>
      <title>Forem: Anag</title>
      <link>https://forem.com/mrnagoo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mrnagoo"/>
    <language>en</language>
    <item>
      <title>Shrink your Rails 7 application.js files</title>
      <dc:creator>Anag</dc:creator>
      <pubDate>Mon, 06 Feb 2023 19:30:15 +0000</pubDate>
      <link>https://forem.com/mrnagoo/shrink-your-rails-7-applicationjs-files-lop</link>
      <guid>https://forem.com/mrnagoo/shrink-your-rails-7-applicationjs-files-lop</guid>
      <description>&lt;p&gt;I discovered that my rails app was bundling a few too many stimulus controllers. Whoa! Slow down there buddy, save some of those controllers for individual pages! Here's my browser trying to download the deferred file...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FkaZdqgKC2Dg0XoRzgD%2Fgiphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FkaZdqgKC2Dg0XoRzgD%2Fgiphy.gif" width="1024" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Maybe you're like me and you thought... "you can choose which stimulus controllers to import?"&lt;/p&gt;

&lt;p&gt;Yes. Obvs.&lt;/p&gt;

&lt;p&gt;I followed the upgrade tutorials and my application.js using &lt;code&gt;esbuild&lt;/code&gt; and &lt;code&gt;js-bundling_rails&lt;/code&gt; loaded my new stimulus controllers like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="s1"&gt;'@hotwired/turbo-rails'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="s1"&gt;'./controllers'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I added a few more controllers and then BAM! I had a massive 4.5MB application.js file!&lt;/p&gt;

&lt;p&gt;Turns out I had a couple pages that used a third party UI component and for reasons I couldn't stop using those components. So I loaded them into my controllers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia2.giphy.com%2Fmedia%2FTd2ohS6FcoSGjyyyf3%2Fgiphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia2.giphy.com%2Fmedia%2FTd2ohS6FcoSGjyyyf3%2Fgiphy.gif" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I didn't want to stop using the controllers in the same way, but I wanted to use them when I needed them. So I load my controllers dynamically now. But I still register them to the &lt;code&gt;{ Application }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is my current setup:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7fjzblrwbbwl4i0gt6n5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7fjzblrwbbwl4i0gt6n5.png" alt="Image description" width="184" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Anything in my &lt;code&gt;controllers&lt;/code&gt; directory goes to application.js. I use active admin...(I know, I know, zip it.) &lt;/p&gt;

&lt;p&gt;So I load &lt;code&gt;jquery&lt;/code&gt; to the window when I'm on active admin pages.&lt;/p&gt;

&lt;p&gt;I then bring in my other controllers individually and use `&amp;lt;%= javascript_include_tag 'preview(or upload)' %&amp;gt; where needed.&lt;/p&gt;

&lt;p&gt;Here's what the preview directory looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fba757va27fr6iawzrr5m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fba757va27fr6iawzrr5m.png" alt="Image description" width="204" height="58"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is what the index.js file looks like.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`javascript&lt;br&gt;
import { application } from "../controllers/application"&lt;/p&gt;

&lt;p&gt;import PreviewController from "./preview_controller"&lt;br&gt;
application.register("preview", PreviewController)&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So now my application.js file is 13x smaller. Maybe there is a better way to do it. I'm not entirely sure. But here I am putting out a way where it works for me. Lighthouse Performance score went from 33 to 93. I really don't need that much javascript globally so I'll be trying to deliver almost 0 javascript to the page over time. I'll have ChatGPT research the best admin, or roll my own for me 😂.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Active Record Encryption and Rollover with ENV's instead of credentials</title>
      <dc:creator>Anag</dc:creator>
      <pubDate>Wed, 11 Jan 2023 21:12:32 +0000</pubDate>
      <link>https://forem.com/mrnagoo/active-record-encryption-and-rollover-with-envs-instead-of-credentials-ak5</link>
      <guid>https://forem.com/mrnagoo/active-record-encryption-and-rollover-with-envs-instead-of-credentials-ak5</guid>
      <description>&lt;p&gt;If you want to use environment variables (ENVs) instead of the credentials files because you're in a cloud service like AWS or Heroku you can still do it and rollover your keys. &lt;/p&gt;

&lt;p&gt;Just add your ENVs to your .env and/or .env.development , etc. like so...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PRIMARY_KEY=EGY8WhulUOXixybod7ZWwMIL68R9o5kC
PRIMARY_KEY_ROLLOVER= &amp;lt;- empty until you need to rollover
DETERMINISTIC_KEY="aPA5XyALhf75NNnMzaspW7akTfZp0lPY"
KEY_DERIVATION_SALT=xEY0dt6TZcAMg52K7O84wYzkjvbA62Hz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don't need the deterministic key if you're not going to use it. I just have it for incase I want to. &lt;/p&gt;

&lt;p&gt;You can create your keys in the console:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SecureRandom.alphanumeric(32)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now that you have the ENVs you can enter them into your Parameter Store however your cloud provider allows.&lt;/p&gt;

&lt;p&gt;Now lets boot up our app with the correct configuration.&lt;/p&gt;

&lt;p&gt;If you added ENVs to the .env file and you're not going to care about per environment basis then add these configs to your &lt;code&gt;application.rb&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  config.active_record.encryption.primary_key = ENV["PRIMARY_KEY"]
  config.active_record.encryption.deterministic_key = ENV["DETERMINISTIC_KEY"]
  config.active_record.encryption.key_derivation_salt = ENV["KEY_DERIVATION_SALT"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However you probably should do everything by environment and add this to your &lt;code&gt;(environment).rb&lt;/code&gt; unless your deploy process imports different ENVs based on the environment it's deploying.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# development.rb&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active_record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encryption&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;primary_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"PRIMARY_KEY"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active_record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encryption&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deterministic_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"DETERMINISTIC_KEY"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active_record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encryption&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;key_derivation_salt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"KEY_DERIVATION_SALT"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what about rollover?&lt;/p&gt;

&lt;p&gt;Lets make a change to your primary key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# development.rb&lt;/span&gt;

  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active_record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encryption&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;primary_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"PRIMARY_KEY"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"PRIMARY_KEY_ROLLOVER"&lt;/span&gt;&lt;span class="p"&gt;]].&lt;/span&gt;&lt;span class="nf"&gt;compact_blank&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active_record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encryption&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deterministic_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"DETERMINISTIC_KEY"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active_record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encryption&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;key_derivation_salt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"KEY_DERIVATION_SALT"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don't want a rollover you can just add &lt;code&gt;PRIMARY_KEY_V2, etc.&lt;/code&gt; and keep the array growing.&lt;/p&gt;

&lt;p&gt;As rails initializes the environment, it will have a single value or an array of values, the last index will be the newest key.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.compact_blank&lt;/code&gt; will clear the array of &lt;code&gt;""&lt;/code&gt; values in the array for when your rollover key is empty. You only need a rollover key when you're going to rollover.&lt;/p&gt;

&lt;p&gt;Save a new 32 char random value to your rollover ENV&lt;/p&gt;

&lt;p&gt;&lt;code&gt;PRIMARY_KEY_ROLLOVER=LsnKbJ3Gyg9Ee2Klfq1gKa6UUR54aCh6&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Every time my rails app deploys it will load ENVs so now the rollover key isn't blank.&lt;/p&gt;

&lt;p&gt;Now you need to write a script to encrypt your data to the rollover key. &lt;/p&gt;

&lt;p&gt;You can call this script from an admin or write a data migration to run it. &lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="no"&gt;Documents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="c1"&gt;#encrypt in batches of 1000&lt;/span&gt;
    &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encrypt&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling &lt;code&gt;.encrypt&lt;/code&gt; on a document will decrypt with any key it finds that works and re-encrypt with a new key (highest array index).&lt;/p&gt;

&lt;p&gt;For the rollover approach I would copy the primary key temporarily, and then move the rollover key to the primary key env.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PRIMARY_KEY=(ROLLOVER_KEY value)
PRIMARY_KEY_ROLLOVER= (empty)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Re deploy and all code should now be decrypting with the new rolled over key. If it didn't work add that encrypted_key that you temporarily saved back to the ENVs and fix your code, you dummy.&lt;/p&gt;

&lt;p&gt;Remember you must add the new keys to the end of the array config.&lt;/p&gt;

&lt;p&gt;Now you're set to rollover your keys at whatever interval you decide. Without credential.yml.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>programming</category>
      <category>security</category>
    </item>
  </channel>
</rss>
