ตรวจสอบสิทธิ์ใน Kubernetes โดยใช้ GitHub OAuth และ Dex

ฉันขอนำเสนอบทช่วยสอนในการสร้างการเข้าถึงคลัสเตอร์ Kubernetes โดยใช้ Dex, dex-k8s-authenticator และ GitHub

ตรวจสอบสิทธิ์ใน Kubernetes โดยใช้ GitHub OAuth และ Dex
มีมท้องถิ่นจากการแชท Kubernetes ภาษารัสเซีย Telegram

การแนะนำ

เราใช้ Kubernetes เพื่อสร้างสภาพแวดล้อมแบบไดนามิกสำหรับทีมพัฒนาและ QA ดังนั้นเราจึงต้องการให้พวกเขาเข้าถึงคลัสเตอร์สำหรับทั้งแดชบอร์ดและ kubectl Vanilla Kubernetes ต่างจาก OpenShift ตรงที่ไม่มีการตรวจสอบสิทธิ์แบบดั้งเดิม ดังนั้นเราจึงใช้เครื่องมือจากภายนอกสำหรับสิ่งนี้

ในการกำหนดค่านี้เราใช้:

  • ตัวตรวจสอบสิทธิ์ dex-k8s  — เว็บแอปพลิเคชันสำหรับสร้างการกำหนดค่า kubectl
  • Dex — ผู้ให้บริการ OpenID Connect
  • GitHub - เพียงเพราะเราใช้ GitHub ในบริษัทของเรา

เราพยายามใช้ Google OIDC แต่น่าเสียดายที่เรา ล้มเหลว เพื่อเริ่มต้นด้วยกลุ่ม ดังนั้นการบูรณาการกับ GitHub จึงเหมาะกับเราค่อนข้างดี หากไม่มีการแมปกลุ่ม จะไม่สามารถสร้างนโยบาย RBAC ตามกลุ่มได้

ดังนั้น กระบวนการอนุญาต Kubernetes ของเราทำงานอย่างไรในการแสดงภาพ:

ตรวจสอบสิทธิ์ใน Kubernetes โดยใช้ GitHub OAuth และ Dex
ขั้นตอนการอนุญาต

รายละเอียดเพิ่มเติมเล็กน้อยและทีละประเด็น:

  1. ผู้ใช้เข้าสู่ระบบ dex-k8s-authenticator (login.k8s.example.com)
  2. dex-k8s-authenticator ส่งต่อคำขอไปยัง Dex (dex.k8s.example.com)
  3. Dex เปลี่ยนเส้นทางไปยังหน้าเข้าสู่ระบบ GitHub
  4. GitHub จะสร้างข้อมูลการอนุญาตที่จำเป็นและส่งคืนให้กับ Dex
  5. Dex ส่งข้อมูลที่ได้รับไปยัง dex-k8s-authenticator
  6. ผู้ใช้ได้รับโทเค็น OIDC จาก GitHub
  7. dex-k8s-authenticator เพิ่มโทเค็นให้กับ kubeconfig
  8. kubectl ส่งโทเค็นไปที่ KubeAPIServer
  9. KubeAPIServer ส่งคืนการเข้าถึง kubectl ตามโทเค็นที่ส่งผ่าน
  10. ผู้ใช้ได้รับการเข้าถึงจาก kubectl

การเตรียมการ

แน่นอนว่าเราได้ติดตั้งคลัสเตอร์ Kubernetes ไว้แล้ว (k8s.example.com) และยังมาพร้อมกับ HELM ที่ติดตั้งไว้ล่วงหน้าอีกด้วย เรายังมีองค์กรบน GitHub (super-org)
หากคุณไม่มี HELM ให้ติดตั้ง ง่ายมาก.

ก่อนอื่นเราต้องตั้งค่า GitHub

ไปที่หน้าการตั้งค่าองค์กร (https://github.com/organizations/super-org/settings/applications) และสร้างแอปพลิเคชันใหม่ (แอป OAuth ที่ได้รับอนุญาต):
ตรวจสอบสิทธิ์ใน Kubernetes โดยใช้ GitHub OAuth และ Dex
การสร้างแอปพลิเคชันใหม่บน GitHub

กรอกข้อมูลในช่องที่มี URL ที่จำเป็น เช่น:

  • URL หน้าแรก: https://dex.k8s.example.com
  • URL โทรกลับเพื่อขออนุมัติ: https://dex.k8s.example.com/callback

ระวังลิงก์ด้วย สิ่งสำคัญคือต้องไม่เสียเครื่องหมายทับ

เพื่อตอบสนองต่อแบบฟอร์มที่กรอกเสร็จแล้ว GitHub จะสร้าง Client ID и Client secretเก็บไว้ในที่ปลอดภัยก็จะเป็นประโยชน์กับเรา (เช่น เราใช้ หกคะเมน เพื่อเก็บความลับ):

Client ID: 1ab2c3d4e5f6g7h8
Client secret: 98z76y54x32w1

เตรียมระเบียน DNS สำหรับโดเมนย่อย login.k8s.example.com и dex.k8s.example.comตลอดจนใบรับรอง SSL สำหรับทางเข้า

มาสร้างใบรับรอง 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 พร้อมหัวเรื่อง le-clusterissuer ควรมีอยู่แล้ว แต่ถ้าไม่มี ให้สร้างมันขึ้นมาโดยใช้ 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

เพื่อให้ kubeAPIServer ทำงาน คุณต้องกำหนดค่า OIDC และอัปเดตคลัสเตอร์:

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

เราใช้ คอปส์ สำหรับการปรับใช้คลัสเตอร์ แต่วิธีนี้ใช้ได้ผลคล้ายกัน ผู้จัดการคลัสเตอร์อื่นๆ.

การกำหนดค่า Dex และ dex-k8s-authenticator

เพื่อให้ Dex ทำงานได้ คุณต้องมีใบรับรองและคีย์จากต้นแบบ Kubernetes มาเริ่มกันเลย:

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

มาโคลนพื้นที่เก็บข้อมูล dex-k8s-authenticator กัน:

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

การใช้ไฟล์ค่าทำให้เราสามารถกำหนดค่าตัวแปรสำหรับเราได้อย่างยืดหยุ่น แผนภูมิหมวกกันน็อค.

อธิบายการกำหนดค่าสำหรับ 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

และสำหรับ 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

ติดตั้ง Dex และ 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

มาตรวจสอบการทำงานของบริการกัน (Dex ควรส่งคืนรหัส 400 และ dex-k8s-authenticator ควรส่งคืนรหัส 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

การกำหนดค่า RBAC

เราสร้าง ClusterRole สำหรับกลุ่ม ในกรณีของเราที่มีสิทธิ์การเข้าถึงแบบอ่านอย่างเดียว:

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

มาสร้างการกำหนดค่าสำหรับ 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

ตอนนี้เราพร้อมสำหรับการทดสอบแล้ว

การทดสอบ

ไปที่หน้าเข้าสู่ระบบ (https://login.k8s.example.com) และเข้าสู่ระบบโดยใช้บัญชี GitHub ของคุณ:

ตรวจสอบสิทธิ์ใน Kubernetes โดยใช้ GitHub OAuth และ Dex
หน้าเข้าสู่ระบบ

ตรวจสอบสิทธิ์ใน Kubernetes โดยใช้ GitHub OAuth และ Dex
หน้าเข้าสู่ระบบเปลี่ยนเส้นทางไปที่ GitHub

ตรวจสอบสิทธิ์ใน Kubernetes โดยใช้ GitHub OAuth และ Dex
 ปฏิบัติตามคำแนะนำที่สร้างขึ้นเพื่อเข้าใช้งาน

หลังจากคัดลอกและวางจากหน้าเว็บแล้ว เราสามารถใช้ kubectl เพื่อจัดการทรัพยากรคลัสเตอร์ของเราได้:

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"

และได้ผล ผู้ใช้ GitHub ทุกคนในองค์กรของเราสามารถดูทรัพยากรและเข้าสู่ระบบพ็อดได้ แต่พวกเขาไม่มีสิทธิ์ในการเปลี่ยนแปลง

ที่มา: will.com

เพิ่มความคิดเห็น