<p>I am reading the tekton <a href="https://tekton.dev/vault/pipelines-v0.14.3/auth/" rel="noreferrer">docs</a> on authentication it explains that two things are needed to do authentication</p>
<p>Create a secret docs give example below</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
 name: basic-user-pass
 annotations:
   tekton.dev/git-0: https://github.com # Described below
type: kubernetes.io/basic-auth
stringData:
 username: &lt;username&gt;
 password: &lt;password&gt;
</code></pre>
<p>Add secret object to the service account</p>
<pre><code>apiVersion: v1
kind: ServiceAccount
metadata:
 name: build-bot
secrets:
 - name: basic-user-pass
</code></pre>
<p>My mental model of a service account in k8s is that it is a JWT used to access the k8s API server. I don't understand what's point of adding a secret to the ServiceAccount, how and why it is used.</p>
<p>Questions:</p>
<ul>
<li>what does it mean for a service account to have secrets added to it?</li>
<li>Why is it useful to add a secret to a service account?</li>
<li>Why does tekton require the secret to be added to the service account?</li>
<li>who/what looks at the service account secrets list?</li>
</ul>

ams
<p>hope you are enjoying your kubernetes journey !</p>
<p><em><strong>1) what does it mean for a service account to have secrets added to it? / Why is it useful to add a secret to a service account?</strong></em></p>
<p>Fist of all, a little remider:</p>
<p>As you may know, you have to see the serviceAccount as a user for a machine/an application/a script (and not only in kubernetes) in short, everything that is not human. As a human a service account, in order to authenticate to things (Git repository/docker registry or API that require authentication, needs to have credentials (username+password).</p>
<p>In Kubernetes this credentials and especially the password are stored in &quot;secrets&quot;.</p>
<p>Now, you should be aware that each namespace in kubernetes has a native service account named &quot;default&quot; that is associated with <strong>every</strong> running pod and that service account is linked to a native &quot;default&quot; kubernetes secret that is also present in all namespaces.
This &quot;default&quot; secret contains the ca.crt and a token that let the pod to make calls to the internal Kubernetes API Server endpoint among other things.</p>
<p>Since the secrets that contains the &quot;credentials&quot; is linked to a service account that is mounted to a pod, this pod can then be able to authenticate to things that require authentication.</p>
<p>For example if someday you'll have to use a private docker registry to pull your images, you can do this in two ways, In each of them you have to create a secret first that will contain your sensitive data (crendentials):</p>
<ul>
<li>The first way consist of adding your secret name,that contains the registry credentials directly in the default serviceAccount (that, as a reminder, is mounted by default in the pod, or in a new created serviceAccount (like tekton is doing in your case) that will be added to the kubernetes deployment manifest in the field <code>serviceAccountName:</code>.</li>
<li>The second way consist of adding the field <code>imagePullSecret</code> in your kubernetes deployment manifest.</li>
</ul>
<p>This way, when kubernetes comes to pull your private docker image, it will check if the needed credentials that are in the serviceAccount secrets works, if not it will check the secret you have added in the imagePullSecret field (or the opposite) and it will be able to connect to the registry and pull the image to run it as a container in a pod !</p>
<p><em><strong>2) who/what looks at the service account secrets list?</strong></em></p>
<p>For example in a brand new namespace:</p>
<pre><code>❯ k get sa
NAME      SECRETS   AGE
default   1         30m
</code></pre>
<p>This default serviceAccount is linked to a secret named &quot;default-token-r4vrb&quot;:</p>
<pre><code>❯ k get sa default -o yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  creationTimestamp: &quot;2022-05-06T08:48:38Z&quot;
  name: default
  namespace: so-tests
  resourceVersion: &quot;1771&quot;
  uid: c3598708-ad14-4806-af31-5c54d60e29b7
secrets:
- name: default-token-r4vrb
</code></pre>
<p>This default-token secret contains what is needed to authenticate the Kubernetes APi endpoint (certificate + token):</p>
<pre><code>❯ k get secret default-token-r4vrb -o yaml
apiVersion: v1
data:
  ca.crt: base64encodedCaCertificate
  namespace: base64encodedNamespace
  token: base64encodedToken
kind: Secret
metadata:
  annotations:
    kubernetes.io/service-account.name: default
    kubernetes.io/service-account.uid: c3598708-ad14-4806-af31-5c54d60e29b7
  creationTimestamp: &quot;2022-05-06T08:48:38Z&quot;
  name: default-token-r4vrb
  namespace: so-tests
  resourceVersion: &quot;1770&quot;
  uid: d342a372-66d1-4c92-b520-23c23babc798
type: kubernetes.io/service-account-token
</code></pre>
<p><em><strong>3) Why does tekton require the secret to be added to the service account?
who/what looks at the service account secrets list?</strong></em></p>
<p>Now I hope you know why, they choose to use a serviceAccount to do this but they could have just mounted the secret into the pod directly also :)</p>
<p>Hope this has helped you. Here is some docs to be more familiar with K8S SA:
<a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/" rel="noreferrer">https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/</a></p>
<p>bguess/</p>
