<?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>DEV Community: nownabe</title>
    <description>The latest articles on DEV Community by nownabe (@nownabe).</description>
    <link>https://dev.to/nownabe</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%2F44474%2Fc5959f5e-a5dd-41ae-9e22-d9e3d25e38b9.jpg</url>
      <title>DEV Community: nownabe</title>
      <link>https://dev.to/nownabe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nownabe"/>
    <language>en</language>
    <item>
      <title>Sekret: Encryption tool for Kubernetes Secrets</title>
      <dc:creator>nownabe</dc:creator>
      <pubDate>Mon, 27 Aug 2018 10:36:41 +0000</pubDate>
      <link>https://dev.to/nownabe/sekret-encryption-tool-for-kubernetes-secrets-4dip</link>
      <guid>https://dev.to/nownabe/sekret-encryption-tool-for-kubernetes-secrets-4dip</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/nownabe/sekret" rel="noopener noreferrer"&gt;Sekret&lt;/a&gt; is a CLI tool to encrypt and edit Kubernetes Secrets. Sekret makes management and deployment for Secret secure and simple.&lt;/p&gt;

&lt;h1&gt;
  
  
  Motivations
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;want version controlled Secret like other resources (e.g., deployments)&lt;/li&gt;
&lt;li&gt;want to commit encrypted Secret YAMLs&lt;/li&gt;
&lt;li&gt;want to edit encrypted Secret YAMLs easily&lt;/li&gt;
&lt;li&gt;want to apply decrypted Secret YAMLs easily&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Functionalities
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Encrypt plain YAMLs&lt;/li&gt;
&lt;li&gt;Decrypt encrypted YAMLs&lt;/li&gt;
&lt;li&gt;Create new encrypted Secret YAMLs&lt;/li&gt;
&lt;li&gt;Edit encrypted Secret YAMLs as plain text&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Demo
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://asciinema.org/a/MyvxqcN0oMbmGc8xAaJh4U2Fz" rel="noopener noreferrer"&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%2F64umgbaetyo2cn9zibtt.png" alt="asciicast" width="800" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Installation
&lt;/h1&gt;

&lt;p&gt;You can get Sekret with &lt;code&gt;go get&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go get github.com/nownabe/sekret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, you can download binaries from &lt;a href="https://github.com/nownabe/sekret/releases" rel="noopener noreferrer"&gt;GitHub Releases&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-sSL&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; /path/to/sekret https://github.com/nownabe/sekret/releases/download/v1.1.0/sekret_linux_amd64
&lt;span class="nb"&gt;chmod&lt;/span&gt; +x /path/to/sekret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Usage
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;sekret&lt;/code&gt; command is used following subcommands &lt;code&gt;enc&lt;/code&gt; / &lt;code&gt;dec&lt;/code&gt; / &lt;code&gt;new&lt;/code&gt; / &lt;code&gt;edit&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sekret subcommand &lt;span class="o"&gt;[&lt;/span&gt;options] filename
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The environment variable &lt;code&gt;ENCRYPTION_KEY&lt;/code&gt; is used as the encryption key for all subcommands. Encryption keys must be 16 or 32 bytes. &lt;code&gt;EDITOR&lt;/code&gt; variable specifies the editor for &lt;code&gt;new&lt;/code&gt; and &lt;code&gt;edit&lt;/code&gt; subcommands. Command options can also specify them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encrypt
&lt;/h2&gt;

&lt;p&gt;Following commands encrypt &lt;code&gt;secret.yaml&lt;/code&gt; and then commit it on Git.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;ENCRYPTION_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; /dev/urandom | &lt;span class="nb"&gt;base64&lt;/span&gt; | &lt;span class="nb"&gt;fold&lt;/span&gt; &lt;span class="nt"&gt;-32&lt;/span&gt; | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-1&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;sekret enc secret.yaml &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; secret.yaml.enc
&lt;span class="nv"&gt;$ &lt;/span&gt;git add secret.yaml.enc
&lt;span class="nv"&gt;$ &lt;/span&gt;git commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Decrypt
&lt;/h2&gt;

&lt;p&gt;Easy to decrypt and apply Secrets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;sekret dec secret.yaml.enc | kubectl apply &lt;span class="nt"&gt;-f&lt;/span&gt; -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create New Encrypted Secrets
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;new&lt;/code&gt; subcommand creates a new encrypted Secret YAMLs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;EDITOR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;vim
&lt;span class="nv"&gt;$ &lt;/span&gt;sekret new secret.yaml.enc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;sekret new&lt;/code&gt; opens specified editor with the Secret template like following YAML.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;apiVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;v1&lt;/span&gt;
&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;Key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Value&lt;/span&gt;
&lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Secret&lt;/span&gt;
&lt;span class="na"&gt;metadata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;creationTimestamp&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;new-secret&lt;/span&gt;
&lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Opaque&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Values of &lt;code&gt;data&lt;/code&gt; must be encoded as base64 in Kubernetes Secrets, but sekret encodes and decodes automatically on opening and saving them. So you can write YAML as completely plain text. If you want to edit as base64, use &lt;code&gt;--decode-base64=false&lt;/code&gt; option.&lt;/p&gt;

&lt;p&gt;Sekret validates before saving YAML, so it doesn't save YAML when invalid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Edit Encrypted Secrets
&lt;/h2&gt;

&lt;p&gt;You can edit encrypted Secret YAML like plaintexts with &lt;code&gt;edit&lt;/code&gt; subcommand.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;sekret edit secret.yaml.enc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;sekret edit&lt;/code&gt; opens decrypted and base64 decoded YAML in the specified editor. When the editor is closed, it saves encrypted and base64 encoded YAML. Of course, it validates YAML before saving.&lt;/p&gt;

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

&lt;p&gt;Sekret makes lifecycle of Sekret very simple and secure. It is effortless to manage and deploy Secret YAML.&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>go</category>
    </item>
    <item>
      <title>Secure User in Docker</title>
      <dc:creator>nownabe</dc:creator>
      <pubDate>Wed, 20 Jun 2018 06:18:38 +0000</pubDate>
      <link>https://dev.to/nownabe/secure-user-in-docker-1b5m</link>
      <guid>https://dev.to/nownabe/secure-user-in-docker-1b5m</guid>
      <description>&lt;h1&gt;
  
  
  TL; DR
&lt;/h1&gt;

&lt;p&gt;Create users in Dockerfile as following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;RUN &lt;/span&gt;groupadd &lt;span class="nt"&gt;-g&lt;/span&gt; 61000 docker
&lt;span class="k"&gt;RUN &lt;/span&gt;useradd &lt;span class="nt"&gt;-g&lt;/span&gt; 61000 &lt;span class="nt"&gt;-l&lt;/span&gt; &lt;span class="nt"&gt;-M&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; /bin/false &lt;span class="nt"&gt;-u&lt;/span&gt; 61000 docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Secure User in Docker
&lt;/h1&gt;

&lt;p&gt;Running docker container as a root user is risky because the root user in containers has same uid 0 as the host root user. So you have to change the user in containers to non-root user as possible.&lt;/p&gt;

&lt;p&gt;Most simple and effective way is to create a user in &lt;code&gt;Dockerfile&lt;/code&gt;. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; debian&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;useradd docker
&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="s"&gt; docker&lt;/span&gt;

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["bash"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also change the user with &lt;code&gt;-u&lt;/code&gt; option of &lt;code&gt;docker run&lt;/code&gt;. With Kubernetes, you can use SecurityContext to modify the user.&lt;/p&gt;

&lt;p&gt;This &lt;code&gt;Dockerfile&lt;/code&gt; is fine, but &lt;code&gt;useradd&lt;/code&gt; has many options. Let me describe which options should be used in Dockerfile.&lt;/p&gt;

&lt;p&gt;Options often used with Dockerfile are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ useradd --help
Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]

Options:
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -r, --system                  create a system account
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's review each option.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-g&lt;/code&gt; specifies uid. Because the same gid as uid is easy to understand, you should use &lt;code&gt;-g&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-l&lt;/code&gt; looks good because lastlog and faillog have few meanings.&lt;/li&gt;
&lt;li&gt;If you have to operate with shell in containers&lt;sup id="fnref1"&gt;1&lt;/sup&gt;, use &lt;code&gt;-m&lt;/code&gt; to create home directory. If not, use &lt;code&gt;-M&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-r&lt;/code&gt; option makes user as a system account. uid is configured from &lt;code&gt;/etc/login.defs&lt;/code&gt; and home directory doesn't be created. You should not use this option because you will use &lt;code&gt;-u&lt;/code&gt; to specify uid and use &lt;code&gt;-m/-M&lt;/code&gt; to configure home directory.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-s /bin/false&lt;/code&gt; can forbid remote login. You can execute shell with &lt;code&gt;docker exec -u $uid sh&lt;/code&gt;, even if &lt;code&gt;/bin/false&lt;/code&gt; or &lt;code&gt;/bin/nologin&lt;/code&gt; is set. This option might protect from direct remote login, so you should use &lt;code&gt;-s /bin/false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-u&lt;/code&gt; specifies uid. When &lt;code&gt;-u&lt;/code&gt; was not used, uid is assigned automatically. To manage simply, you should use this option.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-U&lt;/code&gt; creates a group named same as the user but gid can differ from uid. &lt;code&gt;-g&lt;/code&gt; is preferred to &lt;code&gt;-U&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In conclusion, you should use following instructions to create users:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;RUN &lt;/span&gt;groupadd &lt;span class="nt"&gt;-g&lt;/span&gt; 61000 docker
&lt;span class="k"&gt;RUN &lt;/span&gt;useradd &lt;span class="nt"&gt;-g&lt;/span&gt; 61000 &lt;span class="nt"&gt;-l&lt;/span&gt; &lt;span class="nt"&gt;-M&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; /bin/false &lt;span class="nt"&gt;-u&lt;/span&gt; 61000 docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need home directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;RUN &lt;/span&gt;groupadd &lt;span class="nt"&gt;-g&lt;/span&gt; 61000 docker
&lt;span class="k"&gt;RUN &lt;/span&gt;useradd &lt;span class="nt"&gt;-g&lt;/span&gt; 61000 &lt;span class="nt"&gt;-l&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; /bin/false &lt;span class="nt"&gt;-u&lt;/span&gt; 61000 docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because major distributions reserve uid from 1000 to 60000, I proposed 61000 as uid. By the way, worker nodes of GKE reserve uid from 5000 to 60000. If you use 5000 as uid, they conflict.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;For example, you often use &lt;code&gt;bundle exec rails console&lt;/code&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>docker</category>
      <category>kubernetes</category>
    </item>
  </channel>
</rss>
