Keycloak安装自定义插件及代码解析

参考官方开发示例,使用Java为Keycloak系统编写一套个人隐私问题的认证模块 代码编写完成后,借助Maven将项目打包为jar文件,传输至集群主节点准备部署 注意在打包时需要添加resources/META-INF/services目录,并注册Factory类 导入插件 首先创建初始ConfigMap: kubectl create configmap secret-question-plugin --from-file=SecretQuestion.jar=./kc-plugins/SecretQuestion.jar -n keycloak 然后执行kubectl edit statefulset keycloak -n keycloak对keycloak的StatefulSet进行修改: spec: template: spec: volumes: - name: plugins-volume configMap: name: secret-question-plugin containers: - name: keycloak # 其他配置... volumeMounts: - name: plugins-volume mountPath: /opt/keycloak/providers/SecretQuestion.jar subPath: SecretQuestion.jar 更新插件 之后每次更新迭代插件时,需要先删除原ConfigMap,然后创建新ConfigMap,最后重启Pod即可 # 删除旧的ConfigMap kubectl delete configmap secret-question-plugin -n keycloak # 创建新的ConfigMap kubectl create configmap secret-question-plugin --from-file=SecretQuestion.jar=./kc-plugins/SecretQuestion.jar -n keycloak # 重启Keycloak Pod kubectl delete pod keycloak-0 -n keycloak 插件代码解析 将插件导入Keycloak系统后,需要先在【身份验证】【必需的操作】处将之开启,然后将自定义的认证执行器插入流程之中,如此才能使新认证功能发挥作用 这里将新认证器放置在“账户密码验证”之后且作为必需行动 当用户首先进入认证界面(账户密码界面)时,仅账户密码认证执行器在发挥作用 ...

July 13, 2025

为K8s集群配置基于Keycloak的认证

配置证书 由于之后要将keycloak接入k8s,所以Keycloak提供的服务必须也是HTTPS的,需要先生成crt和key文件 不过之前已经配置过Dex的HTTPS,所以可以直接将dex.crt和dex.key拿来用,这里直接将两个文件设置为kubectl的secret资源: kubectl create secret generic keycloak-tls-secret --from-file=tls.crt=./dex.crt --from-file=tls.key=./dex.key -n keycloak 如此配置后,只要注明secret名称:keycloak-tls-secret,tls.crt和tls.key可以随时被Pod挂载至目录中供引用 安装、运行Keycloak 首先获取Keycloak的部署配置文件,执行: wget https://gh-proxy.com/raw.githubusercontent.com/keycloak/keycloak-quickstarts/refs/heads/main/kubernetes/keycloak.yaml 接着对文件作出修改: 使用volumes和volumeMounts将keycloak-tls-secret挂载至容器目录/etc/x509/https volumes: - name: keycloak-tls secret: secretName: keycloak-tls-secret ... volumeMounts: - name: keycloak-tls mountPath: /etc/x509/https readOnly: true 设置环境变量,开启HTTPS并设置端口8443,引用挂载的证书和秘钥文件 env: - name: KC_HTTPS_CERTIFICATE_FILE value: /etc/x509/https/tls.crt - name: KC_HTTPS_CERTIFICATE_KEY_FILE value: /etc/x509/https/tls.key - name: KC_HTTPS_PORT value: "8443" - name: KC_HTTP_ENABLED value: "false" ... ports: - name: https containerPort: 8443 设置NodePort将服务暴露,可以映射为:8443 -> 30443 apiVersion: v1 kind: Service metadata: name: keycloak labels: app: keycloak spec: ports: - protocol: TCP port: 8443 targetPort: 8443 name: https nodePort: 30443 selector: app: keycloak type: NodePort 最终的keycloak.yaml文件: ...

June 10, 2025

在K8s集群部署Dex认证

Dex是一个开源的第三方身份认证系统,简化了与已有身份提供者或其他认证服务进行认证的开发流程,Dex将检查身份的过程对项目开发者隐藏,使得开发者只需要关注、控制认证业务进行的客体,无需亲自管理身份认证的各项细节 环境 OS:Debian-12.10.0-amd64 Kubernetes:v1.28.0 kubectl:v1.28.2 Helm:v3.17.3 master机已安装Git,已部署LDAP服务器 开发者没有备案的域名,无法进行DNS A解析,只能使用自签名证书 获取dex-k8s-authenticator Dex本身是可以直接使用的,但是如果想要将Dex部署至K8s集群中并提供友好的可视页面,则需要部署dex-k8s-authenticator(之后简称DKA) 执行:git clone https://github.com/mintel/dex-k8s-authenticator.git克隆该Git仓库,仓库的charts/路径中已经存在Dex和DKA的Chart文件,因此无需再单独克隆获取Dex文件 ficn@master:~$ git clone https://github.com/mintel/dex-k8s-authenticator.git ficn@master:~$ cd dex-k8s-authenticator/ ficn@master:~/dex-k8s-authenticator$ ls charts/ dex dex-k8s-authenticator README.md 运行Dex和DKA 执行: helm inspect values charts/dex > dex-values.yaml helm inspect values charts/dex-k8s-authenticator > dka-values.yaml 这两个指令目的是根据Dex和DKA的原始Chart生成各自的新values文件,用于之后覆盖原配置 接下来就对这两个values文件进行修改 Dex # sudo vi dex-values.yaml # Default values for dex # Deploy environment label, e.g. dev, test, prod global: deployEnv: dev replicaCount: 1 image: repository: dexidp/dex tag: v2.37.0 pullPolicy: IfNotPresent env: - name: KUBERNETES_POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace service: type: NodePort port: 5556 nodePort: 30000 # For nodeport, specify the following: # type: NodePort # nodePort: <port-number> tls: create: false ingress: enabled: false annotations: {} # kubernetes.io/ingress.class: nginx # kubernetes.io/tls-acme: "true" path: / hosts: - dex.example.com tls: [] # - secretName: dex.example.com # hosts: # - dex.example.com rbac: # Specifies whether RBAC resources should be created create: true serviceAccount: # Specifies whether a ServiceAccount should be created create: true # The name of the ServiceAccount to use. # If not set and create is true, a name is generated using the fullname template name: resources: {} nodeSelector: {} tolerations: [] affinity: {} # Configuration file for Dex # Certainly secret fields can use environment variables # config: |- issuer: http://192.168.92.128:30000 storage: type: kubernetes config: inCluster: true web: http: 0.0.0.0:5556 # If enabled, be sure to configure tls settings above, or use a tool # such as let-encrypt to manage the certs. # Currently this chart does not support both http and https, and the port # is fixed to 5556 # # https: 0.0.0.0:5556 # tlsCert: /etc/dex/tls/tls.crt # tlsKey: /etc/dex/tls/tls.key 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 # Remember you can have multiple connectors of the same 'type' (with different 'id's) # If you need e.g. logins with groups for two different Microsoft 'tenants' connectors: # These may not match the schema used by your LDAP server # https://github.com/coreos/dex/blob/master/Documentation/connectors/ldap.md - type: ldap id: ldap name: LDAP config: host: 192.168.92.128:389 insecureNoSSL: true startTLS: false bindDN: cn=admin,dc=example,dc=com bindPW: "647252" userSearch: # Query should be "(&(objectClass=inetorgperson)(cn=<username>))" baseDN: ou=People,dc=example,dc=com filter: "(objectClass=inetorgperson)" username: cn # DN must be in capitals idAttr: DN emailAttr: mail nameAttr: cn preferredUsernameAttr: cn groupSearch: # Query should be "(&(objectClass=groupOfUniqueNames)(uniqueMember=<userAttr>))" baseDN: ou=Group,dc=example,dc=com filter: "" # DN must be in capitals userAttr: DN groupAttr: member nameAttr: cn # The 'name' must match the k8s API server's 'oidc-client-id' staticClients: - id: my-cluster name: "my-cluster" secret: "pUBnBOY80SnXgjibTYM9ZWNzY2xreNGQok" redirectURIs: - http://192.168.92.128:30001/callback enablePasswordDB: True staticPasswords: - email: "[email protected]" # bcrypt hash of the string "password" hash: "$2a$10$2b2cU8CPhOTaGrs1HRQuAueS7JTT5ZHsHSzYiFPm1leZck7Mc8T4W" username: "admin" userID: "08a8684b-db88-4b73-90a9-3cd1661f5466" # You should not enter your secrets here if this file will be stored in source control # Instead create a separate file to hold or override these values # You need only list the environment variables you used in the 'config' above # You can add any additional ones you need, or remove ones you don't need # envSecrets: # GitHub GITHUB_CLIENT_ID: "override-me" GITHUB_CLIENT_SECRET: "override-me" # Google (oidc) GOOGLE_CLIENT_ID: "override-me" GOOGLE_CLIENT_SECRET: "override-me" # Microsoft MICROSOFT_APPLICATION_ID: "override-me" MICROSOFT_CLIENT_SECRET: "override-me" # LDAP LDAP_BINDPW: "123456" 如上所示: ...

May 23, 2025

测试K8s集群内部联通性

设置集群时,有时需要测试集群内部Pod之间的连通性,或是测试DNS服务是否正常 可以使用kubectl run命令来创建一个Pod,并指定Pod名称、镜像以及命名空间 例如,创建一个busybox镜像,目的是测试kube-authen命名空间下的Pod是否能ping通、DNS服务是否生效: kubectl run ping-test --image=busybox -n kube-authen -it -- sh 以上命令的含义是:在kube-authen命名空间内创建一个镜像为busybox的Pod,Pod名称为ping-test,创建后立刻打开一个交互式终端 在终端内即可进行ping测试或是nslookup测试 有时仅需要进行临时测试,测试完毕后需要立刻删除Pod,那么可以执行指令: kubectl run ping-test --image=busybox -n kube-authen -it --rm -- ping google.com 这里的--rm参数意为“会话结束后自动删除Pod” 注意 如果仅创建了busybox的Pod而没有指定运行命令,例如kubectl run ping-test --image=busybox -n kube-authen,则会导致Pod无限重启,原因是: 在未指定命令的情况下,busybox使用默认入口点,默认行为是启动shell,但启动shell后没有可执行的命令,于是shell自动退出,这表现为容器进程退出,K8s即认为容器启动失败并进行自动重启,如此不断循环,产生CrashLoopBackOff错误

April 16, 2025

将Go服务部署至K8s提供Webhook认证

以之前构建完成的Webhook工程为例,将此Go应用部署至k8s中并指定为认证服务器 构建镜像 在工程根目录创建Dockerfile: FROM alpine:3.17 WORKDIR /app # 复制已编译好的二进制文件(此处hook-demo为已构建的Go应用) COPY hook-demo /app/ # 设置可执行权限 RUN chmod +x /app/hook-demo # 暴露服务端口 EXPOSE 9999 # 运行应用 CMD ["/app/hook-demo"] 在Dockerfile所在目录中,运行sudo docker build -t webhook-auth:v1.0 . 完成后检查: ficn@master:~/k8s-webhook-auth$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE webhook-auth v1.0 536c17b2a66c 13 seconds ago 31.8MB alpine 3.17 775f483016a7 7 months ago 7.08MB Pod与Service部署 创建deploy-webhook.yaml: # ~/deploy/deploy-webhook.yaml apiVersion: apps/v1 kind: Deployment metadata: name: webhook-auth namespace: kube-authen # 命名空间 labels: app: webhook-auth spec: replicas: 1 # 本地开发环境,使用单个副本 selector: matchLabels: app: webhook-auth template: metadata: labels: app: webhook-auth spec: containers: - name: webhook-auth image: webhook-auth:v1.0 # 本地镜像名称 imagePullPolicy: Never # 强制使用本地镜像,不尝试从远程拉取 ports: - containerPort: 9999 # webhook实际端口 name: http 创建service-webhook.yaml: ...

April 10, 2025