ืื ื ืืฆืื ืืชืฉืืืช ืืื ืืืจืื ืืืฆืืจืช ืืืฉื ืืืฉืืื Kubernetes ืืืืฆืขืืช Dex, dex-k8s-authenticator ื-GitHub.
ืื ืืงืืื ื-Kubernetes ืืฉืคื ืืจืืกืืช ืฆ'ืื ืคื ืืื
ืืืื
ืื ื ืืฉืชืืฉืื ื-Kubernetes ืืื ืืืฆืืจ ืกืืืืืช ืืื ืืืืช ืขืืืจ ืฆืืืช ืืคืืชืื ืื-QA. ืื ืื ืื ื ืจืืฆืื ืืชืช ืืื ืืืฉื ืืืฉืืื ืื ืืืื ืืืืืื ืื ืืื ื-kubectl. ืื ืืืื ื-OpenShift, ื-vanilla Kubernetes ืืื ืืืืืช ืืงืืจื, ืืืื ืื ื ืืฉืชืืฉืื ืืืืื ืฉื ืฆื ืฉืืืฉื ืืฉื ืื.
ืืชืฆืืจื ืื ืื ื ืืฉืชืืฉืื ื:
dex-k8s-authenticatorโ - ืืืฉืื ืืื ืืจื ื ืืืคืงืช kubectl configืืงืก - ืกืคืง OpenID Connect- GitHub - ืคืฉืื ืื ืื ืื ื ืืฉืชืืฉืื ื-GitHub ืืืืจื ืฉืื ื
ื ืืกืื ื ืืืฉืชืืฉ ื-Google OIDC, ืืื ืืฆืขืจื ื
ืื ืืื ืขืืื ืชืืืื ืืืจืฉืื ืฉื Kubernetes ืฉืื ื ืืืืฆืื ืืืืชื:
ืชืืืื ืืจืฉืื
ืงืฆืช ืืืชืจ ืคืืจืื ืื ืงืืื ืืืจ ื ืงืืื:
- ืืฉืชืืฉ ืืชืืืจ ื-dex-k8s-authenticator (
login.k8s.example.com
) - dex-k8s-authenticator ืืขืืืจ ืืช ืืืงืฉื ืืืงืก (
dex.k8s.example.com
) - Dex ืืคื ื ืืืฃ ืืื ืืกื ืฉื GitHub
- GitHub ืืืืฆืจ ืืช ืคืจืื ืืืจืฉืื ืืืจืืฉืื ืืืืืืจ ืืืชื ื-Dex
- Dex ืืขืืืจ ืืช ืืืืืข ืฉืืชืงืื ื-dex-k8s-authenticator
- ืืืฉืชืืฉ ืืงืื ืืกืืืื OIDC ื-GitHub
- dex-k8s-authenticator ืืืกืืฃ ืืกืืืื ื-kubeconfig
- kubectl ืืขืืืจ ืืช ืืืกืืืื ื-KubeAPIServer
- KubeAPIServer ืืืืืจ ืืืฉื ื-kubectl ืขื ืกืื ืืืกืืืื ืฉืขืืจ
- ืืืฉืชืืฉ ืืงืื ืืืฉื ื-kubectl
ืคืขืืืืืืช ืืื ื
ืืืืื ืฉืืืจ ืืืชืงื ืื ื ืืฉืืื Kubernetes (k8s.example.com
), ืืืืืข ืื ืขื HELM ืืืชืงื ืืจืืฉ. ืืฉ ืื ื ืื ืืจืืื ื-GitHub (ืกืืคืจ-org).
ืื ืืื ืื HELM, ืืชืงื ืืืชื
ืจืืฉืืช ืขืืื ื ืืืืืืจ ืืช GitHub.
ืขืืืจ ืืืฃ ืืืืจืืช ืืืจืืื, (https://github.com/organizations/super-org/settings/applications
) ืืฆืืจ ืืคืืืงืฆืื ืืืฉื (Authorized OAuth App):
ืืฆืืจืช ืืคืืืงืฆืื ืืืฉื ื-GitHub
ืืื ืืช ืืฉืืืช ืืืชืืืืช ืืืชืจืื ืื ืืืฆืืช, ืืืืืื:
- ืืชืืืช ืืืชืจ ืฉื ืืฃ ืืืืช:
https://dex.k8s.example.com
- ืืชืืืช ืืชืจ ืืืชืงืฉืจืืช ืืืืจืช ืฉื ืืจืฉืื:
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
ืืืฆืื ืืฉืืื ืขื ืืืชืจืช 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 ืฉืื:
ืขืืื ืืชืืืจืืช
ืืฃ ืืื ืืกื ืื ืืชื ื-GitHub
ืขืงืื ืืืจ ืืืืจืืืช ืฉื ืืฆืจื ืืื ืืงืื ืืืฉื
ืืืืจ ืืขืชืงื-ืืืืงื ืืืฃ ืืืื ืืจื ื, ื ืืื ืืืฉืชืืฉ ื-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 ืืืจืืื ืฉืื ื ืืืืืื ืืจืืืช ืืฉืืืื ืืืืชืืืจ ืืคืืืื, ืืื ืืื ืืื ืืืืืืช ืืฉื ืืช ืืืชื.
ืืงืืจ: www.habr.com