<?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: CareDox Engineering</title>
    <description>The latest articles on Forem by CareDox Engineering (@caredox).</description>
    <link>https://forem.com/caredox</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%2Forganization%2Fprofile_image%2F916%2F11516204-3590-4132-859e-c05a1607f5ef.jpg</url>
      <title>Forem: CareDox Engineering</title>
      <link>https://forem.com/caredox</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/caredox"/>
    <language>en</language>
    <item>
      <title>Secrets Management in Elixir Using AWS</title>
      <dc:creator>erikosmond</dc:creator>
      <pubDate>Thu, 11 Jul 2019 15:45:12 +0000</pubDate>
      <link>https://forem.com/caredox/secrets-management-in-elixir-using-aws-4d9b</link>
      <guid>https://forem.com/caredox/secrets-management-in-elixir-using-aws-4d9b</guid>
      <description>&lt;p&gt;When we at Caredox decided to start writing services in Elixir, building a solution for safely storing and accessing secrets was one of the first action items we wanted to address.  Our infrastructure has always been hosted on AWS so it made sense for us to continue integrating with their services to store our secrets. After considering the handful of encryption options that AWS provides, we decided to move forward with Parameter Store: a component of AWS Systems Manager (SSM).&lt;/p&gt;

&lt;p&gt;Caredox uses Distillery to build releases for our Elixir application. Distillery has a concept called &lt;a href="https://hexdocs.pm/distillery/extensibility/config_providers.html"&gt;configuration providers&lt;/a&gt; to populate the runtime environment by parsing a designated file. Depending on how secrets are stored (JSON, TOML, YAML, etc.), each storage mechanism will require its own config provider. We didn't find a config provider that is designed to integrate with SSM, so we &lt;a href="https://github.com/caredox/aws_ssm_provider"&gt;wrote our own&lt;/a&gt; which is available as a &lt;a href="https://hex.pm/packages/aws_ssm_provider"&gt;hex package&lt;/a&gt;. Our config provider affords us a relatively simple process for accessing secrets. Before launching our app, we request our secrets from AWS SSM and write them to a unidimensional JSON file. Our config provider reads this flat JSON file and generates a multidimensional data object that can be used by our application's runtime to access values, sensitive or otherwise.  When updating or adding values in SSM, running docker containers have to be restarted to reflect the changes.&lt;/p&gt;

&lt;h1&gt;
  
  
  Specific Example
&lt;/h1&gt;

&lt;p&gt;When adding functionality that requires a new config value, we'll first add that value to SSM. We follow the guideline that AWS suggests to define keys as slash (&lt;code&gt;/&lt;/code&gt;) delimited strings, ie. &lt;code&gt;/production/caredox/Redix/password&lt;/code&gt;. The first segment is the environment (production), the second segment is your project (caredox), the third segment is the name of the OTP application (Redix), and all remaining segments are the key you want stored in that OTP application (password).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;aws ssm put-parameter &lt;span class="nt"&gt;--name&lt;/span&gt; &lt;span class="s1"&gt;'/production/caredox/Redix/password'&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;
 &lt;span class="nt"&gt;--type&lt;/span&gt; ‘SecureString’ &lt;span class="nt"&gt;--value&lt;/span&gt; &lt;span class="s1"&gt;'YourSecretPassword'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We then deploy our app in a docker container.  Our Dockerfile specifies an entrypoint to a bash bootscript which requests our secrets from SSM, writes them to a file, then starts our Elixir app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;aws &lt;span class="nt"&gt;--region&lt;/span&gt; us-east-1 ssm get-parameters-by-path &lt;span class="nt"&gt;--path&lt;/span&gt; &lt;span class="s2"&gt;"/production/YOUR_PROJECT/"&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--recursive&lt;/span&gt; &lt;span class="nt"&gt;--with-decryption&lt;/span&gt; &lt;span class="nt"&gt;--query&lt;/span&gt; &lt;span class="s2"&gt;"Parameters[]"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /etc/app_secrets.json
/path/to/your_project foreground
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that our container has a file with our secrets in it, the config provider is ready to read that file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="c1"&gt;# rel/confix.exs&lt;/span&gt;

&lt;span class="n"&gt;environment&lt;/span&gt; &lt;span class="ss"&gt;:prod&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;config_providers:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="no"&gt;AwsSsmProvider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"/etc/app_secrets.json"&lt;/span&gt;&lt;span class="p"&gt;]}])&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Shared Example
&lt;/h1&gt;

&lt;p&gt;Sometimes we have multiple apps that depend on a single source of truth for a configuration.  This might be required on a per environment basis (staging vs. production), or we might want a config to have a default value across the whole system, regardless of environment.  To fetch the global secrets, the bootscript will require an additional call to SSM. We happen to write them to &lt;code&gt;/etc/global_secrets.json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For values that are consistent across a specific environment, we use the magic string &lt;code&gt;host_app&lt;/code&gt; to indicate to the SSM config provider that each app is in charge of managing how the config is set. For instance, a global SSM config by environment would look like this &lt;code&gt;/staging/global/host_app/company_internal_password&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The config provider must be told what atom will substitute the string &lt;code&gt;host_app&lt;/code&gt; in the SSM key, in this case the atom to be substituted in is &lt;code&gt;:myApp&lt;/code&gt;. We do that by including &lt;code&gt;:myApp&lt;/code&gt; as the second element in the list sent to the init function of the config provider.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="c1"&gt;# rel/confix.exs&lt;/span&gt;

&lt;span class="n"&gt;environment&lt;/span&gt; &lt;span class="ss"&gt;:prod&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;config_providers:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="no"&gt;AwsSsmProvider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"/etc/global_secrets.json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:myApp&lt;/span&gt;&lt;span class="p"&gt;]}])&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Our secrets now live securely in AWS SSM and are written to an ephemeral file in each docker container after it starts running. The container itself lives in a secure VPC so we have no loose ends.&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>aws</category>
      <category>security</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
