$30 off During Our Annual Pro Sale. View Details »

GKE Autopilot Gatekeeper の Rego を眺めてみる

makocchi
March 08, 2021

GKE Autopilot Gatekeeper の Rego を眺めてみる

GCPUG Tokyo GKE Day March 2021の発表資料です
「GKE Autopilot Gatekeeper の Rego を眺めてみる」

makocchi

March 08, 2021
Tweet

More Decks by makocchi

Other Decks in Technology

Transcript

  1. Presented by @makocchi
    GKE Autopilot Gatekeeper の Rego を眺めてみる
    GCPUG Tokyo GKE Day March 2021

    View Slide

  2. 2
    Makoto Hasegawa
    Working at // CIU, CyberAgent, Inc
    Currently //
    Develop and maintain private OpenStack cloud.
    Develop and maintain Kubernetes as a Service platform.
    CKA / CKAD / CKS
    Job Title // Technical Lead Infrastructure Engineer
    WHO am I
    Twitter // @makocchi
    Facebook // makocchi0923
    Hobby // Playing bass

    View Slide

  3. GCPUG Tokyo GKE Day March 2021 | @makocchi 3
    💪 本日のゴール💪
    GKE Autopilot では
    どんな Rego が書かれているか知ろう
    GKE Autopilot では様々なポリシーが
    Rego として記述されています
    その中身を見ていきましょう💪

    View Slide

  4. GCPUG Tokyo GKE Day March 2021 | @makocchi 4
    そもそも Rego とは?

    View Slide

  5. GCPUG Tokyo GKE Day March 2021 | @makocchi 5
    の前に

    View Slide

  6. GCPUG Tokyo GKE Day March 2021 | @makocchi 6
    Gatekeeper とは?
    から説明していきます

    View Slide

  7. @makocchi
    GCPUG Tokyo GKE Day March 2021 7
    Gatekeeperとは?
    Open Policy Agent(OPA)を使用して Kubernetes クラスタで様々なリソースを作成および更新するリクエストを検証
    するためのアドミッションコントローラ
    似たような機能で Pod Security Policy (PSP) がありますが、PSP は Kubernetes コミュニティで廃止を進めているので今
    のうちに Gatekeeper に慣れて起きましょう
    GCP のドキュメントに記載されているのがチュートリアルも載っていてわかりやすいです 📝
    👉「Gatekeeper を使用した Pod セキュリティ ポリシーの適用」

    View Slide

  8. @makocchi
    GCPUG Tokyo GKE Day March 2021 8
    Gatekeeperとは?
    https://kubernetes.io/blog/2019/08/06/opa-gatekeeper-policy-and-governance-for-kubernetes/

    View Slide

  9. @makocchi
    GCPUG Tokyo GKE Day March 2021 9
    Gatekeeperとは?
    Gatekeeper にはクラスターを強力かつ柔軟に制御する方法を管理者に提供するための 2 つのコンセプトがあります
    制約 (Constraints)
    制約テンプレート (Constraint templates)
    どちらも Kubernetes の CRD として使用することが可能
    制約テンプレート内で記述する実際のポリシーは Rego を使って記述します
    Rego は Datalog の言語を元に作られた宣言型言語です
    JSON 等の構造化されたドキュメントを扱えるように拡張されています

    View Slide

  10. @makocchi
    GCPUG Tokyo GKE Day March 2021 10
    Rego について
    実際の Rego の書き方や読み方は公式のドキュメントが分かりやすいと思います
    https://www.openpolicyagent.org/docs/latest/policy-language/
    過去に Kubernetes Fest Tokyo 2020で発表した資料も合わ
    せて御覧ください!
    https://speakerdeck.com/makocchi/best-practice-as-
    code-with-open-policy-agent

    View Slide

  11. GCPUG Tokyo GKE Day March 2021 | @makocchi 11
    GKE Autopilot で
    定義されているポリシー(Rego)を
    見ていきましょう

    View Slide

  12. @makocchi
    GCPUG Tokyo GKE Day March 2021 12
    どんな Constraint templatesがあるのか
    $ kubectl get constrainttemplates
    NAME AGE
    autogkeallowlistedworkloadlimitation 26h
    autogkecsrlimitation 26h
    autogkegpulimitation 26h
    autogkehostnamespaceslimitation 26h
    autogkehostpathvolumes 26h
    autogkehostportlimitation 26h
    autogkelinuxcapabilitieslimitation 26h
    autogkemanagednamespaceslimitation 26h
    autogkenodeaffinityselectorlimitation 26h
    autogkenodelimitation 26h
    autogkepodaffinitylimitation 26h
    autogkepodlimitconstraints 26h
    autogkepolicycrdlimitation 26h
    autogkeprivilegedpodlimitation 26h
    autopilotexternaliplimitation 26h
    autopilotvolumetypelimitation 26h
    さっそくですが、”kubectl get constrainttemplates” で見て
    みましょう
    “autogke*” と “autopilot*” の名前で様々なテンプレートが
    定義されているのがわかると思います

    View Slide

  13. @makocchi
    GCPUG Tokyo GKE Day March 2021 13
    どんな Constraint templatesがあるのか
    さて、ここで GKE Autopilot にはいろいろな制約があるのですが、、、
    https://cloud.google.com/kubernetes-engine/docs/concepts/autopilot-overview
    hostPath に関する制約がここでは書かれています
    書き込み可能な状態で hostPath を使うことができない
    読み込み可能なのは /var/log/ 配下のみ

    View Slide

  14. @makocchi
    GCPUG Tokyo GKE Day March 2021 14
    どんな Constraint templatesがあるのか
    というわけで早速違反してみましょう👿
    ⏩ のような hostPath を “readOnly: false” にした yaml を
    適用しようとすると・・・
    apiVersion: v1
    kind: Pod
    metadata:
    name: busybox-with-hostpath-write
    spec:
    containers:
    - name: busybox
    image: busybox
    command: ["sleep", “86400"]
    volumeMounts:
    - name: log-volume
    mountPath: /host-log
    readOnly: false
    volumes:
    - name: log-volume
    hostPath:
    path: /var/log/

    View Slide

  15. @makocchi
    GCPUG Tokyo GKE Day March 2021 15
    どんな Constraint templatesがあるのか
    というわけで早速違反してみましょう👿
    ⏩ のような hostPath を “readOnly: false” にした yaml を
    適用しようとすると・・・
    ちゃんと怒られる!
    $ kubectl apply -f busybox_with_hostpath_write.yaml
    Error from server ([denied by autogke-no-write-mode-hostpath] hostPath volume log-volume in container busybox is
    accessed in write mode; disallowed in Autopilot. Requesting user: and groups:
    <["system:authenticated"]>): error when creating "busybox_with_hostpath_write.yaml": admission webhook
    "validation.gatekeeper.sh" denied the request: [denied by autogke-no-write-mode-hostpath] hostPath volume log-
    volume in container busybox is accessed in write mode; disallowed in Autopilot. Requesting user:
    and groups: <["system:authenticated"]>

    View Slide

  16. @makocchi
    GCPUG Tokyo GKE Day March 2021 16
    どんな Constraint templatesがあるのか
    いろいろエラーメッセージに出てきますが、ここで重要なのは “denied by autogke-no-write-mode-
    hostpath” という部分とその後の実際の内容です
    つまりどこかで定義されている “autogke-no-write-mode-hostpath” のポリシーによって弾かれたことになり
    ます
    $ kubectl apply -f busybox_with_hostpath_write.yaml
    Error from server ([denied by autogke-no-write-mode-hostpath] hostPath volume log-volume in container busybox is
    accessed in write mode; disallowed in Autopilot. Requesting user: and groups:
    <["system:authenticated"]>): error when creating "busybox_with_hostpath_write.yaml": admission webhook
    "validation.gatekeeper.sh" denied the request: [denied by autogke-no-write-mode-hostpath] hostPath volume log-
    volume in container busybox is accessed in write mode; disallowed in Autopilot. Requesting user:
    and groups: <["system:authenticated"]>

    View Slide

  17. @makocchi
    GCPUG Tokyo GKE Day March 2021 17
    どんな Constraint templatesがあるのか
    $ kubectl get constrainttemplates
    NAME AGE
    autogkeallowlistedworkloadlimitation 26h
    autogkecsrlimitation 26h
    autogkegpulimitation 26h
    autogkehostnamespaceslimitation 26h
    autogkehostpathvolumes 26h
    autogkehostportlimitation 26h
    autogkelinuxcapabilitieslimitation 26h
    autogkemanagednamespaceslimitation 26h
    autogkenodeaffinityselectorlimitation 26h
    autogkenodelimitation 26h
    autogkepodaffinitylimitation 26h
    autogkepodlimitconstraints 26h
    autogkepolicycrdlimitation 26h
    autogkeprivilegedpodlimitation 26h
    autopilotexternaliplimitation 26h
    autopilotvolumetypelimitation 26h
    さて、先程の Constraint templates 一覧に戻りましょう
    この中でそれっぽいやつがあるかというと・・

    View Slide

  18. @makocchi
    GCPUG Tokyo GKE Day March 2021 18
    どんな Constraint templatesがあるのか
    さて、先程の Constraint templates 一覧に戻りましょう
    この中でそれっぽいやつがあるかというと・・
    $ kubectl get constrainttemplates
    NAME AGE
    autogkeallowlistedworkloadlimitation 26h
    autogkecsrlimitation 26h
    autogkegpulimitation 26h
    autogkehostnamespaceslimitation 26h
    autogkehostpathvolumes 26h
    autogkehostportlimitation 26h
    autogkelinuxcapabilitieslimitation 26h
    autogkemanagednamespaceslimitation 26h
    autogkenodeaffinityselectorlimitation 26h
    autogkenodelimitation 26h
    autogkepodaffinitylimitation 26h
    autogkepodlimitconstraints 26h
    autogkepolicycrdlimitation 26h
    autogkeprivilegedpodlimitation 26h
    autopilotexternaliplimitation 26h
    autopilotvolumetypelimitation 26h
    これじゃね?(確信)

    View Slide

  19. @makocchi
    GCPUG Tokyo GKE Day March 2021 19
    どんな Constraint templatesがあるのか
    さっそくこの “autogkehostpathvolumes” のテンプレート
    の中身を見ていきましょう
    中身を見るには “describe” もしくは “get -o yaml” で見ると
    いいでしょう
    $ kubectl describe constrainttemplates autogkehostpathvolumes
    $ kubectl get constrainttemplates autogkehostpathvolumes -o yaml
    $ kubectl get constrainttemplates
    NAME AGE
    autogkeallowlistedworkloadlimitation 26h
    autogkecsrlimitation 26h
    autogkegpulimitation 26h
    autogkehostnamespaceslimitation 26h
    autogkehostpathvolumes 26h
    autogkehostportlimitation 26h
    autogkelinuxcapabilitieslimitation 26h
    autogkemanagednamespaceslimitation 26h
    autogkenodeaffinityselectorlimitation 26h
    autogkenodelimitation 26h
    autogkepodaffinitylimitation 26h
    autogkepodlimitconstraints 26h
    autogkepolicycrdlimitation 26h
    autogkeprivilegedpodlimitation 26h
    autopilotexternaliplimitation 26h
    autopilotvolumetypelimitation 26h

    View Slide

  20. @makocchi
    GCPUG Tokyo GKE Day March 2021
    apiVersion: templates.gatekeeper.sh/v1beta1
    kind: ConstraintTemplate
    metadata:
    name: autogkehostpathvolumes
    spec:
    targets:
    rego: |-
    violation_pod[{"msg": msg}] {
    spec := helpers.get_spec(input.review.object)
    spec["volumes"]
    hostPathVolumes := {vol.name | vol := spec.volumes[_]; vol["hostPath"]}
    containers := array.concat(object.get(spec, "initContainers", []), object.get(spec, "containers", []))
    container := containers[_]
    container["volumeMounts"]
    volumeMount := container.volumeMounts[_]
    hostPathVolume := hostPathVolumes[_]
    hostPathVolume == volumeMount.name
    not object.get(volumeMount, "readOnly", false)
    msg := sprintf("hostPath volume %s in container %s is accessed in write mode; disallowed in Autopilot.
    Requesting user: <%s> and groups: <%v>", [hostPathVolume, container.name, input.review.userInfo.username,
    input.review.userInfo.groups])
    }
    20
    どんな Constraint templatesがあるのか
    中身を見るとこんな感じです
    ※実際の中身はもっといろいろ書かれているんです
    が、書ききれないので省略して書いてあります

    View Slide

  21. @makocchi
    GCPUG Tokyo GKE Day March 2021
    apiVersion: templates.gatekeeper.sh/v1beta1
    kind: ConstraintTemplate
    metadata:
    name: autogkehostpathvolumes
    spec:
    targets:
    rego: |-
    violation_pod[{"msg": msg}] {
    spec := helpers.get_spec(input.review.object)
    spec["volumes"]
    hostPathVolumes := {vol.name | vol := spec.volumes[_]; vol["hostPath"]}
    containers := array.concat(object.get(spec, "initContainers", []), object.get(spec, "containers", []))
    container := containers[_]
    container["volumeMounts"]
    volumeMount := container.volumeMounts[_]
    hostPathVolume := hostPathVolumes[_]
    hostPathVolume == volumeMount.name
    not object.get(volumeMount, "readOnly", false)
    msg := sprintf("hostPath volume %s in container %s is accessed in write mode; disallowed in Autopilot.
    Requesting user: <%s> and groups: <%v>", [hostPathVolume, container.name, input.review.userInfo.username,
    input.review.userInfo.groups])
    }
    21
    どんな Constraint templatesがあるのか
    先程のエラーメッセージはここから生成されていることが分かります
    hostPath volume log-volume in container busybox is accessed in write mode; disallowed in
    Autopilot. Requesting user: and groups: <["system:authenticated"]>

    View Slide

  22. @makocchi
    GCPUG Tokyo GKE Day March 2021 22
    どんな Constraint templatesがあるのか
    ちなみに “readOnly: true” に変更することで先程の Pod は問題なく起動させることが可能です
    さらにちなみにどんな log が host 側の “/var/log” にあるのかというと・・・
    /host-log # ls -F
    alternatives.log cloud-init-output.log containers/ journal/ lastlog pods/ wtmp
    btmp cloud-init.log fluentbit.log kube-proxy.log metrics/ private/

    View Slide

  23. @makocchi
    GCPUG Tokyo GKE Day March 2021 23
    どんな Constraint templatesがあるのか
    各コンテナの log は containers/ 配下にきちんと履かれているようです (実際には /var/log/pods/ への symlink)
    /host-log/containers # ls
    busybox-with-hostpath-read_default_busybox-9b5e3324bac7442bc10b4f8d59a7a71f721d236b2ec5d363ea66c49355aac7d9.log
    fluentbit-gke-kfw96_kube-system_fluentbit-96c87727478cfc0810b1d152b605af4c87459b6baf00c51f8d2bdc224f0f760b.log
    fluentbit-gke-kfw96_kube-system_fluentbit-gke-e8f155cad0ea48aa7cd372ad05418bff3d8d850b8f2d46abba56ee144959527b.log
    gke-metadata-server-p957p_kube-system_gke-metadata-server-a118ef45690be5424ec82358f1882d84ffc6b2ce27e014b02bdce453887462ca.log
    gke-metrics-agent-lg9tz_kube-system_gke-metrics-agent-cfec2dd2d1d99041ba6b6765959edc032e89437389cabf6ee96bd9fc78dcff74.log
    kube-proxy-gk3-makocchi-ap-default-pool-299f5e3f-gk35_kube-system_kube-
    proxy-8b3e73275e1814174d8d3b3b488f67e2bcd8f7dd7ff5437d2fae8ae45e398613.log
    netd-2r9d6_kube-system_install-cni-bc03fc54612292f9cf45370aca2cfdc10751f3ddbcb5f9fb5bfc88ee9de5f171.log
    netd-2r9d6_kube-system_netd-59cc778b980a9169610784554350edb87114895b31702007e87a9dc25fd5eb6b.log
    node-local-dns-tgshb_kube-system_node-cache-fb941fec3f187a2ab2857236ee630ab14533e7c40cda11f6eb7dc261d4d11318.log
    pdcsi-node-px9sq_kube-system_csi-driver-registrar-c8f185e87332e9e63114cddcdc09ceb95c199df20101e373768b293d078a4d9a.log
    pdcsi-node-px9sq_kube-system_gce-pd-driver-634a0cd6a0f8c616ea9ceea6e18c5438a81ba1911880ea823347264d758a339e.log

    View Slide

  24. @makocchi
    GCPUG Tokyo GKE Day March 2021 24
    どんな Constraint templatesがあるのか
    話を戻しましょう
    先程の autogkehostpathvolumes テンプレートには他にも
    いろいろなポリシーが定義されています
    例えば先程のマニフェストを編集して “/var/log” 以外の
    hostPath を指定してみましょう
    apiVersion: v1
    kind: Pod
    metadata:
    name: busybox-with-hostpath-tmp-read
    spec:
    containers:
    - name: busybox
    image: busybox
    command: ["sleep", “86400"]
    volumeMounts:
    - name: tmp-volume
    mountPath: /host-tmp
    readOnly: true
    volumes:
    - name: tmp-volume
    hostPath:
    path: /var/tmp # /var/log ͔Βมߋ

    View Slide

  25. @makocchi
    GCPUG Tokyo GKE Day March 2021 25
    どんな Constraint templatesがあるのか
    “readOnly: false” の時とは違ったエラーメッセージが出てきますが、ポリシーは autogke-no-write-mode-
    hostpath で同じですね
    $ kubectl apply -f busybox_with_hostpath_read_tmp.yaml
    Error from server ([denied by autogke-no-write-mode-hostpath] hostPath volume tmp-volume used in container busybox
    uses path /var/tmp which is not allowed in Autopilot. Allowed path prefixes for hostPath volumes are: ["/var/
    log/"]. Requesting user: and groups: <["system:authenticated"]>): error when creating
    "busybox_with_hostpath_read_tmp.yaml": admission webhook "validation.gatekeeper.sh" denied the request: [denied by
    autogke-no-write-mode-hostpath] hostPath volume tmp-volume used in container busybox uses path /var/tmp which is
    not allowed in Autopilot. Allowed path prefixes for hostPath volumes are: ["/var/log/"]. Requesting user:
    and groups: <["system:authenticated"]>

    View Slide

  26. @makocchi
    GCPUG Tokyo GKE Day March 2021
    apiVersion: templates.gatekeeper.sh/v1beta1
    kind: ConstraintTemplate
    metadata:
    name: autogkehostpathvolumes
    spec:
    targets:
    rego: |-
    violation_pod[{"msg": msg}] {
    spec := helpers.get_spec(input.review.object)
    spec["volumes"]
    hostPathVolumes := {vol | vol := spec.volumes[_]; vol["hostPath"]}
    containers := array.concat(object.get(spec, "initContainers", []), object.get(spec, "containers", []))
    container := containers[_]
    container["volumeMounts"]
    volumeMount := container.volumeMounts[_]
    hostPathVolume := hostPathVolumes[_]
    hostPathVolume.name == volumeMount.name
    object.get(volumeMount, "readOnly", false)
    count({x | x := input.parameters.allowedPrefixes[_]; startswith(helpers.path(hostPathVolume.hostPath.path), x)}) == 0
    msg := sprintf("hostPath volume %s used in container %s uses path %s which is not allowed in Autopilot. Allowed path prefixes
    for hostPath volumes are: %v. Requesting user: <%s> and groups: <%v>", [hostPathVolume.name, container.name,
    hostPathVolume.hostPath.path, input.parameters.allowedPrefixes, input.review.userInfo.username, input.review.userInfo.groups])
    }
    26
    どんな Constraint templatesがあるのか
    例によって抜粋していますが、先程の hostPath の prefix に関する
    チェックはこの Rego で行われていますね

    View Slide

  27. @makocchi
    GCPUG Tokyo GKE Day March 2021
    apiVersion: templates.gatekeeper.sh/v1beta1
    kind: ConstraintTemplate
    metadata:
    name: autogkehostpathvolumes
    spec:
    targets:
    rego: |-
    violation_pod[{"msg": msg}] {
    spec := helpers.get_spec(input.review.object)
    spec["volumes"]
    hostPathVolumes := {vol | vol := spec.volumes[_]; vol["hostPath"]}
    containers := array.concat(object.get(spec, "initContainers", []), object.get(spec, "containers", []))
    container := containers[_]
    container["volumeMounts"]
    volumeMount := container.volumeMounts[_]
    hostPathVolume := hostPathVolumes[_]
    hostPathVolume.name == volumeMount.name
    object.get(volumeMount, "readOnly", false)
    count({x | x := input.parameters.allowedPrefixes[_]; startswith(helpers.path(hostPathVolume.hostPath.path), x)}) == 0
    msg := sprintf("hostPath volume %s used in container %s uses path %s which is not allowed in Autopilot. Allowed path prefixes
    for hostPath volumes are: %v. Requesting user: <%s> and groups: <%v>", [hostPathVolume.name, container.name,
    hostPathVolume.hostPath.path, input.parameters.allowedPrefixes, input.review.userInfo.username, input.review.userInfo.groups])
    }
    27
    どんな Constraint templatesがあるのか
    エラーメッセージを見ると ”Allowed path prefixes for hostPath volumes are: [“/var/log/“]" となっていました
    つまり、こちらの input.paramters.allowedPrefixes に /var/log が渡されているということになります

    View Slide

  28. @makocchi
    GCPUG Tokyo GKE Day March 2021 28
    どんな Constraint templatesがあるのか
    allowedPrefixes はどこから設定されているのでしょうか?
    これは autogkehostpathvolumes のテンプレートを呼び出す時にパラメーターとして渡されています
    テンプレートを適用する対象は制約 (Constraints) で定義するのですが、そこでパラメーターが指定されています
    autogkehostpathvolumes の Constraint template を定義すると、Gatekeeper によりそれに対応する Constraints
    の CRD が自動で作成されます
    ⏩ のような感じで Constraint template 内で定義されています
    これにより “AutoGKEHostPathVolumes” という CRD が作成されます
    apiVersion: templates.gatekeeper.sh/v1beta1
    kind: ConstraintTemplate
    metadata:
    name: autogkehostpathvolumes
    spec:
    crd:
    spec:
    names:
    kind: AutoGKEHostPathVolumes
    ※ 抜粋

    View Slide

  29. @makocchi
    GCPUG Tokyo GKE Day March 2021 29
    どんな Constraint があるのか
    さっそく制約(Constraint) の CRD を見てみましょう
    $ kubectl get crd | grep -i AutoGKEHostPathVolumes
    autogkehostpathvolumes.constraints.gatekeeper.sh 2021-03-06T14:09:45Z
    対応する CR も作られていることが分かります
    $ kubectl get AutoGKEHostPathVolumes
    NAME AGE
    autogke-no-write-mode-hostpath 28h
    ※ちなみに autogkehostpathvolumes 以外にもいろいろな種類の定義がされているので要チェック

    View Slide

  30. @makocchi
    GCPUG Tokyo GKE Day March 2021 30
    どんな Constraint があるのか
    例によって中身を見てみましょう (get -o yaml か describe で OK!)
    spec.parameters のところを見ると allowedPrefixes が定義されてい
    て “/var/log/“ が書かれています
    他にもパラメーターとして privilegedGroups や privilegedUsers み
    たいなものも定義されています
    apiVersion: constraints.gatekeeper.sh/v1beta1
    kind: AutoGKEHostPathVolumes
    metadata:
    name: autogke-no-write-mode-hostpath
    spec:
    ...
    parameters:
    allowedPrefixes:
    - /var/log/
    ...
    ※ 抜粋

    View Slide

  31. @makocchi
    GCPUG Tokyo GKE Day March 2021 31
    どんな Constraint があるのか
    例によって中身を見てみましょう (get -o yaml か describe で OK!)
    spec.parameters のところを見ると allowedPrefixes が定義されてい
    て “/var/log/“ が書かれています
    他にもパラメーターとして privilegedGroups や privilegedUsers み
    たいなものも定義されています
    spec.match.kinds では適用対象のリソースを設定します
    実際には Pod/Deployment/DaemonSet/ReplicaSet/ReplicationController/
    StatefulSet/Job/CronJob/PersistentVolume が kinds で指定されています
    apiVersion: constraints.gatekeeper.sh/v1beta1
    kind: AutoGKEHostPathVolumes
    metadata:
    name: autogke-no-write-mode-hostpath
    spec:
    match:
    kinds:
    - apiGroups:
    - apps
    - ""
    - batch
    apiVersions:
    - v1
    - v1beta1
    kinds:
    - Pod
    - Deployment
    ...
    ※ 抜粋

    View Slide

  32. GCPUG Tokyo GKE Day March 2021 | @makocchi 32
    Gatekeeper を通す設定は
    どこでされているのか?

    View Slide

  33. @makocchi
    GCPUG Tokyo GKE Day March 2021 33
    Gatekeeper を通す設定はどこでされているのか?
    結論から言ってしまうと ValidatingWebhookConfiguration で定義されています
    先程紹介したエラーメッセージにも書かれていましたね
    $ kubectl get ValidatingWebhookConfiguration
    NAME WEBHOOKS AGE
    gatekeeper-validating-webhook-configuration 1 29h
    nodelimit.config.common-webhooks.networking.gke.io 1 29h
    policycontroller.config.common-webhooks.networking.gke.io 1 29h
    こちらの “gatekeeper-validating-webhook-configuration” にて設定されています

    View Slide

  34. @makocchi
    GCPUG Tokyo GKE Day March 2021 34
    Gatekeeper を通す設定はどこでされているのか?
    定義の中を見ると webhook 先に “https://localhost:8787/
    v1/admit” が設定されていることが分かります
    クラスター外の endpoint になっているので Google が管理し
    ているコントロールプレーン内で kube-apiserver と一緒に
    Gatekeeper が動いているんだろうと思われます (もしかした
    ら Gatekeeper ではない可能性もありますが・・)
    apiVersion: admissionregistration.k8s.io/v1
    kind: ValidatingWebhookConfiguration
    metadata:
    name: gatekeeper-validating-webhook-configuration
    webhooks:
    - admissionReviewVersions:
    - v1beta1
    clientConfig:
    url: https://localhost:8787/v1/admit
    name: validation.gatekeeper.sh
    rules:
    ...
    ※ 抜粋

    View Slide

  35. @makocchi
    GCPUG Tokyo GKE Day March 2021 35
    Gatekeeper を通す設定はどこでされているのか?
    Webhook の rule に関しては 3 つ定義されています
    すべてのリソースの CREATE/UPDATE 及び CRD と node の DELETE の時に hook されるようですね
    rules:
    - apiGroups:
    - '*'
    apiVersions:
    - '*'
    operations:
    - CREATE
    - UPDATE
    resources:
    - '*'
    scope: '*'
    ※ 抜粋
    rules:
    - apiGroups:
    - apiextensions.k8s.io
    apiVersions:
    - v1
    operations:
    - CREATE
    - UPDATE
    - DELETE
    resources:
    - customresourcedefinitions
    scope: '*'
    rules:
    - apiGroups:
    - ""
    apiVersions:
    - v1
    operations:
    - CREATE
    - UPDATE
    - DELETE
    resources:
    - nodes
    scope: '*'

    View Slide

  36. @makocchi
    GCPUG Tokyo GKE Day March 2021 36
    Gatekeeper を通す設定はどこでされているのか?
    試しに消そうとしてみた👿
    $ kubectl delete crd autogkehostpathvolumes.constraints.gatekeeper.sh
    Error from server ([denied by autogke-policy-crd-limitation] Operations on Policy related CRDs are not allowed in
    Autopilot. Requesting user: and groups: <["system:authenticated"]>): admission webhook
    "validation.gatekeeper.sh" denied the request: [denied by autogke-policy-crd-limitation] Operations on Policy
    related CRDs are not allowed in Autopilot. Requesting user: and groups:
    <["system:authenticated"]>
    しっかりと拒否されましたね
    今度は autogke-policy-crd-limitation のポリシーが適用されたというのが分かります
    どんな Rego が書かれているかは Constraint template の autogkepolicycrdlimitation を見てみてください

    View Slide

  37. GCPUG Tokyo GKE Day March 2021 | @makocchi 37
    本日のまとめ

    View Slide

  38. GCPUG Tokyo GKE Day March 2021 | @makocchi 38
    GKE Autopilot の様々な制約は Gatekeeper によって制御されている
    GKE Autopilot 用に作られた定義は “autogke*” や “autopilot*” の Constraint
    template を見ると分かる
    Gatekeeper はクラスター内ではなく Google マネージドのコントロールプレー
    ン側で稼働していると思われる
    ひょっとしたら公式のドキュメントに書かれていない制約が Gatekeeper 側で設
    定されているかもしれない
    ポリシー自体はユーザーから操作できないので、ポリシーの更新はどうやって
    行われるのか(or 行われないのか)が気になるところ
    みんなもいろんな Rego を眺めてみましょう!
    まとめだよ

    View Slide

  39. GCPUG Tokyo GKE Day March 2021 | @makocchi 39
    達成できましたでしょうか?
    おぼえてね
    💪 本日のゴール💪
    GKE Autopilot では
    どんな Rego が書かれているか知ろう

    View Slide

  40. Presented by @makocchi
    GKE Autopilot Gatekeeper の Rego を眺めてみる
    GCPUG Tokyo GKE Day March 2021
    FINISH
    ご清聴ありがとうございました!!
    GKE Autopilot Gatekeeper の Rego を眺めてみる
    All background images in this presentation are picked from pixabay.com

    View Slide