<?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 Eugene Romero on Medium]]></title>
        <description><![CDATA[Stories by Eugene Romero on Medium]]></description>
        <link>https://medium.com/@damn-engineer?source=rss-f326a315ccc8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jIqeOpsraKUnDfld7-YdSA.png</url>
            <title>Stories by Eugene Romero on Medium</title>
            <link>https://medium.com/@damn-engineer?source=rss-f326a315ccc8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 14:46:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@damn-engineer/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[Performing Elasticsearch API calls with Terraform, part 2]]></title>
            <link>https://damn-engineer.medium.com/performing-elasticsearch-api-calls-with-terraform-part-2-d11988e046fc?source=rss-f326a315ccc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/d11988e046fc</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[cloud]]></category>
            <dc:creator><![CDATA[Eugene Romero]]></dc:creator>
            <pubDate>Wed, 27 Apr 2022 12:11:19 GMT</pubDate>
            <atom:updated>2022-04-27T12:11:19.747Z</atom:updated>
            <content:encoded><![CDATA[<h4>Configuring Terraform to call the Elasticsearch API</h4><figure><img alt="Can&#39;t wait for robots to actually start doing all this grueling work for us" src="https://cdn-images-1.medium.com/max/1024/0*Epmu5qI3kBVDU6-0.jpg" /></figure><p>Photo by <a href="https://unsplash.com/@ekrull?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Eric Krull</a> on <a href="https://unsplash.com/s/photos/robot?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p><p>In <a href="https://damn.engineer/2022/04/19/elasticsearch-api-terraform-pt1">the previous post</a>, we created a script to automate calls to our Elasticsearch endpoint. Let’s now configure Terraform to use this script whenever we need to reach the API.</p><h4>Creating the Terraform null_resource</h4><p>As an example, say we wanted to add a <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/set-up-lifecycle-policy.html#:~:text=To%20create%20a%20lifecycle%20policy,policy%20to%20the%20Elasticsearch%20cluster.">lifecycle policy</a> to Elasticsearch. First off, let’s create a variable to hold the policy definition. We use <a href="https://linuxize.com/post/bash-heredoc/">Heredoc</a> syntax to make sure Terraform does not trip up on the multiline JSON definition:</p><pre>variable &quot;lifecyclePolicy&quot; {<br>  type    = map(string)<br>  default = {<br>    name   = &quot;log-lifecycle&quot;<br>    policy = &lt;&lt;-JSON<br>      {<br>        &quot;policy&quot;: {<br>          &quot;phases&quot;: {<br>            &quot;hot&quot;: {<br>              &quot;actions&quot;: {<br>                &quot;rollover&quot;: {<br>                  &quot;max_age&quot;: &quot;7d&quot;<br>                }<br>              }<br>            }<br>          }<br>        }<br>      }<br>    JSON<br>  }<br>}</pre><p>Now, we create a Terraform null_resource to apply the above policy with the script we wrote earlier. Notice that the script is called elastic_api_call.sh and it is located inside of the ./scripts/ subfolder, in the same folder as the Terraform files.</p><p>Notice also that we pass the variables for the script as ENV variables. Especially of note is the AUTHORIZATION value, this one should be a Base64 of the es_username:es_password string. In the real world, we would not want to have the password in the codebase, so we plug it in from somewhere else (replacing the &quot;pass&quot; string below), and we use Terraform&#39;s <a href="https://www.terraform.io/language/functions/join">join</a> and <a href="https://www.terraform.io/language/functions/base64encode">base64encode</a> functions to create the string for us (do remember however, that the Base64 string will be stored in your Terraform state. <strong>Always</strong> make sure to properly secure your state files).</p><p>Finally, the ENDPOINT variable should be set to the specific API endpoint we are trying to reach, while CLUSTER_NAME and CLUSTER_RESOURCE_GROUP should be set to the correct values for our Kubernetes cluster:</p><pre>resource &quot;null_resource&quot; &quot;setPolicy&quot; {<br>  provisioner &quot;local-exec&quot; {<br>    interpreter = [&quot;/bin/bash&quot;, &quot;-c&quot;]<br>    command     = &quot;chmod +x ./scripts/elastic_api_call.sh &amp;&amp; ./scripts/elastic_api_call.sh&quot;<br><br>    environment = {<br>      CLUSTER_NAME           = &quot;es-cluster&quot;<br>      CLUSTER_RESOURCE_GROUP = &quot;es-resource-group&quot;<br>      AUTHORIZATION          = base64encode(join(&quot;:&quot;, [&quot;user&quot;, &quot;pass&quot;]))<br>      HTTP_VERB              = &quot;PUT&quot;<br>      ENDPOINT               = &quot;_ilm/policy/${var.lifecyclePolicy.name}&quot;<br>      BODY_JSON              = var.lifecyclePolicy.policy<br>    }<br>  }<br><br>  triggers = {<br>    policy = var.lifecyclePolicy.policy<br>  }<br>}</pre><p>Using the policy definition as a <a href="https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource#optional">trigger</a> will ensure that the resource is run every time changes are made to it, keeping our null_resource idempotent. Triggers can be set to any string, so make sure to set them to a value that makes sense for your specific scenario (such as the policy definition, in this example).</p><h4>Conclusion</h4><p>The script we created can be used with different combinations of endpoint/HTTP verb/body. This means that we can easily create more null_resources as needed, to call different API endpoints in Elasticsearch. In this way, we can keep all our configuration in code, and keep manual work to a minimum.</p><p><em>This story was originally published on my personal blog, </em><a href="https://damn.engineer/"><em>damn.engineer</em></a><em>.</em> <em>You can follow the </em><a href="https://twitter.com/DamnDotEngineer"><em>blog’s Twitter account</em></a><em> for new post notifications, and reach me through my </em><a href="https://twitter.com/theEugeneRomero"><em>personal Twitter</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d11988e046fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Performing Elasticsearch API calls with Terraform, part 1]]></title>
            <link>https://damn-engineer.medium.com/performing-elasticsearch-api-calls-with-terraform-part-1-e14fa3971ea2?source=rss-f326a315ccc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/e14fa3971ea2</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Eugene Romero]]></dc:creator>
            <pubDate>Mon, 25 Apr 2022 11:00:34 GMT</pubDate>
            <atom:updated>2022-04-25T11:00:34.975Z</atom:updated>
            <content:encoded><![CDATA[<h4>Configuring Elasticsearch with Terraform, by means of direct API calls</h4><figure><img alt="A completely unrealistic work scene" src="https://cdn-images-1.medium.com/max/1024/0*pKyYTWvUnYAKJK4B.jpg" /></figure><p>Photo by <a href="https://burst.shopify.com/@kthukral?utm_campaign=photo_credit&amp;utm_content=Free+Stock+Photo+of+Laptop+Coffee+%E2%80%94+HD+Images&amp;utm_medium=referral&amp;utm_source=credit">Karan Thukral</a> on <a href="https://burst.shopify.com/laptop?utm_campaign=photo_credit&amp;utm_content=Free+Stock+Photo+of+Laptop+Coffee+%E2%80%94+HD+Images&amp;utm_medium=referral&amp;utm_source=credit">Burst</a></p><p>If you use <a href="https://www.terraform.io/">Terraform</a> for automating your Elasticsearch deployments, you might find that you need to call the Elasticsearch API during your Terraform runs. Although the Elasticsearch config can cover a lot of ES settings, there are certain changes that can only be made by means of a direct API call to ES. Thankfully, by means of some clever scripting and a <a href="https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource">null-resource</a> in Terraform, we can easily solve this issue.</p><p>NOTE: Even though the information below can be used for a regular instance of Elasticsearch, I am adapting it specifically to Elastic Cloud in Kubernetes (ECK) running on an Azure Kubernetes Service (AKS), as that is the setup we currently have in my project. This only applies to setting up a network tunnel for reaching the endpoints, as otherwise this solution should be identical for any type of ES deployment.</p><h4>Building blocks</h4><p>To build the solution, we will leverage the following components:</p><h4>The Elasticsearch API</h4><p>The ES API is well documented in the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html">official docs</a>. We will create a script that uses curl to perform the calls.</p><h4>Terraform null_resource</h4><p>A <a href="https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource">null_resource</a> allows one to run an arbitrary command or script with Terraform. These resources should be configured with triggers, to ensure that they only run when needed.</p><p>To perform the calls themselves, we will create a Bash script. Let’s get started!</p><h4>Connecting to Elasticsearch</h4><p>Before trying to perform API calls on ES, we need to be able to <em>reach</em> the API. If your ES API is available on your network (or over the Internet), you can probably skip these steps. In my case, our ES is self-contained inside of AKS, and the endpoints are not exposed outside of the cluster. So before we can do anything else, we need to open a tunnel to Elasticsearch.</p><p>The first step, since we are running in AKS, is to acquire credentials to connect to Kubernetes. Assuming az cli is already authenticated with Azure, the only information needed will be the names of the cluster and resource group.</p><p>We also create a “lock file” so the credentials are not pulled multiple times, which can help speed up subsequent runs:</p><pre>if [ ! -f /tmp/kubectl_config_present ]; then<br>  az aks get-credentials \<br>    --name &quot;$CLUSTER_NAME&quot; \<br>    --resource-group &quot;$CLUSTER_RESOURCE_GROUP&quot; &amp;&amp;<br>  touch /tmp/kubectl_config_present;<br>fi</pre><p>Then, we use the port-forward functionality in kubectl to temporarily expose the Elasticsearch API. Make sure to use the correct Kubernetes namespace for the elasticsearch-es-http service. We will use a random high numbered port, and add a 5 second pause, to ensure we don&#39;t try to hit the endpoint before it&#39;s fully available. We also move the process to the background (by means of &amp;), since otherwise it will not release the terminal and the rest of the script won&#39;t run:</p><pre>local_port=$(shuf -i 10000-65000 -n 1);<br><br>kubectl port-forward service/elasticsearch-es-http \<br>  --namespace default &quot;$local_port&quot;:9200 &amp;<br>sleep 5;</pre><h4>Performing the call</h4><p>With the Kubernetes service now exposed, we are able to reach the API. Lets create a flexible curl call that can be used for different endpoints. When we run the script, we will pass it a few variables, including the HTTP verb we want to use, the JSON body (if any), the authorization string (more on that in the next post), and the specific endpoint we want to call.</p><p>If you are not using Kubernetes, make sure to add the correct Elasticsearch URL to the curl call.</p><p>We also use a little “safeguard” at the end to ensure the call was actually successful.</p><pre>if [ -n &quot;$HTTP_VERB&quot; ]; then<br>  verb=&quot;-X $HTTP_VERB&quot;<br>else<br>  verb=&quot;-X GET&quot;<br>fi<br><br>if [ -n &quot;$BODY_JSON&quot; ]; then<br>  body=&quot;-d ${BODY_JSON}&quot;<br>else<br>  body=&quot;&quot;<br>fi<br><br>curl --silent --show-error --fail &quot;$verb&quot; \<br>  -H &quot;Authorization: Basic $AUTHORIZATION&quot; \<br>  -H &quot;Content-Type: application/json&quot; &quot;$body&quot; \<br>  -k &quot;https://localhost:$local_port&quot;/&quot;$ENDPOINT&quot; \<br>  || { export dirty_exit=&quot;true&quot;; };</pre><h4>Cleaning up after ourselves</h4><p>After performing our call, we should shut down the port forward in kubectl:</p><pre>kill -2 &quot;$(pgrep -f &quot;kubectl port-forward.*$local_port&quot;)&quot;;</pre><p>Finally, throw an error in case the call was not successful:</p><pre>if [ &quot;$dirty_exit&quot; == true ]; then<br>  echo -e &quot;Something went wrong! Check output above.&quot;;<br>  exit 1;<br>fi</pre><h4>The TL;DR</h4><p>Putting it all together, our script will look like this (added some comments and terminal colors on the error):</p><pre>#!/bin/bash<br><br># Pull down k8s config, only first time<br>if [ ! -f /tmp/kubectl_config_present ]; then<br>  az aks get-credentials --name &quot;$CLUSTER_NAME&quot; --resource-group &quot;$CLUSTER_RESOURCE_GROUP&quot; &amp;&amp;<br>  touch /tmp/kubectl_config_present;<br>fi<br><br># Start kubectl port-forward and detach it from current console<br>local_port=$(shuf -i 10000-65000 -n 1);<br><br>kubectl port-forward service/elasticsearch-es-http --namespace default &quot;$local_port&quot;:9200 &amp;<br>sleep 5;<br><br># Hit that API!<br>if [ -n &quot;$HTTP_VERB&quot; ]; then<br>  verb=&quot;-X $HTTP_VERB&quot;<br>else<br>  verb=&quot;-X GET&quot;<br>fi<br><br>if [ -n &quot;$BODY_JSON&quot; ]; then<br>  body=&quot;-d ${BODY_JSON}&quot;<br>else<br>  body=&quot;&quot;<br>fi<br><br>curl --silent --show-error --fail &quot;$verb&quot; -H &quot;Authorization: Basic $AUTHORIZATION&quot; -H &quot;Content-Type: application/json&quot; &quot;$body&quot; -k &quot;https://localhost:$local_port&quot;/&quot;$ENDPOINT&quot; || { export dirty_exit=&quot;true&quot;; };<br><br># Ctrl+C the port forward process<br>kill -2 &quot;$(pgrep -f &quot;kubectl port-forward.*$local_port&quot;)&quot;;<br><br>if [ &quot;$dirty_exit&quot; == true ]; then<br>  echo -e &quot;\n\033[0;31mSomething went wrong! Check output above.\033[0m\n\n&quot;;<br>  exit 1;<br>fi</pre><p>This script can be saved anywhere Terraform can see it. For convenience, I will save it to a subfolder called ./scripts/, and will name the script elastic_api_call.sh.</p><p>In my next post, I will show how to run this script with a Terraform null_resource, as well as making sure that the resource only runs whenever we need it to.</p><p><em>This story was originally published on my personal blog, </em><a href="https://damn.engineer/"><em>damn.engineer</em></a><em>.</em> <em>You can follow the </em><a href="https://twitter.com/DamnDotEngineer"><em>blog’s Twitter account</em></a><em> for new post notifications, and reach me through my </em><a href="https://twitter.com/theEugeneRomero"><em>personal Twitter</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e14fa3971ea2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using SSL/TLS certificates from Azure Key Vault in Kubernetes pods]]></title>
            <link>https://damn-engineer.medium.com/using-ssl-tls-certificates-from-azure-key-vault-in-kubernetes-pods-482c95cff0cf?source=rss-f326a315ccc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/482c95cff0cf</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Eugene Romero]]></dc:creator>
            <pubDate>Mon, 07 Feb 2022 11:21:08 GMT</pubDate>
            <atom:updated>2022-02-07T11:21:08.071Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to make Kubernetes pods trust internal HTTPS services</h4><figure><img alt="Padlocks, padlocks everywhere" src="https://cdn-images-1.medium.com/max/1024/0*JcsXqEZdXcl0S0kx.jpg" /></figure><p>Photo by <a href="https://unsplash.com/@parsoakhorsand?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Parsoa Khorsand</a> on <a href="https://unsplash.com/s/photos/lock?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p><p><strong>NOTE: This post builds upon my previous post </strong><a href="https://damn.engineer/2022/01/31/azure-keyvault-to-kubernetes"><strong>Accessing Azure Key Vault secrets from Kubernetes</strong></a><strong>, and assumes understanding of the subject discussed there.</strong></p><p>A common task I face as a DevOps engineer has to do with injecting TLS (formerly SSL) certificates into an application or service. Why would this be needed? There can be many reasons, although by far the one I’ve encountered most, has to do with internal TLS certificates.</p><p>Most medium-to-large enterprises use internal TLS certificates to authenticate internal connections. By <em>internal</em>, I mean certificates which have not been obtained from a known Certificate Signing Authority, but instead, have been locally generated, for use by internal applications. This model requires that any client or user attempting to connect have a <a href="https://stackoverflow.com/a/61422058/4441002">“Certificate Authority Certificate”</a> installed, which makes the system trust certificates generated by that particular (internally created, and unique to the organization) Certificate Authority.</p><p>For example, an organization might have an internal log sink/aggregator which accepts connections over HTTPS. If this sink is only available within the internal network, its TLS certificate will probably have been generated in-house. Now, imagine a microservice running in Kubernetes needs to send logs to this sink. How can we make the Kubernetes pod trust the internal Certificate Authority, so that connections to the log sink are properly secured?</p><p>Although there are probably a few different ways of achieving this result, this is one that I have used which has worked well for me, and does not require any additional helper tools/sidecar containers/etc.</p><h4>Requirements</h4><p>To start off, the CA certificate to be installed in the microservice should be stored in an Azure Key Vault. For simplicity, I will assume that this certificate has been saved as a secret. This method should also work if it has been saved as a certificate, although the syntax might be different. Refer to the <a href="https://azure.github.io/secrets-store-csi-driver-provider-azure/docs/configurations/getting-certs-and-keys/">documentation</a> for more information on how to reference the saved cert.</p><p>Next, our Kubernetes cluster should already have the Kubernetes Secrets Store CSI Driver set up. For instructions on how to do that, check my <a href="https://damn.engineer/2022/01/31/azure-keyvault-to-kubernetes">previous post</a> on the subject.</p><p>The certificate to be used should be in a format that our microservice understands. Since I am using Linux-based microservices, I need to make sure my cert is available as a PEM/CRT file.</p><p>Finally, I am going to assume our microservice is based on some flavor of Debian. If it isn’t, the location to mount the certificate or the command to be run might be slightly different. Refer to your distribution’s docs for specific instructions on how to update the local certificate store.</p><h4>Querying the certificate</h4><p>The certificate can be queried in the same way as any other key vault object. One thing to notice is that we do not create a Kubernetes secret from the Azure secret (notice the missing spec.secretObjects section):</p><pre>apiVersion: secrets-store.csi.x-k8s.io/v1<br>kind: SecretProviderClass<br>metadata:<br>  name: azure-secrets<br>spec:<br>  provider: azure<br>  parameters:<br>    keyvaultName: $KEYVAULT_NAME<br>    tenantId: $SERVICE_PRINCIPAL_TENANT_ID<br>    # Name of the secret containing the certificate<br>    objects: |<br>      array:<br>        - |<br>          objectName: internal-ca-certificate<br>          objectType: secret</pre><p>The secret should now be available for use in our cluster.</p><h4>Mounting the certificate in our microservice</h4><p>With the cert now available, we can use the volume functionality in Kubernetes to mount it in our pod. First, we need to declare our secrets provider as an eligible volume:</p><pre>volumes:<br>  # Can be anything<br>  - name: secrets-provider<br>    csi:<br>      driver: secrets-store.csi.k8s.io<br>      volumeAttributes:<br>        # Should match the name of the SecretProviderClass<br>        secretProviderClass: azure-secrets<br>      # Credentials for authenticating to the key vault,<br>      # see previous post<br>      nodePublishSecretRef:<br>        name: $SERVICE_PRINCIPAL_CREDENTIALS</pre><p>With that out of the way, we should then mount the secret as a file in our pod. This uses a little trick found in the volumeMounts functionality of Kubernetes, where a single file can be mounted <em>into</em> a directory, instead of mounting <em>on top</em> of a directory and overriding its contents. To achieve this, we use the full path of the mounted file, and use the subPath field to indicate the specific file in the volume we wish to mount. In this case, the subPath should match the name of the secret we are querying with our SecretProviderClass:</p><pre>volumeMounts:<br>  # Should match the name of the volume<br>  - name: secrets-provider<br>    # Full path of the mounted file.<br>    # For Debian-based images, it should be<br>    # inside the /usr/local/share/ca-certificates/<br>    # folder, since that is where the system&#39;s<br>    # CA certificates are stored<br>    mountPath: &quot;/usr/local/share/ca-certificates/internal-ca.crt&quot;<br>    # Name of the secret containing the certificate<br>    subPath: &quot;internal-ca-certificate&quot;<br>    readOnly: true</pre><p>With this, the certificate will be available as a file in our pod. However, most Linux-based systems do not just use whatever files are in that folder at any given moment. Instead, the system needs to be told to update the local certificate store, which is built from whatever files are in that directory. We will do that in the next step.</p><h4>Updating the certificate store</h4><p>To update the microservice’s certificate store, we use the update-ca-certificates command. To make sure that our new cert is available for our service from the moment it starts up, we can run this command as part of a spec.containers.lifecycle.postStart instruction. PostStart events are sent immediately after a container is started, which means that our command will be run as soon as possible. Additionally, since volume mounts are performed <em>before</em> startup, we can be sure that our cert will be ready to be included in the local certificate store:</p><pre>lifecycle:<br>  postStart:<br>    exec:<br>      command: [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;update-ca-certificates&quot;]</pre><p>This is the last piece of the puzzle. Putting it all together, our pod deployment should look like this:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: lifecycle-pod<br>spec:<br>  volumes:<br>    - name: secrets-provider<br>      csi:<br>        driver: secrets-store.csi.k8s.io<br>        readOnly: true<br>        volumeAttributes:<br>          secretProviderClass: azure-secrets<br>        nodePublishSecretRef:<br>          name: $SERVICE_PRINCIPAL_CREDENTIALS<br>  containers:<br>    - name: app-with-certificate<br>      image: ubuntu<br>      command: [&quot;sleep&quot;, &quot;300&quot;]<br>      volumeMounts:<br>        - name: secrets-provider<br>          mountPath: &quot;/usr/local/share/ca-certificates/internal-ca.crt&quot;<br>          subPath: &quot;internal-ca-certificate&quot;<br>          readOnly: true<br>      lifecycle:<br>        postStart:<br>          exec:<br>            command: [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;update-ca-certificates&quot;]</pre><p>At this point, our service should be able to perform HTTPS calls to any other internal services using the same CA provider.</p><h4>Verifying</h4><p>To verify if our certificate is indeed working, we can exec into our pod:</p><pre>kubectl exec --stdin --tty app-with-certificate -- /bin/bash</pre><p>Once inside, we can try curling into a known internal service:</p><pre>curl <a href="https://internalsite.mycompany.local/">https://internalsite.mycompany.local</a></pre><p>If the CA certificate has been set up correctly, curl should be able to successfully connect to the HTTPS service without complaining about insecure certificates.</p><p><em>This story was originally published on my personal blog, </em><a href="https://damn.engineer/"><em>damn.engineer</em></a><em>.</em> <em>You can follow the </em><a href="https://twitter.com/DamnDotEngineer"><em>blog’s Twitter account</em></a><em> for new post notifications, and reach me through my </em><a href="https://twitter.com/theEugeneRomero"><em>personal Twitter</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=482c95cff0cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Accessing Azure Key Vault secrets from Kubernetes]]></title>
            <link>https://damn-engineer.medium.com/accessing-azure-key-vault-secrets-from-kubernetes-5dbd2a143cad?source=rss-f326a315ccc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/5dbd2a143cad</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Eugene Romero]]></dc:creator>
            <pubDate>Mon, 31 Jan 2022 14:18:44 GMT</pubDate>
            <atom:updated>2022-01-31T14:18:44.842Z</atom:updated>
            <content:encoded><![CDATA[<h4>Querying and injecting Azure Key Vault secrets into Kubernetes microservices</h4><figure><img alt="Application secrets should be properly protected" src="https://cdn-images-1.medium.com/max/1024/0*JhHWuw846aUF-u5P.jpg" /></figure><p>Photo by <a href="https://unsplash.com/@flyd2069?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">FLY:D</a> on <a href="https://unsplash.com/s/photos/padlock?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p><p>If you use Kubernetes to run your applications, sooner or later your cluster pods will need access to <strong>secrets</strong>.</p><p>Of course, putting secrets in code is a <a href="https://littlemaninmyhead.wordpress.com/2021/04/05/why-we-shouldnt-commit-secrets-into-source-code-repositories/">very bad idea</a>. Many cyber-attacks and vulnerabilities could have been avoided if a password/API key/connection string/etc had not been committed to a code base.</p><p>On the other hand, modern applications, especially in a DevOps environment, should have all of their configuration available programmatically. Having to manually add configuration to running services would defeat the purpose of “Automating All The Things”.</p><p>Nowadays, a common approach to secrets management is the use of Vaults. Vaults keep secrets safe, controlling access to them, and providing APIs for querying them. If you have any secrets in your cloud-based application, you should be storing them in a vault. All major cloud providers have vaults readily available for customers.</p><p>So, how can Kubernetes-based applications access vault secrets in a secure and automated way?</p><p>Recently, I was tasked with finding the answer to that question in my current project. After some research and testing, I found what is currently the best (only?) solution for this:</p><p>Enter <a href="https://secrets-store-csi-driver.sigs.k8s.io/introduction.html"><strong>Kubernetes Secrets Store CSI Driver</strong></a>. Now that’s a mouthful.</p><h4>Secrets Store Driver</h4><p>At the most basic level, the Kubernetes Secrets Store CSI Driver (from now on, <strong>KSSCD</strong>) is a tool which connects to a vault, pulls one or multiple secrets from it, and makes them available inside the Kubernetes cluster. Pods can then use those secrets natively, without any additional work. If the secrets update on the Vault side, KSSCD will make those updates available in the cluster.</p><p>KSSCD is able to query secrets from many different types of vaults. For this example, I am going to use Azure Key Vault.</p><p>To accomplish this task, we need to perform the following steps:</p><ol><li>An identity needs to be created for granting KSSCD access to the vault</li><li>KSSCD must be installed in the cluster</li><li>KSSCD must be configured to know which vault secrets to query, and which Kubernetes secret(s) to create from them</li><li>Pods have to be configured to use the new Kubernetes secret(s)</li></ol><h4>Granting KSSCD access to Azure Key Vault</h4><p>For KSSCD to have access to the key vault, we must create a new Service Principal, or identity, give it permissions on key vault objects, and store the SP credentials as a Kubernetes secret. There are many ways of creating Service Principals, but my preferred way is by using the Azure CLI:</p><pre>az ad sp create-for-rbac --name KSSCD-ServicePrincipal</pre><p>Note down the returned appID, password, and tenant.</p><p>With our new SP in hand, we can now grant it get permissions on our key vault objects:</p><pre>az keyvault set-policy -n &quot;$KEYVAULT_NAME&quot; \<br>  --secret-permissions get \<br>  --key-permissions get \<br>  --certificate-permissions get \<br>  --spn &quot;$SERVICE_PRINCIPAL_APP_ID&quot;</pre><p>Finally, we create a Kubernetes secret to hold our Service Principal information. Note that the secret containing the credentials needs to be created in the <strong>same namespace</strong> as the application pod. If pods in multiple namespaces need to use the same credentials to access the key vault, this secret needs to be created in <em>each</em> namespace:</p><pre>kubectl create secret generic keyvault-credentials \<br>  --from-literal clientid=&quot;$SERVICE_PRINCIPAL_APP_ID&quot; \<br>  --from-literal clientsecret=&quot;$SERVICE_PRINCIPAL_APP_PASSWORD&quot;<br><br># KSSCD requires that the secret have this specific label<br>kubectl label secret keyvault-credentials \<br>  secrets-store.csi.k8s.io/used=true</pre><p>The credentials are now ready to be used by KSSCD.</p><h4>Installing KSSCD</h4><p>It’s now time to actually install KSSCD in our Kubernetes cluster. In my case, I chose to do it by means of the official Helm chart. Note that this installs the CSI Secrets Provider, and the required bits for an Azure-specific deployment:</p><pre>helm repo add csi-secrets-store-provider-azure https://raw.githubusercontent.com/Azure/secrets-store-csi-driver-provider-azure/master/charts<br><br>helm install csi csi-secrets-store-provider-azure/csi-secrets-store-provider-azure --namespace kube-system</pre><p>There are several reasons for installing KSSCD in the kube-system namespace, which are outlined in the <a href="https://azure.github.io/secrets-store-csi-driver-provider-azure/getting-started/installation/">official documentation</a>.</p><p>With KSSCD now installed, we are ready to tell it where to go looking for secrets.</p><h4>Configuring KSSCD</h4><p>To configure KSSCD to make key vault secrets available locally, we must create a Kubernetes SecretProviderClass resource. In my experience, I prefer creating a SecretProviderClass for each microservice. There are a few reasons for this:</p><ol><li>It is not easy to have pods in one namespace read secrets from a different namespace. Since each one of my application’s microservices lives in a different namespace, it also makes sense to have a local SecretProviderClass create local secrets exclusive to that microservice&#39;s needs.</li><li>This helps avoid secrets leaking, by creating a series of small Kubernetes secrets, instead of one huge secret with everything in it.</li></ol><p>To create a SecretProviderClass, the following YAML can be customized and deployed to the same namespace as the pods that will use the secrets. In this case, we will query the Azure Key Vault objects key-vault-secret-1 and key-vault-secret-2, and make their values available inside the namespace in a new Kubernetes secret called foo-secrets:</p><pre>apiVersion: secrets-store.csi.x-k8s.io/v1<br>kind: SecretProviderClass<br>metadata:<br>  name: azure-secrets-microservice-foo<br>spec:<br>  provider: azure<br>  parameters:<br>    keyvaultName: $KEYVAULT_NAME<br>    # Azure AD tenant ID. Received when we created the SP.<br>    tenantId: $SERVICE_PRINCIPAL_TENANT_ID<br>    # List of secrets to pull from the key vault <br>    objects: |<br>      array:<br>        - |<br>          objectName: key-vault-secret-1<br>          objectType: secret<br>        - |<br>          objectName: key-vault-secret-2<br>          objectType: secret<br>  # The new Kubernetes secret to create<br>  secretObjects:<br>    # Name of the new Kubernetes secret<br>    - secretName: foo-secrets<br>      type: Opaque<br>      data:<br>        # A key name inside the new secret<br>        - key: databasepassword<br>          # Secret value to use<br>          objectName: key-vault-secret-1<br>        - key: clientsecret<br>          objectName: key-vault-secret-2</pre><p>Note that it is possible to create multiple Kubernetes secrets under secretObjects, and populate them with different keys and values.</p><p>Save this file as secret-provider-class.yaml and deploy it to the cluster with kubectl:</p><pre>kubectl apply -f ./secret-provider-class.yaml</pre><p>At this point, KSSCD is configured to access Azure, but has not actually made a connection to the key vault yet. For that, we have to configure a pod to use the secrets provided by KSSCD.</p><h4>Using the secrets</h4><p>Because of how KSSCD works, secrets are only queried from the key vault <em>when a pod attempts to mount them as a volume</em>. My understanding is that this is because of KSSCD being a CSI driver. Therefore, to use our secrets in a pod, we also need to make them available as volumes.</p><p>Here is a sample pod deployment, which mounts our secrets on /mnt/secrets, and then creates environment variables from those secrets:</p><pre>kind: Pod<br>apiVersion: v1<br>metadata:<br>  # The pod we are creating<br>  name: foo-microservice<br>spec:<br>  volumes:<br>    # The volume created by KSSCD. This block makes it available<br>    # to the pod for mounting. It can be named anything we want.<br>    - name: secrets-volume<br>      csi:<br>        driver: secrets-store.csi.k8s.io<br>        readOnly: true<br>        volumeAttributes:<br>          # Which SecretProviderClass is providing this volume?<br>          secretProviderClass: azure-secrets-microservice-foo<br>        # This is the secret with the SP credentials, which <br>        # KSSCD will use to connect to the key vault <br>        nodePublishSecretRef:<br>          name: keyvault-credentials<br>  containers:<br>    - name: busybox<br>      image: busybox:latest<br>      command:<br>        - &quot;/bin/sleep&quot;<br>        - &quot;10000&quot;<br>      volumeMounts:<br>        # Mount the above volume. This also makes our secrets<br>        # available as files in the pod&#39;s filesystem. Crucially,<br>        # this step also creates the Kubernetes secret we<br>        # defined in the SecretProviderClass<br>        - name: secrets-volume<br>          mountPath: &quot;/mnt/secrets&quot;<br>          readOnly: true<br>      env:<br>        - name: NON-SECRET-ENV<br>          value: &quot;some value&quot;<br>        # Create ENV variables from the Kubernetes secret<br>        - name: DB_PASSWORD<br>          valueFrom:<br>            secretKeyRef:<br>              name: foo-secrets<br>              key: databasepassword<br>        - name: CLIENT_SECRET<br>          valueFrom:<br>            secretKeyRef:<br>              name: foo-secrets<br>              key: clientsecret<br>        # ENVs can be used inside of other ENVs if needed<br>        - name: CONNECTION_STRING_WITH_SECRET_INJECTED<br>          value:<br>            &quot;Server=db;User ID=user;Password=$(DB_PASSWORD);&quot;</pre><p>Lets save this file as pod.yaml and deploy it to the cluster:</p><pre>kubectl apply -f ./pod.yaml</pre><p>Once the pod successfully deploys, our secrets will be available to the pod in two ways:</p><ol><li>As files in the path we specified in spec.containers.volumeMounts.mountPath. The files will be named <strong>the same way as the Azure Key Vault secret</strong> (in our example, these files will be /mnt/secrets/key-vault-secret-1 and /mnt/secrets/key-vault-secret-2). <strong>All</strong> queried secrets will be available here.</li><li>As environment variables. <strong>Only secrets explicitly set up under </strong><strong>spec.containers.env will be available as ENVs</strong>.</li></ol><p>In this way, we can choose which secrets we only need as files (certificate files for example), and which ones we want to use as environment variables.</p><h4>What happens if a secret is updated in the key vault?</h4><p>KSSCD will query key vaults to check for changes from time to time. However, if a secret being used as an ENV variable is updated at the source, the pod will need to be restarted for the new ENV variables to become available to it. More information on secret rotation can be found in the <a href="https://secrets-store-csi-driver.sigs.k8s.io/topics/secret-auto-rotation.html">official docs</a>.</p><h4>Conclusion</h4><p>The Kubernetes Secrets Store CSI Driver is a very powerful tool which can be leveraged to keep secrets and code separate. By using it in our Kubernetes clusters, our entire workflow can be automated, while maintaining security all around.</p><p>Questions? Suggestions? Reach out on <a href="https://twitter.com/theEugeneRomero">Twitter</a> and let me know!</p><p><em>This story was originally published on my personal blog, </em><a href="https://damn.engineer/"><em>damn.engineer</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5dbd2a143cad" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Accessing multiple Azure subscriptions in a single Terraform run]]></title>
            <link>https://damn-engineer.medium.com/accessing-multiple-azure-subscriptions-in-a-single-terraform-run-129aa1971586?source=rss-f326a315ccc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/129aa1971586</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Eugene Romero]]></dc:creator>
            <pubDate>Mon, 24 Jan 2022 08:56:53 GMT</pubDate>
            <atom:updated>2022-01-25T08:10:53.004Z</atom:updated>
            <content:encoded><![CDATA[<h4>A tale of two Azure subscriptions</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s7Z4ZgTQLJ2T3iAXl9W2KA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@aeschwarz?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Adrian Schwarz</a> on <a href="https://unsplash.com/s/photos/cities?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>When infrastructure is declared as Terraform code, resources are usually only created in a single Azure subscription. It normally is best practice to keep multiple subscriptions separated in code, to prevent ending up with a large codebase which can be difficult to maintain and understand. However, there are times when it is necessary, or most logical, to create or query resources from different subscriptions. Luckily, Terraform allows us to work with two (or more) subscriptions in a single run if needed, by means of <a href="https://www.terraform.io/language/providers/configuration#alias-multiple-provider-configurations">configuration aliases</a>.</p><p>As an example, say we have two subscriptions, one called main and one secondary. Each of those subscriptions has its own Terraform repository and resources, including their own Azure Key Vaults. Now, imagine we need to put a newly created password into both of them, since this secret will be used by applications on both subscriptions.</p><h3>Credentials, a.k.a. Service Principals</h3><p>The first thing needed will be credentials for each subscription. We could create a single Service Principal and give it access to both subscriptions, but I recommend dividing up access to reduce the attack surface if any Service Principal was compromised.</p><p>If you are already using Terraform, chances are you already have Service Principals which you use for your Terraform runs. If not, we can create two new ones, one for each subscription. I will not get into the specifics of how to do that here, and instead recommend following the <a href="https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/guides/service_principal_client_secret">official Terraform documentation</a>.</p><h3>Configuring Terraform to use multiple Azure providers</h3><p>With our newly minted Service Principals (SPs) on hand, we can now configure Terraform to use them both.</p><p>First, we add some variables to hold the data for both subscriptions and SPs. That way, this sensitive information can be injected at runtime, for example by means of <a href="https://www.terraform.io/language/values/variables#environment-variables">environment variables</a>.</p><p><strong>variables.tf</strong></p><pre># The Active Directory tenant ID.<br># This one should be the same for both SPs<br># and subscriptions<br>variable TENANT_ID { type=string }<br><br># Data for the &quot;main&quot; subscription and SP<br>variable SUBSCRIPTION_ID_MAIN { type=string }<br>variable SERVICE_PRINCIPAL_ID_MAIN { type=string }<br>variable SERVICE_PRINCIPAL_SECRET_MAIN { type=string }<br><br># Data for the &quot;secondary&quot; subscription and SP<br>variable SUBSCRIPTION_ID_SECONDARY { type=string }<br>variable SERVICE_PRINCIPAL_ID_SECONDARY { type=string }<br>variable SERVICE_PRINCIPAL_SECRET_SECONDARY { type=string }</pre><p>With the variables in place, we can tell Terraform to use the <a href="https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs">azurerm</a> provider with two separate configurations. Notice the alias field in the config for the secondary subscription, which will allow us to specify when we want to use this one. <strong>Otherwise, Terraform will default to using the block with no </strong><strong>alias declared.</strong></p><p>We also add the <a href="https://registry.terraform.io/providers/hashicorp/random/latest">random</a> provider, which we will use for creating the password we are saving to both Key Vaults:</p><p><strong>config.tf</strong></p><pre>terraform {<br>  required_providers {<br>    azurerm = {<br>      version = &quot;~&gt;2.90&quot;<br>    }<br>    random = {<br>      version = &quot;~&gt;3.1&quot;<br>    }<br>  }<br>}<br><br># Configuration for our &quot;main&quot; subscription<br>provider &quot;azurerm&quot; {<br>  tenant_id       = var.TENANT_ID<br>  subscription_id = var.SUBSCRIPTION_ID_MAIN<br>  client_id       = var.SERVICE_PRINCIPAL_ID_MAIN<br>  client_secret   = var.SERVICE_PRINCIPAL_SECRET_MAIN<br>  features {}<br>}<br><br># Configuration for the &quot;secondary&quot; subscription<br>provider &quot;azurerm&quot; {<br>  alias           = &quot;secondary&quot;<br>  tenant_id       = var.TENANT_ID<br>  subscription_id = var.SUBSCRIPTION_ID_SECONDARY<br>  client_id       = var.SERVICE_PRINCIPAL_ID_SECONDARY<br>  client_secret   = var.SERVICE_PRINCIPAL_SECRET_SECONDARY<br>  features {}<br>}</pre><p>Terraform is now ready to work with both subscriptions.</p><h3>Accessing and modifying resources</h3><p>With that out of the way, we can create the actual resources we need. We start by finding out data about both Key Vaults. Once again, notice the use of provider to query the non-default subscription:</p><p><strong>data.tf</strong></p><pre># Query data from the default subscription<br>data &quot;azurerm_key_vault&quot; &quot;main_key_vault&quot; {<br>  name                = &quot;kv-main&quot;<br>  resource_group_name = &quot;rg-main&quot;<br>}<br><br># Query the secondary subscription<br>data &quot;azurerm_key_vault&quot; &quot;secondary_key_vault&quot; {<br>  provider            = azurerm.secondary<br>  name                = &quot;kv-secondary&quot;<br>  resource_group_name = &quot;rg-secondary&quot;<br>}</pre><p>Now that we have what we need, we can finally create the secret, and store it into both Key Vaults, using the provider block when appropriate:</p><p><strong>shared_secret.tf</strong></p><pre>resource &quot;random_password&quot; &quot;shared_password&quot; {<br>  length = 64<br>}<br><br># Saving the password in the main key vault<br>resource &quot;azurerm_key_vault_secret&quot; &quot;shared_password_main&quot; {<br>  name         = &quot;super-secret-password&quot;<br>  value        = random_password.shared_password.result<br>  key_vault_id = data.azurerm_key_vault.main_key_vault.id<br>}<br><br># And in the secondary, using the &quot;provider&quot; field again<br>resource &quot;azurerm_key_vault_secret&quot; &quot;shared_password_secondary&quot; {<br>  provider     = azurerm.secondary<br>  name         = &quot;super-secret-password&quot;<br>  value        = random_password.shared_password.result<br>  key_vault_id = data.azurerm_key_vault.secondary_key_vault.id<br>}</pre><p>And just like that, we have created our secret and saved it to both Key Vaults. If we ever need to rotate the secret, we can just taint the single random_password resource, and Terraform will update all the Key Vaults automatically. Neat!</p><h3>Final notes</h3><p>Something to keep in mind is that all of these resources will be stored in the same terraform-state file. Also, make sure you don&#39;t end up creating circular dependencies, where each repository needs data from resources created in the other repository. These are a few of the reasons why it is good to be mindful about only using this little trick when appropriate.</p><p><em>This story was originally published on my personal blog, </em><a href="https://damn.engineer/"><em>damn.engineer</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=129aa1971586" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bash alias for cleaning git branches]]></title>
            <link>https://damn-engineer.medium.com/bash-alias-for-cleaning-git-branches-677d73332de8?source=rss-f326a315ccc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/677d73332de8</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[bash]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Eugene Romero]]></dc:creator>
            <pubDate>Sun, 09 Jan 2022 23:00:00 GMT</pubDate>
            <atom:updated>2022-01-20T14:23:52.787Z</atom:updated>
            <content:encoded><![CDATA[<p>As a git user, I got tired of constantly seeing my list of branches grow out of control. Git is great for keeping the history of a repository, but at least for me, there isn&#39;t a lot of value in keeping deleted branches in my local history. Besides, if you are using <a href="https://trunkbaseddevelopment.com/short-lived-feature-branches/">short-lived feature branches</a> (and you <em>really</em> should be), you will be creating and deleting several new branches per week.</p><p>Because of this, I decided to create an alias to remove old branches from my local machine. My criteria was:</p><ol><li>Only branches which have been merged to master should be deleted</li><li>My local list of remote branches should be cleaned up, to remove non-existing branches</li></ol><p>With that criteria in mind, I came up with the following steps:</p><h3>The solution</h3><p>First, return a list of all local branches which have already been merged to master. Crucially, this does not include any branches with commits <strong>different</strong> from master:</p><pre>git branch --merged master</pre><p>This list also includes the local master branch, as well as the currently checked out branch (denoted by a *). This grep removes that:</p><pre>grep -v -e &#39;master&#39; -e &#39;\*&#39;</pre><p>Finally, delete all remaining results. The lowercase -d flag also ensures only fully merged branches get deleted (as opposed to the delete all -D):</p><pre>xargs -n 1 git branch -d</pre><p>Afterwards, do a prune of the local copy of remote, to remove any references to non-existing remote branches:</p><pre>git remote prune origin</pre><p>Putting it all together, I ended up with this alias, which I then added to my .bashrc file:</p><p><strong>git-clean-branches:</strong></p><pre>alias git-clean-branches=&quot;git branch --merged master | grep -v -e &#39;master&#39; -e &#39;\*&#39; | xargs -n 1 git branch -d &amp;&amp; git remote prune origin || echo &#39;No local branches to remove, so nothing done.&#39;&quot;</pre><p>Now, my clean-up ritual after completing a pull request is always the following:</p><pre>gcm # this is an alias for &quot;git checkout master&quot;<br>git pull # grab the latest master<br>git-clean-branches # clean up all merged local branches and prune my local list of remote branches</pre><p>Automate all the things!</p><p><em>Originally published at </em><a href="https://damn.engineer/2022/01/10/git-clean-branches"><em>https://damn.engineer</em></a><em> on January 9, 2022.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=677d73332de8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Don’t Shave that Yak!]]></title>
            <link>https://damn-engineer.medium.com/dont-shave-that-yak-872e994da32b?source=rss-f326a315ccc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/872e994da32b</guid>
            <category><![CDATA[azure-devops]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <category><![CDATA[work-smart]]></category>
            <dc:creator><![CDATA[Eugene Romero]]></dc:creator>
            <pubDate>Mon, 17 Jun 2019 08:17:33 GMT</pubDate>
            <atom:updated>2019-06-17T08:17:33.309Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BO9ysrMOtQ_z5JGFfDNw-g.jpeg" /><figcaption>Really, don’t. Image credit: <a href="http://agileanimal.blogspot.com/">http://agileanimal.blogspot.com</a></figcaption></figure><p>Yesterday, I had to do a two-line code change to a single source file in our project. Literally all that had to be done was to replace a hard-coded value into a variable. “Easy”, I thought. Lo and behold, after making the change, the CI pipeline decided it no longer wanted to complete successfully. “What gives?” I asked myself. Looking into the issue, it seemed the pipeline fail might not be related to my change; instead, it appeared to be a mix of not having pushed any changes to the repository over the past week (so no pipeline runs had occurred in that period) and a dependency which had been updated in that time after not having been touched by its developer in several months.</p><p>“Ok, no problem, lets try to make this work”.</p><p>The error message I was getting was a bit cryptic, so I went into the dependency’s GitHub page to try and figure out what was breaking. After some digging around and looking through open and closed issues on the project’s site, I realized that the developer had added support for the new version of its parent tool (Terraform), but without also accounting for backwards compatibility. But, oh no! our pipelines run on Azure DevOps’ provided agents, which still don’t have that new version of Terraform on them! So this is why the pipe was breaking. The old version of Terraform didn’t understand the new version’s way of doing things.</p><p>“Ok, what to do now? I suppose I could get around the old version on the agents by modifying the pipeline to download and set up the latest executable, at least until Microsoft updates their images.” But what other things will break with this change? This is a major version change (11 to 12), and apparently a lot of things had changed in between versions. Oh wait, Terraform has a helper in its executable to tell you what things will no longer work in your code, cool. My workstation was on the same version of Terraform as the agents, so after downloading and setting up the new version in my machine, I ran the helper. This tool not only tells you what is deprecated, but will also fix things for you if possible, so lets hope there aren’t a lot of changes and… wait, all of a sudden I was sitting on <em>a lot</em> of modified files in my branch. I also realized that, since Terraform does not look in subfolders, I would need to re-run this process within every subfolder as well, for example the ones where we keep our modules. Ok, maybe I can write a script that does this, instead of manually going into every folder and running these commands. Should I use Bash or Ruby, hmm? Afterwards, I’ll have to modify the pipeline YAML so it downloads and upgrades Terraform every time it runs, and afterwards I should probably find out how often MS updates their images…</p><p>You can see where this is going.</p><p>A 10 min change of two lines, had become a multi-hour project with a modified pipeline and dozens of changed files in a pull request. And who knows what else I would have needed to do afterwards! At this point, my colleague and I realized that this was an exercise in futility. “Why don’t we pin the library to the last-know working version, wait until MS updates their agent images, then create a task for tackling the upgrade properly?” 3 modified lines later (the original two plus a new one for pinning the dependency version), and the pipeline was happy again.</p><p>I think this GIF describes the situation perfectly:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FmbluZIdy0Ww5q%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FmbluZIdy0Ww5q%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FmbluZIdy0Ww5q%2Fgiphy-downsized-large.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8c7f272e089ff58b70acf4e566e5461/href">https://medium.com/media/d8c7f272e089ff58b70acf4e566e5461/href</a></iframe><p>What is the lesson here? <a href="https://seths.blog/2005/03/dont_shave_that/">Don’t Shave that Yak</a>. Stick to the planned work. If new, unexpected work appears while you’re doing planned work, don’t jump on it, however tempting it might be. If the work can be planned for later, do that. If it absolutely can’t, inform your lead first so that the work can be accounted for and planned, before performing it.</p><p>Otherwise, you might find yourself shaving a Yak when all you wanted to do was wax your car.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=872e994da32b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>