在 Ingress 上启用 IAP

Dja*_*abx 3 kubernetes google-kubernetes-engine google-iap

我已按照有关如何在 GKE 上启用 IAP 的文档进行操作。

我有:

  1. 配置同意屏幕
  2. 创建 OAuth 凭据
  3. 添加通用重定向 URL
  4. 将我自己添加为IAP-secured Web App User

并像这样编写我的部署:

data:
  client_id: <my_id>
  client_secret: <my_secret>
kind: Secret
metadata:
  name: backend-iap-secret
type: Opaque
---
apiVersion: v1
kind: Service
metadata:
  name: grafana
spec:
  ports:
  - port: 443
    protocol: TCP
    targetPort: 3000
  selector:
    k8s-app: grafana
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: grafana
spec:
  replicas: 1
  template:
    metadata:
      labels:
        k8s-app: grafana
    spec:
      containers:
      - env:
        - name: GF_SERVER_HTTP_PORT
          value: "3000"
        image: docker.io/grafana/grafana:6.7.1
        name: grafana
        ports:
        - containerPort: 3000
          protocol: TCP
        readinessProbe:
          httpGet:
            path: /api/health
            port: 3000
---
apiVersion: cloud.google.com/v1beta1
kind: BackendConfig
metadata:
  name: backend-config-iap
spec:
  iap:
    enabled: true
    oauthclientCredentials:
      secretName: backend-iap-secret
---
apiVersion: networking.gke.io/v1beta1
kind: ManagedCertificate
metadata:
  name: monitoring-tls
spec:
  domains:
  - monitoring.foo.com
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    beta.cloud.google.com/backend-config: '{"default": "backend-config-iap"}'
    kubernetes.io/ingress.global-static-ip-name: monitoring
    networking.gke.io/managed-certificates: monitoring-tls
  name: grafana
spec:
  backend:
    serviceName: grafana
    servicePort: 443

Run Code Online (Sandbox Code Playgroud)

当我查看我的入口时,我看到了:

$ k describe ingress
Name:             grafana
[...]
Annotations:  beta.cloud.google.com/backend-config: {"default": "backend-config-iap"}
              ingress.kubernetes.io/backends: {"k8s-blabla":"HEALTHY"}
              [...] 
Events:       <none>
$
Run Code Online (Sandbox Code Playgroud)

我可以毫无问题地连接到网页,grafana 已启动并运行,但我也可以在未经身份验证的情况下连接(女巫是一个问题)。

所以一切看起来都很好,但是 IAP 没有激活,为什么?

最糟糕的是,如果我手动启用它,它可以工作,但如果我重做,kubectl apply -f monitoring.yamlIAP 就会被禁用。

我缺少什么?


因为我的秘密值存储在秘密管理器中(并在构建时检索),所以我怀疑我的秘密中存在一些故障(空格、\n 等),因此我添加了一个脚本来测试它:

gcloud compute backend-services update \
  --project=<my_project_id> \
  --global \
  $(kubectl get ingress grafana -o json | jq -r '.metadata.annotations."ingress.kubernetes.io/backends"' | jq -r 'keys[0]') \
  --iap=enabled,oauth2-client-id=$(gcloud --project="<my_project_id>" beta secrets versions access latest --secret=Monitoring_client_id),oauth2-client-secret=$(gcloud --project="<my_project_id>" beta secrets versions access latest --secret=Monitoring_secret)
Run Code Online (Sandbox Code Playgroud)

现在 IAP 已使用正确的 OAuth 客户端正确启用,因此我的秘密是“干净的”

顺便说一句,我还尝试重命名这样的秘密变量(来自 client_id): * oauth_client_id * oauth-client-id * clientID (如后端文档中所示

我还在后端写了这样的值:

kind: BackendConfig
metadata:
  name: backend-config-iap
spec:
  iap:
    enabled: true
    oauthclientCredentials:
      secretName: backend-iap-secret
      clientID: <value>
      clientSecret: <value>
Run Code Online (Sandbox Code Playgroud)

但也不起作用。


勘误

事实上,当我再次部署时(在 Web UI 中启用它之后),IAP 被销毁,这是我在此测试中的部署脚本的一部分(我之前做了 kubectl 删除)。

但尽管如此,我无法仅通过后端配置启用 IAP。


正如建议的那样,我已经提交了错误报告:https ://issuetracker.google.com/issues/153475658


图腾给出的解决方案

更改给定的 yaml 如下:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.global-static-ip-name: monitoring
    networking.gke.io/managed-certificates: monitoring-tls
  name: grafana
[...]
---
apiVersion: v1
kind: Service
metadata:
  name: grafana
  annotations:
    beta.cloud.google.com/backend-config: '{"default": "backend-config-iap"}'
[...]
Run Code Online (Sandbox Code Playgroud)

后端与服务相关联,而不是与 Ingress 相关联...现在可以使用了!

Tot*_*tem 6

您所做的一切都是正确的,只是一个小更改:应该在服务资源上添加注释

apiVersion: v1
kind: Service
metadata:
  annotations:
    beta.cloud.google.com/backend-config: '{"ports": { "443":"backend-config-iap"}}'
  name: grafana
Run Code Online (Sandbox Code Playgroud)

通常您需要将其与端口关联,因此我在上面添加了此示例,但请确保它按预期与 443 配合使用。

这是基于我使用的内部示例:

beta.cloud.google.com/backend-config: '{"ports": { "3000":"be-cfg}}'
Run Code Online (Sandbox Code Playgroud)