E hōʻoia i nā Kubernetes me ka hoʻohana ʻana iā GitHub OAuth a me Dex

Ke hōʻike aku nei au iā ʻoe i kahi aʻoaʻo no ka hoʻomohala ʻana i kahi hui Kubernetes me Dex, dex-k8s-authenticator a me GitHub.

E hōʻoia i nā Kubernetes me ka hoʻohana ʻana iā GitHub OAuth a me Dex
Meme kūloko mai ka ʻōlelo Lūkini Kubernetes kamaʻilio ma Telegram

Hōʻike

Hoʻohana mākou i nā Kubernetes e hana i nā kaiapuni ikaika no ka hoʻomohala a me ka hui QA. No laila makemake mākou e hāʻawi iā lākou i ke komo ʻana i ka hui no ka dashboard a me kubectl. ʻAʻole like me OpenShift, ʻaʻohe o vanilla Kubernetes i ka hōʻoia maoli, no laila hoʻohana mākou i nā mea hana ʻaoʻao ʻekolu no kēia.

Ma kēia hoʻonohonoho hoʻohana mākou:

  • dex-k8s-authenticator  - noi pūnaewele no ka hoʻokumu ʻana i ka kubectl config
  • Dex — Mea hoʻolako OpenID Connect
  • GitHub - no ka mea hoʻohana mākou iā GitHub i kā mākou hui

Ua ho'āʻo mākou e hoʻohana iā Google OIDC, akā naʻe, ʻo mākou ua hāʻule e hoʻomaka iā lākou me nā hui, no laila ua kūpono ka hoʻohui ʻana me GitHub iā mākou. Me ka ʻole o ka palapala ʻāina hui, ʻaʻole hiki ke hana i nā kulekele RBAC e pili ana i nā hui.

No laila, pehea e hana ai kā mākou hana ʻae Kubernetes ma kahi hōʻike ʻike.

E hōʻoia i nā Kubernetes me ka hoʻohana ʻana iā GitHub OAuth a me Dex
Kaʻina hana mana

ʻO kahi kikoʻī hou aʻe a me kahi kikoʻī:

  1. Hoʻopili ka mea hoʻohana i dex-k8s-authenticator (login.k8s.example.com)
  2. Hoʻouna ʻo dex-k8s-authenticator i ka noi iā Dex (dex.k8s.example.com)
  3. Hoʻihoʻi hou ʻo Dex i ka ʻaoʻao inoa inoa ʻo GitHub
  4. Hoʻopuka ʻo GitHub i ka ʻike mana kūpono a hoʻihoʻi iā Dex
  5. Hāʻawi ʻo Dex i ka ʻike i loaʻa iā dex-k8s-authenticator
  6. Loaʻa ka mea hoʻohana i kahi hōʻailona OIDC mai GitHub
  7. Hoʻohui ʻo dex-k8s-authenticator i ka hōʻailona i kubeconfig
  8. Hāʻawi ʻo kubectl i ka hōʻailona iā KubeAPIServer
  9. Hoʻihoʻi ʻo KubeAPIServer i nā komo i kubectl ma muli o ka hōʻailona i hala
  10. Loaʻa ka mea hoʻohana mai kubectl

Nā hana hoʻomākaukau

ʻOiaʻiʻo, ua loaʻa iā mākou kahi hui Kubernetes i hoʻokomo ʻia (k8s.example.com), a hele pū me HELM i hoʻokomo mua ʻia. Loaʻa iā mākou kahi hui ma GitHub (super-org).
Inā ʻaʻohe HELM, e hoʻokomo iā ia he mea maʻalahi.

Pono mua mākou e hoʻonohonoho iā GitHub.

E hele i ka ʻaoʻao hoʻonohonoho hoʻonohonoho, (https://github.com/organizations/super-org/settings/applications) a hana i kahi noi hou (Authorized OAuth App):
E hōʻoia i nā Kubernetes me ka hoʻohana ʻana iā GitHub OAuth a me Dex
Ke hana nei i kahi noi hou ma GitHub

E hoʻopiha i nā kahua me nā URL kūpono, no ka laʻana:

  • HKH ʻaoʻao home: https://dex.k8s.example.com
  • HKH kāhea hoʻihoʻi mana: https://dex.k8s.example.com/callback

E makaʻala me nā loulou, he mea nui ʻaʻole e nalowale nā ​​ʻoki.

I ka pane ʻana i kahi palapala i hoʻopau ʻia, e hana ʻo GitHub Client ID и Client secret, mālama iā lākou ma kahi palekana, e pono iā mākou (e laʻana, hoʻohana mākou Vault no ka mālama ʻana i nā mea huna):

Client ID: 1ab2c3d4e5f6g7h8
Client secret: 98z76y54x32w1

E hoʻomākaukau i nā moʻolelo DNS no nā subdomains login.k8s.example.com и dex.k8s.example.com, a me nā palapala SSL no ke komo ʻana.

E hana kākou i nā palapala SSL:

cat <<EOF | kubectl create -f -
apiVersion: certmanager.k8s.io/v1alpha1
kind: Certificate
metadata:
  name: cert-auth-dex
  namespace: kube-system
spec:
  secretName: cert-auth-dex
  dnsNames:
    - dex.k8s.example.com
  acme:
    config:
    - http01:
        ingressClass: nginx
      domains:
      - dex.k8s.example.com
  issuerRef:
    name: le-clusterissuer
    kind: ClusterIssuer
---
apiVersion: certmanager.k8s.io/v1alpha1
kind: Certificate
metadata:
  name: cert-auth-login
  namespace: kube-system
spec:
  secretName: cert-auth-login
  dnsNames:
    - login.k8s.example.com
  acme:
    config:
    - http01:
        ingressClass: nginx
      domains:
      - login.k8s.example.com
  issuerRef:
    name: le-clusterissuer
    kind: ClusterIssuer
EOF
kubectl describe certificates cert-auth-dex -n kube-system
kubectl describe certificates cert-auth-login -n kube-system

ClusterIssuer me ke poʻo inoa le-clusterissuer Pono e noho, akā inā ʻaʻole, e hana me ka hoʻohana ʻana iā HELM:

helm install --namespace kube-system -n cert-manager stable/cert-manager
cat << EOF | kubectl create -f -
apiVersion: certmanager.k8s.io/v1alpha1
kind: ClusterIssuer
metadata:
  name: le-clusterissuer
  namespace: kube-system
spec:
  acme:
    server: https://acme-v02.api.letsencrypt.org/directory
    email: [email protected]
    privateKeySecretRef:
      name: le-clusterissuer
    http01: {}
EOF

KubeAPIServer hoʻonohonoho

No ka hana ʻana o kubeAPIServer, pono ʻoe e hoʻonohonoho i ka OIDC a hoʻonui i ka pūʻulu:

kops edit cluster
...
  kubeAPIServer:
    anonymousAuth: false
    authorizationMode: RBAC
    oidcClientID: dex-k8s-authenticator
    oidcGroupsClaim: groups
    oidcIssuerURL: https://dex.k8s.example.com/
    oidcUsernameClaim: email
kops update cluster --yes
kops rolling-update cluster --yes

Hoʻohana mākou kops no ka waiho ʻana i nā pūʻulu, akā hana like kēia no nā hui pūʻulu ʻē aʻe.

Dex hoʻonohonoho a me dex-k8s-authenticator

No ka hana ʻana o Dex, pono ʻoe e loaʻa i kahi palapala hōʻoia a me kahi kī mai ka haku Kubernetes, e kiʻi mai laila mai:

sudo cat /srv/kubernetes/ca.{crt,key}
-----BEGIN CERTIFICATE-----
AAAAAAAAAAABBBBBBBBBBCCCCCC
-----END CERTIFICATE-----
-----BEGIN RSA PRIVATE KEY-----
DDDDDDDDDDDEEEEEEEEEEFFFFFF
-----END RSA PRIVATE KEY-----

E hoʻopili i ka waihona dex-k8s-authenticator:

git clone [email protected]:mintel/dex-k8s-authenticator.git
cd dex-k8s-authenticator/

Ke hoʻohana nei i nā faila waiwai, hiki iā mākou ke hoʻonohonoho maʻalahi i nā loli no kā mākou HELM palapala.

E wehewehe kākou i ka hoʻonohonoho no Dex:

cat << EOF > values-dex.yml
global:
  deployEnv: prod
tls:
  certificate: |-
    -----BEGIN CERTIFICATE-----
    AAAAAAAAAAABBBBBBBBBBCCCCCC
    -----END CERTIFICATE-----
  key: |-
    -----BEGIN RSA PRIVATE KEY-----
    DDDDDDDDDDDEEEEEEEEEEFFFFFF
    -----END RSA PRIVATE KEY-----
ingress:
  enabled: true
  annotations:
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: "true"
  path: /
  hosts:
    - dex.k8s.example.com
  tls:
    - secretName: cert-auth-dex
      hosts:
        - dex.k8s.example.com
serviceAccount:
  create: true
  name: dex-auth-sa
config: |
  issuer: https://dex.k8s.example.com/
  storage: # https://github.com/dexidp/dex/issues/798
    type: sqlite3
    config:
      file: /var/dex.db
  web:
    http: 0.0.0.0:5556
  frontend:
    theme: "coreos"
    issuer: "Example Co"
    issuerUrl: "https://example.com"
    logoUrl: https://example.com/images/logo-250x25.png
  expiry:
    signingKeys: "6h"
    idTokens: "24h"
  logger:
    level: debug
    format: json
  oauth2:
    responseTypes: ["code", "token", "id_token"]
    skipApprovalScreen: true
  connectors:
  - type: github
    id: github
    name: GitHub
    config:
      clientID: $GITHUB_CLIENT_ID
      clientSecret: $GITHUB_CLIENT_SECRET
      redirectURI: https://dex.k8s.example.com/callback
      orgs:
      - name: super-org
        teams:
        - team-red
  staticClients:
  - id: dex-k8s-authenticator
    name: dex-k8s-authenticator
    secret: generatedLongRandomPhrase
    redirectURIs:
      - https://login.k8s.example.com/callback/
envSecrets:
  GITHUB_CLIENT_ID: "1ab2c3d4e5f6g7h8"
  GITHUB_CLIENT_SECRET: "98z76y54x32w1"
EOF

A no dex-k8s-authenticator:

cat << EOF > values-auth.yml
global:
  deployEnv: prod
dexK8sAuthenticator:
  clusters:
  - name: k8s.example.com
    short_description: "k8s cluster"
    description: "Kubernetes cluster"
    issuer: https://dex.k8s.example.com/
    k8s_master_uri: https://api.k8s.example.com
    client_id: dex-k8s-authenticator
    client_secret: generatedLongRandomPhrase
    redirect_uri: https://login.k8s.example.com/callback/
    k8s_ca_pem: |
      -----BEGIN CERTIFICATE-----
      AAAAAAAAAAABBBBBBBBBBCCCCCC
      -----END CERTIFICATE-----
ingress:
  enabled: true
  annotations:
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: "true"
  path: /
  hosts:
    - login.k8s.example.com
  tls:
    - secretName: cert-auth-login
      hosts:
        - login.k8s.example.com
EOF

E hoʻouka iā Dex a me dex-k8s-authenticator:

helm install -n dex --namespace kube-system --values values-dex.yml charts/dex
helm install -n dex-auth --namespace kube-system --values values-auth.yml charts/dex-k8s-authenticator

E nānā kākou i ka hana o nā lawelawe (e hoʻihoʻi ʻo Dex i ke code 400, a ʻo dex-k8s-authenticator e hoʻihoʻi i ka code 200):

curl -sI https://dex.k8s.example.com/callback | head -1
HTTP/2 400
curl -sI https://login.k8s.example.com/ | head -1
HTTP/2 200

Hoʻonohonoho RBAC

Hoʻokumu mākou i kahi ClusterRole no ka hui, i kā mākou hihia me ka loaʻa heluhelu-wale nō:

cat << EOF | kubectl create -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: cluster-read-all
rules:
  -
    apiGroups:
      - ""
      - apps
      - autoscaling
      - batch
      - extensions
      - policy
      - rbac.authorization.k8s.io
      - storage.k8s.io
    resources:
      - componentstatuses
      - configmaps
      - cronjobs
      - daemonsets
      - deployments
      - events
      - endpoints
      - horizontalpodautoscalers
      - ingress
      - ingresses
      - jobs
      - limitranges
      - namespaces
      - nodes
      - pods
      - pods/log
      - pods/exec
      - persistentvolumes
      - persistentvolumeclaims
      - resourcequotas
      - replicasets
      - replicationcontrollers
      - serviceaccounts
      - services
      - statefulsets
      - storageclasses
      - clusterroles
      - roles
    verbs:
      - get
      - watch
      - list
  - nonResourceURLs: ["*"]
    verbs:
      - get
      - watch
      - list
  - apiGroups: [""]
    resources: ["pods/exec"]
    verbs: ["create"]
EOF

E hana i kahi hoʻonohonoho no ClusterRoleBinding:

cat <<EOF | kubectl create -f -
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: dex-cluster-auth
  namespace: kube-system
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-read-all
subjects:
  kind: Group
  name: "super-org:team-red"
EOF

I kēia manawa ua mākaukau mākou no ka hoʻāʻo.

Nā ho'āʻo

E hele i ka ʻaoʻao komo (https://login.k8s.example.com) a komo i ka hoʻohana ʻana i kāu moʻokāki GitHub:

E hōʻoia i nā Kubernetes me ka hoʻohana ʻana iā GitHub OAuth a me Dex
ʻAoʻao komo

E hōʻoia i nā Kubernetes me ka hoʻohana ʻana iā GitHub OAuth a me Dex
Hoʻihoʻi ʻia ka ʻaoʻao komo i GitHub

E hōʻoia i nā Kubernetes me ka hoʻohana ʻana iā GitHub OAuth a me Dex
 E hahai i nā kuhikuhi i hana ʻia no ka loaʻa ʻana

Ma hope o ka hoʻopaʻa kope ʻana mai ka ʻaoʻao pūnaewele, hiki iā mākou ke hoʻohana i ka kubectl e hoʻokele i kā mākou kumu waiwai cluster:

kubectl get po
NAME                READY   STATUS    RESTARTS   AGE
mypod               1/1     Running   0          3d

kubectl delete po mypod
Error from server (Forbidden): pods "mypod" is forbidden: User "[email protected]" cannot delete pods in the namespace "default"

A hana ia, hiki i nā mea hoʻohana a pau o GitHub i kā mākou hui ke ʻike i nā kumuwaiwai a komo i loko o nā pods, akā ʻaʻohe o lākou kuleana e hoʻololi iā lākou.

Source: www.habr.com

Pākuʻi i ka manaʻo hoʻopuka