Hamarino amin'ny Kubernetes amin'ny fampiasana GitHub OAuth sy Dex

Manome torolalana ho anao aho amin'ny famoronana fidirana amin'ny cluster Kubernetes mampiasa Dex, dex-k8s-authenticator ary GitHub.

Hamarino amin'ny Kubernetes amin'ny fampiasana GitHub OAuth sy Dex
Meme eo an-toerana avy amin'ny resaka Kubernetes amin'ny teny Rosiana telegrama

fampidirana

Mampiasa Kubernetes izahay hamorona tontolo mavitrika ho an'ny ekipa fampandrosoana sy QA. Noho izany dia tiantsika ny hanome azy ireo fidirana amin'ny cluster ho an'ny dashboard sy kubectl. Tsy sahala amin'ny OpenShift, vanilla Kubernetes dia tsy manana fanamarinana teratany, noho izany dia mampiasa fitaovana avy amin'ny antoko fahatelo izahay amin'izany.

Amin'ity fanamafisana ity dia mampiasa:

  • dex-k8s-authenticator  - fampiharana tranonkala amin'ny famoronana kubectl config
  • Dex - Mpanome OpenID Connect
  • GitHub - satria fotsiny mampiasa GitHub ao amin'ny orinasanay izahay

Nanandrana nampiasa Google OIDC izahay, saingy indrisy fa izahay tsy nahomby hanombohana azy ireo miaraka amin'ny vondrona, ka ny fampidirana amin'ny GitHub dia mety tsara ho antsika. Raha tsy misy sarintany vondrona, dia tsy azo atao ny mamorona politika RBAC mifototra amin'ny vondrona.

Noho izany, ahoana no fiasan'ny dingan'ny fanomezan-dàlana Kubernetes amin'ny fanehoana hita maso:

Hamarino amin'ny Kubernetes amin'ny fampiasana GitHub OAuth sy Dex
Fomba fanomezan-dàlana

Tsipiriany sy teboka isaky ny teboka:

  1. Ny mpampiasa dia miditra ao amin'ny dex-k8s-authenticator (login.k8s.example.com)
  2. dex-k8s-authenticator dia mandefa ny fangatahana amin'i Dex (dex.k8s.example.com)
  3. Dex dia mitodika mankany amin'ny pejy fidirana GitHub
  4. GitHub dia mamorona ny fampahalalana momba ny fanomezan-dàlana ilaina ary mamerina izany amin'ny Dex
  5. Dex dia mandefa ny fampahalalana voaray amin'ny dex-k8s-authenticator
  6. Ny mpampiasa dia mahazo marika OIDC avy amin'ny GitHub
  7. dex-k8s-authenticator dia manampy marika amin'ny kubeconfig
  8. Kubectl dia mandefa ny famantarana amin'ny KubeAPIServer
  9. KubeAPIServer dia mamerina ny fidirana amin'ny kubectl mifototra amin'ny marika nandalo
  10. Ny mpampiasa dia mahazo fidirana amin'ny kubectl

Hetsika fanomanana

Mazava ho azy fa efa misy cluster Kubernetes napetraka (k8s.example.com), ary tonga miaraka amin'ny HELM efa napetraka mialoha. Manana fikambanana ao amin'ny GitHub (super-org) ihany koa izahay.
Raha tsy manana HELM ianao dia apetraho izany tena tsotra.

Voalohany dia mila manangana GitHub isika.

Mandehana any amin'ny pejin'ny firafitry ny fandaminana, (https://github.com/organizations/super-org/settings/applications) ary mamorona fampiharana vaovao (App OAuth nahazo alalana):
Hamarino amin'ny Kubernetes amin'ny fampiasana GitHub OAuth sy Dex
Mamorona fampiharana vaovao amin'ny GitHub

Fenoy ny saha miaraka amin'ny URL ilaina, ohatra:

  • URL an-trano: https://dex.k8s.example.com
  • URL fanomezan-dàlana hiantsoana: https://dex.k8s.example.com/callback

Mitandrema amin'ny rohy, zava-dehibe ny tsy ho very slash.

Ho setrin'ny endrika feno dia hamokatra ny GitHub Client ID и Client secret, tehirizo ao amin'ny toerana azo antoka izy ireo, dia mahasoa antsika (ohatra, mampiasa andohalambony eny ho fitehirizana tsiambaratelo):

Client ID: 1ab2c3d4e5f6g7h8
Client secret: 98z76y54x32w1

Omano ny rakitra DNS ho an'ny zana-tohatra login.k8s.example.com и dex.k8s.example.com, ary koa ny mari-pankasitrahana SSL ho an'ny fidirana.

Andao hamorona certificat 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 misy lohateny le-clusterissuer tokony efa misy, fa raha tsy izany dia mamorona azy amin'ny 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

KubeAPISserver configuration

Mba hiasa ny kubeAPIServer dia mila manamboatra OIDC ianao ary manavao ny cluster:

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

Mampiasa izahay kops ho an'ny fametrahana clusters, saingy miasa toy izany koa izany mpitantana cluster hafa.

Dex configuration sy dex-k8s-authenticator

Raha te hiasa i Dex dia mila manana taratasy fanamarinana sy lakile avy amin'ny tompon'ny Kubernetes ianao, andao haka azy avy any:

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

Andao hatao clone ny tahiry dex-k8s-authenticator:

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

Amin'ny alàlan'ny fisie soatoavina, azontsika atao ny manitsy ny variables ho an'ny antsika Sarin'ny HELM.

Andeha hofaritana ny firafitry ny 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

Ary ho an'ny 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

Mametraka Dex sy 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

Andeha hojerentsika ny fiasan'ny serivisy (Dex dia tokony hamerina ny code 400, ary ny dex-k8s-authenticator dia tokony hamerina ny 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

RBAC configuration

Mamorona ClusterRole ho an'ny vondrona izahay, amin'ny tranga misy antsika amin'ny fidirana vakiana fotsiny:

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

Andao hamorona tefy ho an'ny 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

Efa vonona amin'ny fitsapana izahay.

fitsapana

Mandehana any amin'ny pejy fidirana (https://login.k8s.example.com) ary midira mampiasa ny kaontinao GitHub:

Hamarino amin'ny Kubernetes amin'ny fampiasana GitHub OAuth sy Dex
Pejy fidirana

Hamarino amin'ny Kubernetes amin'ny fampiasana GitHub OAuth sy Dex
Pejy fidirana naverina mankany amin'ny GitHub

Hamarino amin'ny Kubernetes amin'ny fampiasana GitHub OAuth sy Dex
 Araho ny toromarika novokarina mba hahazoana fidirana

Aorian'ny fametahana kopia avy amin'ny pejin-tranonkala dia afaka mampiasa kubectl izahay hitantana ireo loharanon-tsofinay:

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"

Ary miasa izany, ny mpampiasa GitHub rehetra ao amin'ny fikambananay dia afaka mahita loharano sy miditra amin'ny pods, saingy tsy manan-jo hanova izany izy ireo.

Source: www.habr.com

Add a comment