<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>K8S | Breek Een Been</title>
    <link>https://blog.breekeenbeen.nl/tag/k8s/</link>
      <atom:link href="https://blog.breekeenbeen.nl/tag/k8s/index.xml" rel="self" type="application/rss+xml" />
    <description>K8S</description>
    <generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><copyright>Rob Maas</copyright><lastBuildDate>Sat, 25 Mar 2023 13:03:12 +0100</lastBuildDate>
    <image>
      <url>https://blog.breekeenbeen.nl/images/icon_hue0c3a5851739ca8a2afc787728ee763e_182872_512x512_fill_lanczos_center_3.png</url>
      <title>K8S</title>
      <link>https://blog.breekeenbeen.nl/tag/k8s/</link>
    </image>
    
    <item>
      <title>Automate Export certificates and keys from Kubernetes and import in Palo Alto Networks</title>
      <link>https://blog.breekeenbeen.nl/post/automate-export-of-certificates-from-k8s-and-import-in-palo-alto-networks/</link>
      <pubDate>Sat, 25 Mar 2023 13:03:12 +0100</pubDate>
      <guid>https://blog.breekeenbeen.nl/post/automate-export-of-certificates-from-k8s-and-import-in-palo-alto-networks/</guid>
      <description>&lt;p&gt;In my precious post I explained how to export certificates and keys from Kubernetes and how to set a password on the key file for import in Palo Alto Networks (by hand). Since I&amp;rsquo;ve several services running on Kubernetes, all with there own certificate and key. I wanted to automate this process. This is good practice, especially since the certificates are relatively short lived, which means I need to renew the certificates on the firewall at least every 90 days. The first step I did was creating a bash script that would take care of this.&lt;/p&gt;
&lt;p&gt;I know I&amp;rsquo;ve hardcoded the passphrase on the certificate key, for this temporarily solution this is fine. I can delete all &lt;code&gt;crt&lt;/code&gt; and &lt;code&gt;key&lt;/code&gt; files after the import.&lt;/p&gt;
&lt;p&gt;Next step is of course to see if I can automate this when a certificate is renewed. The &lt;code&gt;watch&lt;/code&gt; command could help with this.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;#!/bin/bash
#
# Description: Get all certificates from all namespaces and import them into the Palo Alto Networks firewall
# Uses: awk, kubectl, openssl

FW_HOST=&amp;quot;1.2.3.4&amp;quot;
API_KEY=&amp;quot;VERYSECRETAPIKEY&amp;quot;

while read ns cert
do 
    echo &amp;quot;retrieving: $ns $cert&amp;quot;
    # Get the certificate and key from the secret
    kubectl get secrets -n $ns $cert -o json | jq -r &#39;.data.&amp;quot;tls.crt&amp;quot;&#39; | base64 -d &amp;gt; $cert.crt
    kubectl get secrets -n $ns $cert -o json | jq -r &#39;.data.&amp;quot;tls.key&amp;quot;&#39; | base64 -d &amp;gt; $cert.key
    # Set the password for the key - This is required for the import on the Palo Alto Networks firewall
    openssl rsa -aes256 -in $cert.key -out $cert.key -passout &amp;quot;pass:P@ssw0rd!&amp;quot; 
    # Import the certificate and key into the Palo Alto Networks firewall
    curl -k -X POST -F &amp;quot;file=@$cert.crt&amp;quot; &amp;quot;https://$FW_HOST/api/?key=$API_KEY&amp;amp;type=import&amp;amp;category=certificate&amp;amp;certificate-name=$cert&amp;amp;format=pem&amp;quot;
    curl -k -X POST -F &amp;quot;file=@$cert.key&amp;quot; &amp;quot;https://$FW_HOST/api/?key=$API_KEY&amp;amp;type=import&amp;amp;category=private-key&amp;amp;certificate-name=$cert&amp;amp;format=pem&amp;amp;passphrase=P@ssw0rd!&amp;quot;
done &amp;lt; &amp;lt;(kubectl get certificates -A -o custom-columns=NAMESPACE:.metadata.namespace,SECRET:.spec.secretName --no-headers --sort-by=.metadata.namespace | awk &#39;{print $1 &amp;quot; &amp;quot; $2}&#39;)
&lt;/code&gt;&lt;/pre&gt;
</description>
    </item>
    
    <item>
      <title>Export certificates and keys from Kubernetes</title>
      <link>https://blog.breekeenbeen.nl/post/export-certificates-and-keys-from-k8s/</link>
      <pubDate>Mon, 20 Feb 2023 19:03:12 +0100</pubDate>
      <guid>https://blog.breekeenbeen.nl/post/export-certificates-and-keys-from-k8s/</guid>
      <description>&lt;p&gt;One of the things I really believe in is a layered security approach, if one of the layers fail you will have other layers still providing protection. One of the first layers is often the network. However even if you have a &amp;ldquo;next-gen&amp;rdquo; / layer-7 firewall, it is only fully utilized when the traffic is &amp;ldquo;readable&amp;rdquo; and therefore not encrypted. Think of protection against SQLi, XSS, etc.&lt;/p&gt;
&lt;p&gt;Nowadays it is very easy (and free) to protect your services with TLS, think of services like 
&lt;a href=&#34;https://letsencrypt.org&#34; target=&#34;_blank&#34; rel=&#34;noopener&#34;&gt;Let&amp;rsquo;s Encrypt&lt;/a&gt;. With Kubernetes it is even easier, as you can use the 
&lt;a href=&#34;https://cert-manager.io&#34; target=&#34;_blank&#34; rel=&#34;noopener&#34;&gt;cert-manager&lt;/a&gt; to automatically request and renew certificates.&lt;/p&gt;
&lt;p&gt;These certficates and keys are stored in Kubernetes as secrets. When you want your firewall to decrypt the traffic, you need to export the certificates and keys from Kubernetes and import them to your firewall, in my case a Palo Alto Networks firewall. This is called SSL Inbound decryption, since we own the certificate and private key, the firewall can simply read along. Just to note, this is different with SSL Outbound (Forward Proxy) decryption, wich is a bit more complex or at least it involves additional components.&lt;/p&gt;
&lt;p&gt;To list all the certificates on your Kubernetes cluster run the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;&amp;gt; k get certificates -A
NAMESPACE       NAME                              READY   SECRET                               AGE
website         blog.breekeenbeen.nl              True    blog.breekeenbeen.nl                 110d
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;The output is slightly modified to not expose all applications and URLs.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now that we now the namespace and the name of the secret we can export the certificate and key to a file, this however has a few caveats.
The secret, basically consistes of two entries, the certificate and the key. Secondly the certificate and key are base64 encoded. So we need to decode the base64 encoded certificate and key and write them to a file.&lt;/p&gt;
&lt;p&gt;The easiest way is to print the output to &lt;code&gt;json&lt;/code&gt; format and use &lt;code&gt;jq&lt;/code&gt; to export the specific field, this output can then be base64 decoded and written to a file.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;k get secrets -n website blog.breekeenbeen.nl -o json | jq -r &#39;.data.&amp;quot;tls.crt&amp;quot;&#39; | base64 -d &amp;gt; blog.breekeenbeen.nl.crt
k get secrets -n website blog.breekeenbeen.nl -o json | jq -r &#39;.data.&amp;quot;tls.key&amp;quot;&#39; | base64 -d &amp;gt; blog.breekeenbeen.nl.key
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we have the files, we can normally import them into our firewall. In my case I use a Palo Alto Networks firewall, which won&amp;rsquo;t accept the key file without a passphrase set (when using the WUI import). So I need to add a passphrase to the key file.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;openssl rsa -aes256 -in blog.breekeenbeen.nl.key -out blog.breekeenbeen.nl.key
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can import the certificate and key into the firewall and create a decryption rule.&lt;/p&gt;





  
  











&lt;figure &gt;


  &lt;a data-fancybox=&#34;&#34; href=&#34;https://blog.breekeenbeen.nl/post/export-certificates-and-keys-from-k8s/featured_hubdc80fe51b8afe0a4c537b6dc672a3bf_50382_2000x2000_fit_lanczos_3.png&#34; &gt;


  &lt;img data-src=&#34;https://blog.breekeenbeen.nl/post/export-certificates-and-keys-from-k8s/featured_hubdc80fe51b8afe0a4c537b6dc672a3bf_50382_2000x2000_fit_lanczos_3.png&#34; class=&#34;lazyload&#34; alt=&#34;&#34; width=&#34;1670&#34; height=&#34;232&#34;&gt;
&lt;/a&gt;



&lt;/figure&gt;

&lt;p&gt;See the column on the right, this is indicates that the traffic is decrypted.&lt;/p&gt;





  
  











&lt;figure &gt;


  &lt;a data-fancybox=&#34;&#34; href=&#34;https://blog.breekeenbeen.nl/post/export-certificates-and-keys-from-k8s/decrypted_hu913595ee3ddfe9a7aef9802107481933_47387_2000x2000_fit_lanczos_3.png&#34; &gt;


  &lt;img data-src=&#34;https://blog.breekeenbeen.nl/post/export-certificates-and-keys-from-k8s/decrypted_hu913595ee3ddfe9a7aef9802107481933_47387_2000x2000_fit_lanczos_3.png&#34; class=&#34;lazyload&#34; alt=&#34;&#34; width=&#34;1862&#34; height=&#34;144&#34;&gt;
&lt;/a&gt;



&lt;/figure&gt;

&lt;p&gt;This work great, however since (Let&amp;rsquo;s Encrypt) certificates will renew every 90 days (not the private key), it would make sense to automate this process.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
