Share
Embed
PodSecurityPolicyの廃止に備えて、 一足先にPodSecurity Admissionを試してみよう! / from-psp-to-podsecurity
uesyn
August 26, 2021
Transcript
39 slides
-
Kubernetes Meetup Tokyo #44 (2021/8/26 ) Shinya Uemura @uesyn PodSecurityPolicyͷഇࢭʹඋ͑ͯɺ
ҰઌʹPodSecurity AdmissionΛࢼͯ͠ΈΑ͏! -
Shinya Uemura / @uesy n ▶ θοτϥϘגࣜձࣾ ιϑτΣΞΤϯδχΞ ▶ ҎԼͷίϛϡχςΟͷӡӦͯ͠·͢
+ Prometheus Meetup Toky o + Cloud Native Meetup Toky o + Kubernetes มߋ༰ڞ༗ձ ▶ དྷि9/2()ʹKubernetes v1.22 มߋ༰ڞ༗ձΛ։࠵͢ΔͷͰͥͻࢀՃ͍ͩ͘͞ʂ + https://kubernetes-updates.connpass.com/event/222915/ -
θοτϥϘגࣜձࣾ / Z Lab Corporatio n ▶ 2015ʹઃཱ͞ΕͨϠϑʔגࣜձࣾͷ100%ࢠձࣾ ▶ Πϯϑϥج൫ٕज़ͷௐࠪɾݚڀ։ൃ
▶ Ϡϑʔגࣜձ͚ࣾͷϚωʔδυ Kubernetes αʔϏεͷ։ൃ ▶ https://zlab.co.jp/ -
ΞδΣϯμ 1. PodSecurityPolicy͕क͍ͬͯͨͷ 2. PodSecurityPolicyͷ͓͞Β͍ 3. PodSecurityPolicyͷͱഇࢭ 4. PodSecurityͱʁ 5.
PodSecurityΛಈ͔ͯ͠ΈΑ͏ʂ 6. PodSecurityͰࠔΓͦ͏ͳͱ͜Ζ 7. ·ͱΊ -
ͦͦPodSecurityPolicyͰक͍ͬͯͨͷʁ(2/2) ▶ PodͷspecΛߟྀ͠ͳ͍ͱෆ߹ͳύλʔϯ͕͋Δ + ྫ͑Ϋϥελͷཧऀͱར༻ऀ͕ҧ͏߹Λߟ͑Δͱ … + ίϯςφ͕ಈ࡞͢ΔϊʔυͷrootݖݶͱಉͷcapabilityͷPodͷ࡞ΛڐՄͯ͠Α͍ʁ + ίϯςφ͕Host
networkͷଓΛڐՄͯ͠Α͍ʁ + ίϯςφ͕ϗετͷϘϦϡʔϜΛͳΜͰϚϯτͯ͠ྑ͍͔ʁ Etc … ▶ Podͷ࡞ͷೝՄ͚ͩͰݖݶঢ͕֨ՄೳͳͨΊPodͷϙϦγʔ੍ޚॏཁ + ੍ݶͳ͠ͷPodϊʔυͷroot(݅࣍ୈͰcluster-admin)ͱಉ + ࢀߟ) Securing Clusters with Kubernetes Extensibilit y + https://speakerdeck.com/ladicle/securing-clusters-with-kubernetes-extensibility?slide=27 ▶ Pod͕ཁٻ͢ΔݖݶͰ࡞ΛڐՄ͢Δ͔Ͳ͏੍͔ޚͨ͘͠ͳΔͣ => PodSecurityPolicyͳΒͰ͖·͢ʂ -
PodSecurityPolicyͱʁ ▶ Admission ControlͰPodͷηΩϡϦςΟϙϦγʔΛࡉ੍͔͘ޚ͢Δ + privilegedͳίϯςφɺར༻ՄೳͳvolumeωοτϫʔΫɺread onlyͳrootϑΝΠϧγεςϜͷڧ੍ etc … +
σϑΥϧτແޮ ▶ PodSecurityPolicyϦιʔεͰϙϦγʔΛఆٛ͢Δ + PodSecurityPolicyϦιʔεverbͰuse͕ೝՄ͞Ε͍ͯΕɺͦΕΛຬͨ͢Pod͕࡞Ͱ͖Δ ▶ ڐՄɾڋ൱͚ͩͰͳ͘ɺϙϦγʔͰఆٛ͞ΕͨͷೖͳͲ͢Δ(capabilityͳͲ) RBAC͜͜ PSP͜͜ ը૾: https://kubernetes.io/blog/2019/03/21/a-guide-to-kubernetes-admission-controllers/ -
PodSecurityPolicyͷഇࢭʹ͍ͭͯ ▶ ͦΜͳPSP͕Kubernetes v1.21Ͱඇਪ, v1.25Ͱഇࢭ༧ఆͱͳ͍ͬͯ·͢ ▶ ഇࢭͷܦҢͷৄࡉҎԼͷϦϯΫΛࢀর͍ͩ͘͞ + KEP-2579: Pod
Security Admission Control - Motivatio n + https://github.com/kubernetes/enhancements/tree/master/keps/sig-auth/2579-psp-replacement#motivation + PodSecurityPolicy Deprecation: Past, Present, and Futur e + https://kubernetes.io/blog/2021/04/06/podsecuritypolicy-deprecation-past-present-and-future/#why-is-podsecuritypolicy-going-away It is easy to accidentally grant broader permissions than intended, and dif fi cult to inspect which PSP(s) apply in a given situation. The “changing Pod defaults” feature can be handy, but is only supported for certain Pod settings and it’s not obvious when they will or will not apply to your Pod . Without a “dry run” or audit mode, it’s impractical to retro fi t PSP to existing clusters safely, and it’s impossible for PSP to ever be enabled by default. [PodSecurityPolicy Deprecation: Past, Present, and FutureΑΓҾ༻] -
ᶃҙਤ͠ͳ͍ݖݶͷར༻(1/2) ▶ ୭ʹͱͬͯར༻ՄೳͳPSP͕Podద༻Մೳͳͷ͔ʁ + Pod࡞ͷϦΫΤετΛ͛ͨϢʔβ(·ͨServiceAccount ) + Podͷ .spec.serviceAccountName Ͱࢦఆ͞ΕͨServiceAccount
User: human Request apiVersion: v1 kind: Pod metadata: name: test spec: containers: - image: test name: i-need-privilege securityContext: privileged: true Create PSPͰPrivilegedͳίϯςφ͕࡞Εͳ͍ -
ᶃҙਤ͠ͳ͍ݖݶͷར༻(2/2) ▶ ୭ʹͱͬͯར༻ՄೳͳPSP͕Podద༻Մೳͳͷ͔ʁ + Pod࡞ͷϦΫΤετΛ͛ͨϢʔβ(·ͨServiceAccount ) + Podͷ .spec.serviceAccountName Ͱࢦఆ͞ΕͨServiceAccount
ServiceAccount: robot User: human Request apiVersion: v1 kind: Pod metadata: name: test spec: serviceAccountName: robot containers: - image: test name: i-need-privilege securityContext: privileged: true Create PSPͰPrivilegedͳίϯςφ࡞ΕΔ! PSPͰPrivilegedͳίϯςφ͕࡞Εͳ͍ Ϣʔβͷ༩͑ΒΕͨݖݶΛ ͑ͨૢ࡞͕ग़དྷͯ͠·͏!! ͲͪΒ͔͕ PrivilegedͳίϯςφΛ ىಈͰ͖Εྑ͍ -
ᶄ୭ͷར༻ՄೳͳPSP͕ద༻͞ΕΔ͔Θ͔ΓͮΒ͍ʂ ▶ ReplicaSetStatefulSetͳͲΛ࡞͢Δ߹ … + PodΛ࡞͢Δͷkube-controller-manager(Ͱར༻͞Ε͍ͯΔίϯτϩʔϥͷServiceAccount ) + ͦͷͨΊେݩͷϦΫΤετͷϢʔβ(·ͨServiceAccount)͕ར༻Ͱ͖ΔPSPؔͳ͍ ServiceAccount:
robot User: human Request StatefulSetΛ࡞͚ͬͨͩ… kube-controller-manager apiVersion: v1 kind: Pod metadata: name: test spec: serviceAccountName: robot containers: - image: test name: i-need-privilege securityContext: privileged: true Watch Create -
ᶅͲͷPSP͕ద༻͞ΕΔ͔Θ͔ΓͮΒ͍ʂ ▶ ͋ΔϢʔβ/ServiceAccountʹར༻ՄೳͳPSPϦιʔε͕ෳଘࡏͨ͠߹Λߟ͑Δ + ىಈ͍ͨ͠Pod͕શͯͷϙϦγʔΛຬͨ͢߹ͲΕ͕ར༻͞Ε͔ඇৗʹΘ͔ΓͮΒ͍ + MutatingΛ͢Δɾ͠ͳ͍ͷҧ͍͕͋Δ߹ɺҙਤ͠ͳ͍ݖݶͷPod͕ੜ͞ΕΔՄೳੑ͕͋Δ User: human Request
Create 2ͭͷPSP͕ར༻Ͱ͖Δ ᶃ seccompͷσϑΥϧτ: runtimeDefault ᶄ seccompͷσϑΥϧτ: ࢦఆͳ͠ seccomp = runtimeDefault? Null? -
ᶆσϑΥϧτͷϙϦγʔ͕༻ҙ͞Ε͍ͯͳ͍ ▶ PSPʹσϑΥϧτͷϙϦγʔ͕༻ҙ͞Ε͍ͯͳ͍ + ϙϦγʔΛݸผʹ࡞͍ͯ͘͠ඞཁ͕͋Δ ▶ PSPΛ༗ޮԽͯ͠ར༻ՄೳͳPSPϦιʔε͕ͳ͚ΕPod͕ىಈͰ͖ͳ͍ + PSPΛॳΊͯ༗ޮԽ͢Δ࣌ҙ͢Δඞཁ ▶
ٖࣅతʹσϑΥϧτͷPSPΛ༻ҙ͢Δ͜ͱՄೳ + system:authenticatedάϧʔϓʹରͯ͠ར༻ՄೳͳPSPΛઃఆ͢Δ + (system:authenticated: શͯͷೝূࡁΈϢʔβɾServiceAccount͕ॴଐ͢Δάϧʔϓ) -
PodSecurityͷొ ͜͜Ͱ࣮ߦ͞ΕΔ PSPͱҟͳΓMutating͠ͳ͍ʂ ը૾: https://kubernetes.io/blog/2019/03/21/a-guide-to-kubernetes-admission-controllers/ ▶ Kubernetes v1.22͔Βalphaػೳͱͯ͠ొʂ (ॱௐʹ͍͚v1.23Ͱbeta )
+ ݱࡏFeatureGatesͰ໌ࣔతʹઃఆ͢Δ͜ͱͰར༻Ͱ͖Δ ▶ Pod Security StandardsΛద༻͢ΔAdmission Contro l + Podʹର͢ΔηΩϡϦςΟϓϩϑΝΠϧͷఆٛ(privileged, baseline, restricted ) + ref: https://kubernetes.io/docs/concepts/security/pod-security-standards/ ▶ ཧNamespace୯Ґ + PSPͷΑ͏ʹϢʔβ/ServiceAccountͰͳ͍ -
PodSecurityԿΛͯ͘͠ΕΔʁ ▶ Pod Security StandardsͷϙϦγʔΛPodద༻͢Δ + privileged, baseline, restricte d
▶ 3ͭͷϞʔυͦΕͧΕʹରͯ͠Pod Security StandardsͷϙϦγʔΛద༻͢Δ + enforce: ҧͨ͠Podͷ࡞Λڋ൱͢Δ + audit: ҧͨ͠Βaudit logͷΞϊςʔγϣϯͱͯ͠ه͢Δ (࡞ͷڋ൱͞Εͳ͍ ) + warn: ҧͨ͠Βwarningͱͯ͠දࣔ͢Δ (࡞ͷڋ൱͞Εͳ͍ ) ▶ ྫ) enforceͱaudit͕privileged, warn͕restrictedΛࢦఆ + => Podͷ࡞ʹ੍ݶͳ͍͕ɺrestrictedΛຬͨ͞ͳ͍߹ʹwarning͕දࣔ͞ΕΔ -
PodSecurityͷઃఆํ๏(1/2) ▶ PSPͱҟͳΓઐ༻ͷϦιʔεͳ͍ ▶ NamespaceͷϥϕϧͰઃఆΛߦ͏ + pod-security.kubernetes.io/xxx ͱ͍͏ϥϕϧ + NamespaceͷશͯͷPodϙϦγʔ͕ద༻͞ΕΔ
+ PSPͷ߹Ϣʔβ/ServiceAccount͕ར༻ՄೳͳϙϦγʔͷதͰىಈͰ͖Δͷ͕બ͞Εͨ apiVersion: v1 kind: Namespace metadata: labels: pod-security.kubernetes.io/enforce: privileged pod-security.kubernetes.io/audit: privileged pod-security.kubernetes.io/warn: baseline name: test -
PodSecurityͷઃఆํ๏(2/2) ▶ PodSecurityʹόʔδϣϯ͕͋Δ + Կࢦఆ͠ͳ͍߹latest͕ར༻͞ΕΔ apiVersion: v1 kind: Namespace metadata:
labels: pod-security.kubernetes.io/enforce: privileged pod-security.kubernetes.io/enforce-version: v1.22 name: test -
PodSecurityͷσϑΥϧτ ▶ PSPͱҟͳΓσϑΥϧτͷઃఆ͕͋Δ + enforce, audit, warnશͯprivileged (ͭ·ΓԿ੍ݶ͠ͳ͍ ) +
όʔδϣϯlatest ▶ “Կ੍ݶ͠ͳ͍”ͨΊPodSecurityσϑΥϧτ༗ޮͳAdmission ControlʹͳΕΔ apiVersion: v1 kind: Namespace metadata: labels: pod-security.kubernetes.io/enforce: privileged pod-security.kubernetes.io/audit: privileged pod-security.kubernetes.io/warn: privileged pod-security.kubernetes.io/enforce-version: latest pod-security.kubernetes.io/audit-version: latest pod-security.kubernetes.io/warn-version: latest name: test apiVersion: v1 kind: Namespace metadata: name: test -
PodSecurityͷର֎σϑΥϧτͷมߋ(1/2) ▶ ҎԼͷΛkube-apiserverઃఆ͢Δ͜ͱͰมߋՄೳ + enforce/audit/warnͷσϑΥϧτͷϙϦγʔ + enforce/audit/warnͷσϑΥϧτͷόʔδϣϯ + PodSecurityͷ੍ݶΛ͔͚ͳ͍Ϣʔβ/ServiceAccoun t
+ PodSecurityͷ੍ݶΛ͔͚ͳ͍Namespac e + PodSecurityͷ੍ݶΛ͔͚ͳ͍RuntimeClass -
PodSecurityͷର֎σϑΥϧτͷมߋ(2/2) apiVersion: apiserver.config.k8s.io/v1 kind: AdmissionConfiguration plugins: - name: PodSecurity configuration:
apiVersion: pod-security.admission.config.k8s.io/v1alpha1 kind: PodSecurityConfiguration defaults: enforce: baseline enforce-version: latest audit: baseline audit-version: latest warn: baseline warn-version: latest exemptions: usernames: - system:serviceaccount:test-ps:test-ps runtimeClasses: - kataContainer namespaces: - kube-system ▶ ઃఆྫ + શͯͷϙϦγʔΛσϑΥϧτΛbaseline + ϙϦγʔద༻ͷର֎Λࢦఆ + kube-system namespac e + test-ps namespaceͷtest-ps ServiceAccoun t + Runtime class໊͕kataContainer -
v1.22ΑΓલͷΫϥελͰPodSecurityΛಈ͔͍ͨ͠ʂ ▶ PodSecuritystandaloneͳWebhookʹΑΔ࣮ਐΊΒΕ͍ͯΔ༷ࢠ + KEP-2579: Pod Security Admission ControlͷFlexible Extension
Suppor t + https://github.com/kubernetes/enhancements/tree/master/keps/sig-auth/2579-psp-replacement# fl exible-extension-support + Podsecurity webhook #10346 5 + https://github.com/kubernetes/kubernetes/pull/103465 -
MutatingΛͯ͘͠Εͳ͍ ▶ PSPͰseccompcapabilityͷσϑΥϧτΛMutatingʹΑΓ࣮ݱ͍ͯͨ͠ ▶ PodSecurityͰ໌ࣔతʹઃఆ͢Δඞཁ͕͋Δ + ྫ͑baselineͰseccompͰnullೝΊΒΕ͍ͯΔ + PodSecurityPolicy͔ΒҠߦͷࡍҙ͕ඞཁ ▶
seccompʹݶͬͯݴ͑σϑΥϧτΛೖ͢Δઐ༻ͷػೳ͕͋Δ + v1.22͔Βalphaͱͯ͠ఏڙ͞ΕFeature GateͰ༗ޮԽ͢Δͱར༻Մೳ + Enable seccomp for all workloads with a new v1.22 alpha featur e + https://kubernetes.io/blog/2021/08/25/seccomp-default/ -
Namespaceͷϥϕϧฤूݖݶͷѻ͍ ▶ PSPͰPSPϦιʔεͷuseΛRBACͳͲͷೝՄͰ੍ޚ ▶ PodSecurityͰNamespaceͷฤूݖݶ͕Pod࡞ͷೝՄʹӨڹ + Ϋϥελཧऀͱར༻ऀ͕͔Ε͍ͯΔ߹Λߟ͑Δͱ … + Namespaceͷϥϕϧฤूݖݶ
= ࡞Ͱ͖ΔPodͷݖݶঢ͕֨Մೳ + ্هΛ੍ݶ͢ΔValidatingWebhookMutatingWebhookΛ࣮͠ͳ͍ͱ͍͚ͳ͍͔… -
PodSecurityͷࣗ༝ʹ͍ͭͯ ▶ PSPͰϢʔβ͕ϙϦγʔͷఆٛΛ͢Δඞཁ͕͋ͬͨͷͷࣗ༝͕͋ͬͨ ▶ PodSecurityͰPrivileged, Baseline, Restrictedͷ3͔ͭ͠બࢶ͕ͳ͍ + Baseline +
ಛఆͷhostPath͚ͩڐՄ Έ͍ͨͳ͜ͱͰ͖ͳ͍ ▶ ಠࣗͷϙϦγʔWebhookͰ࣮͠ͳ͚ΕͳΒͳ͍͔ʁ + KEP-2579: Pod Security Admission ControlͷCustom Pro fi le s + https://github.com/kubernetes/enhancements/tree/master/keps/sig-auth/2579-psp-replacement#custom-pro fi les + ͔͠͠betaͰCustom Pro fi lesແ͘ͳΓͦ͏ … + https://github.com/kubernetes/enhancements/pull/2895ɹ(2021/8/25࣌Ͱ·ͩmerge͞Ε͍ͯͳ͍ ) + ͦ͏ͳΔͱStandaloneͷPodSecurityΛFork࣮ͯ͢͠Δ͔͠ͳ͍…? -
PSP͔ΒPodSecurityͷҠߦ ▶ Ҡߦʹؔ͢Δެࣜϖʔδ͕͋Δ + Migrate from PodSecurityPolicy to the Built-In
PodSecurity Admission Controlle r + https://kubernetes.io/docs/tasks/con fi gure-pod-container/migrate-from-psp / ▶ ஈ֊తʹ༷ࢠݟ͠ͳ͕ΒҠߦ͠·͠ΐ͏ͱ͍͏༰ + PSPͷMutatingͷػೳΛར༻͍ͯ͠ΔͱҠߦ͕গ͠େม͔͠Εͳ͍ ▶ ҠߦͷͨΊͷπʔϧ͕༻ҙ͞ΕΔ͔ʁ + ࢀߟ) https://github.com/kubernetes/enhancements/pull/2895 -
PodSecurityPolicyͱPodSecurityͷҧ͍ PodSecurityPolicy PodSecurity σϑΥϧτ༗ޮ? ແޮ ༗ޮ (*v1.22࣌ͰalphaͷͨΊFeatureGateͷઃఆ͕ඞཁ) ϙϦγʔద༻ͷઃఆ PodSecurityPolicyϦιʔεͱRBAC Namespaceͷϥϕϧ
ͲͷϙϦγʔ͕ద༻͞ΕΔʁ ϦΫΤετϢʔβ/ServiceAccount ·ͨ.spec.serviceAccountNameͰࢦఆ͞ΕͨServiceAccount ͕ར༻Ͱ͖ΔϙϦγʔ NamespaceͰઃఆ͞ΕͨϙϦγʔ ΧελϜϙϦγʔͷ࣮ Մೳ (ϙϦγʔશͯఆٛ͢Δඞཁ͕͋Δ) ෆՄೳ (fork࣮ͯ͢͠ΕͰ͖ͦ͏) dry-runͷ࣮ߦ ෆՄ Մ Ͳ͕ͬͪ৽͍͠ʁ چ ৽ Ͳ͕ͬͪෳࡶʁ ෳࡶ γϯϓϧ -
(એ) Kubernetes v1.22 มߋ༰ڞ༗ձΛ։࠵͠·͢ʂ ▶ ࣌: 9/2() 19:00 ~ 21:3
0 ▶ https://kubernetes-updates.connpass.com/event/222915/