Upgrade to Pro — share decks privately, control downloads, hide ads and more …

入門Open Policy Agent: Policy as Codeを目指して / introduction-to-open-policy-agent

Kurochan
September 16, 2021

入門Open Policy Agent: Policy as Codeを目指して / introduction-to-open-policy-agent

サイバーエージェントの社内エンジニアカンファレンス CA BASE CAMP 2021で発表した資料です

Kurochan

September 16, 2021
Tweet

More Decks by Kurochan

Other Decks in Technology

Transcript

  1. גࣜձࣾαΠόʔΤʔδΣϯτ AIࣄۀຊ෦
    ೖ໳Open Policy Agent
    Policy as CodeΛ໨ࢦͯ͠
    ࠇ࡚ ༏ଠ

    View full-size slide

  2. ࠇ࡚ ༏ଠ
    2015೥౓ ৽ଔೖࣾ
    AIࣄۀຊ෦ DXຊ෦ ΞϓϦӡ༻ηϯλʔ
    @kurochan
    @kuro_m88
    ج൫ٕज़੹೚ऀ
    🆕 αΠόʔΤʔδΣϯτ CTO౷ׅࣨ
    #times_kurochan

    View full-size slide

  3. ࠓճ͓࿩͢Δ಺༰
    • Open Policy Agentͱ͍͏ιϑτ΢ΣΞʹ͍ͭͯͷ঺հ
    • Open Policy Agent͸ೝՄͷจ຺Ͱ঺հ͞ΕΔ͜ͱ͕ଟ͍͕ɺ
    ΋͏গ͠Ҿ͍ͯʮϙϦγʔʯͱ͍͏ࢹ఺͔Β঺հ
    • ΞϓϦӡ༻ηϯλʔͰಋೖ͢Δ༧ఆ͕͋ΔͷͰ
    ૝ఆ͍ͯ͠ΔϢʔεέʔεͷ঺հ

    View full-size slide

  4. 1.Open Policy Agentͱ͸
    2.Policy as Code
    3.Open Policy Agentͷ࢖͍ํ
    4.Open Policy Agentͷ࢖͍ॴ
    5.ΞϓϦӡ༻ηϯλʔͰͷ
    Open Policy Agentͷಋೖ༧ఆ

    View full-size slide

  5. Open Policy Agentͱ͸

    View full-size slide

  6. Open Policy Agentͱ͸
    • ܰྔͳ൚༻ͷʮϙϦγʔΤϯδϯʯ
    • ಠཱͯ͠ಈ͔͢͜ͱ΋Ͱ͖Δ͠ɺαʔϏεʹϥΠϒϥϦͱͯ͠౷߹͢Δ͜ͱ΋Մೳ
    • WebAssembly΋αϙʔτͯͨ͠Γ
    • OPAͱུ͞ΕΔ͜ͱ͕ଟ͍
    • ΫΤϦʹରͯ͠ϙϦγʔΛద༻ɺ݁ՌΛੜ੒͢Δ
    • ϙϦγʔ͸Regoͱ͍͏ݴޠͰੜ੒͢Δ

    View full-size slide

  7. "Policy"ͱ͸
    • ϧʔϧͷू߹
    • ೖྗΛ৚݅Ͱൺֱͨ͠Γ
    • rate limitͷΑ͏ʹಈతʹมΘΔΑ͏ͳ΋ͷͩͬͨΓ
    • ͦΕΒΛ૊Έ߹ΘͤͨΓ
    • ࠷ऴతʹԿ͔͠ΒͷҙࢥܾఆΛ͢Δ

    View full-size slide

  8. ϙϦγʔΤϯδϯΛ෼཭͢Δͱ͍͏͜ͱ
    • ϙϦγʔͷ࣮૷͸೉͍͠
    • ਖ਼࣮͘͠૷Ͱ͖·͔͢…ʁ
    • ෳࡶͳϙϦγʔ͸ϙϦγʔࣗମͷڍಈΛςετ͍ͨ͠
    • ࠓճ͸ʮϙϦγʔΤϯδϯʯͱ͍͏෦඼͕༷ʑͳϢʔεέʔεʹద༻Մ
    ೳͰ͋Δ͜ͱΛ͓࿩͠·͢

    View full-size slide

  9. Policy as Code

    View full-size slide

  10. Policy as Code
    • XXX as Code
    • Infrastructure as CodeͳͲ
    • ϙϦγʔ͸ͦΕࣗମ͕γεςϜͷٕज़తͳ੍໿΍ηΩϡϦςΟͳͲɺ
    ॏཁͳ஌ࣝΛ಺แ͍ͯ͠Δ
    • ΞϓϦέʔγϣϯίʔυதʹຒΊࠐ·ΕΔΑΓϙϦγʔͱͯ͠
    ಠཱͯ͠ఆٛ͢Δ͜ͱͰ҉໧஌Խ͢Δ͜ͱ΋๷͙͜ͱ͕Ͱ͖Δ

    View full-size slide

  11. Rego
    • ϙϦγʔΛهड़͢ΔͨΊͷݴޠ
    • ߏ଄Խ͞Ε͍ͯͯ֊૚తͳσʔλߏ଄Λ࣋ͯΔ
    • JSONͷΑ͏ͳߏ଄΋ѻ͑Δ
    • ϙϦγʔͷద༻݁Ռ΋ಉ༷ʹॊೈͳσʔλߏ଄ͰදݱͰ͖Δ
    • ೖग़ྗͷσʔλߏ଄͕͔ͳΓࣗ༝
    • ৚݅ࣜ΍ؔ਺΋ॆ࣮

    View full-size slide

  12. Regoͷจ๏ྫ
    • งғؾ͚ͩ঺հ͠·͢
    • ਖ਼͍͠จ๏͸ެࣜυΩϡϝϯτΛࢀর͍ͯͩ͘͠͞
    https://www.openpolicyagent.org/docs/latest/policy-language/

    View full-size slide

  13. Regoͷจ๏ྫ: ม਺

    View full-size slide

  14. Regoͷจ๏ྫ: Object

    View full-size slide

  15. Regoͷจ๏ྫ: Rule

    View full-size slide

  16. Regoͷจ๏ྫ: Rule

    View full-size slide

  17. Regoͷจ๏ྫ: ৚݅
    • ࢛ଇԋࢉɺ౳߸ɺෆ౳߸ɺϏοτԋࢉͳͲ͸΋ͪΖΜఆٛ͞Ε͍ͯΔ͕
    ͜ΕΒ͸͢΂ͯϏϧτΠϯؔ਺ͱͯ͠ѻΘΕΔ

    View full-size slide

  18. Regoͷจ๏ྫ: ϏϧτΠϯؔ਺
    • Ϗοτԋࢉɺू߹ԋࢉɺਖ਼نදݱɺจࣈྻૢ࡞
    • Base64ɺURLɺJSON/YAMLɺUUID
    • ࣌ࠁ
    • άϥϑ
    • ωοτϫʔΫ
    • τʔΫϯ(JWTͳͲ)
    • ଞʹ΋ศརͳؔ਺͕࠷ॳ͔Βͨ͘͞Μ༻ҙ͞Ε͍ͯΔ

    View full-size slide

  19. ؆୯ͳྫ
    • HTTPϦΫΤετͷೝՄ
    • user "alice" ͸ /hello ʹରͯ͠GETϦΫΤετ͕Ͱ͖Δ

    View full-size slide

  20. The Rego Playground
    • ϒϥ΢β্ͰRegoΛॻ͍ͯࢼͤͯศར
    https://play.openpolicyagent.org/

    View full-size slide

  21. Testable
    • ςετ͕ॻ͚Δʂ

    View full-size slide

  22. Testable
    • ςετ͕ॻ͚Δʂ

    View full-size slide

  23. Open Policy Agentͷ࢖͍ํ

    View full-size slide

  24. ೝՄػೳΛඋ͑ͨAPI
    • ೝূͱೝՄ
    • ೝূ(Authenticate)
    • ʮ୭ͳͷ͔ʯΛࣝผ͢Δ
    • ೝՄ(Authorize)
    • ʮԿ͕Ͱ͖Δͷ͔(Ͱ͖ͳ͍ͷ͔)ʯΛ൑அ͢Δ

    View full-size slide

  25. RBACΛ࣮૷ͯ͠ΈΔ

    View full-size slide

  26. External Data
    • ͜͜·Ͱͷྫͩͱuser_roles΍role_permissions૬౰͸ݻఆ
    • ࣮༻్Ͱ͸ಈతʹมԽͤͨ͞Γ૿ݮ͍ͤͨ͞
    • ϙϦγʔΛධՁ͢Δʹ͋ͨͬͯඞཁͳ৘ใΛ֎෦͔Βऔಘ͢Δ࢓૊Έ

    View full-size slide

  27. ϦΫΤετʹຒΊࠐΉ
    • ϦΫΤετΛૹ৴͢Δଆ͕෇Ճ৘ใΛૹ৴͢Δ
    • ΋ͪΖΜૹΒΕͯ͘Δ஋͕৴༻Ͱ͖ͳ͍ͱμϝ
    • ৴༻Ͱ͖Δͱ͍͏લఏ͔ɺ
    JWTͳͲͰॺ໊͞Εͨ஋Λݕূͯ͠࢖͏͔
    https://www.openpolicyagent.org/docs/latest/external-data/

    View full-size slide

  28. σʔλΛ౉͢(push)
    • ֎෦͔Βߋ৽͕͋Δ౓ʹpush͢Δ
    • ಉظ࿙Εͱ͔ϥά͕ى͖Δͱ͜Θ͍͔΋
    • ͋·Γେ͖ͳσʔλ͸஗Εͳ͍
    https://www.openpolicyagent.org/docs/latest/external-data/

    View full-size slide

  29. σʔλΛ΋Β͏(pull)
    • OPA͕ಈతʹ֎෦ͷAPIΛݺͼग़ͤΔ(B)
    • OPAͷϨεϙϯελΠϜ͸૿Ճ͢Δ
    • ॊೈʹ࿈ܞ͠΍ͦ͢͏
    https://www.openpolicyagent.org/docs/latest/external-data/

    View full-size slide

  30. ςετ
    • ϙϦγʔͱಉ͡σΟϨΫτϦʹςετϑΝΠϧΛஔ͘
    • "test_"Ͱ࢝·Δϧʔϧ͕ධՁ͞ΕɺͦΕ͕ςετʹͳΔ
    • opa testίϚϯυͰςετ͕࣮ߦͰ͖Δ
    • ΧόϨοδܭଌ΋Մೳ

    View full-size slide

  31. ஋ͷϞοΫ
    • withΩʔϫʔυͰ஋Λஔ͖׵͑ΒΕΔ

    View full-size slide

  32. Open Policy Agentͷ࢖͍Ͳ͜Ζ

    View full-size slide

  33. Open Policy Agentͷ࢖͍Ͳ͜Ζ
    • Kubernetesͷݖݶ؅ཧͷจ຺Ͱ঺հ͞ΕΔ͜ͱ͕ଟ͍ҹ৅
    • ͦΕҎ֎ͷ༻్Λத৺ʹ঺հ͠·͢

    View full-size slide

  34. Envoyͱ͸
    https://speakerdeck.com/kurochan/ru-men-envoy

    View full-size slide

  35. Envoyͱ͸
    • OSSͷL4/L7ϓϩΩγ
    • ʮϞμϯͳαʔϏεࢦ޲ΞʔΩςΫνϟʯ޲͚
    • ʮϢχόʔαϧσʔλϓϨʔϯʯΛ໨ࢦͯ͠։ൃ͞Ε͍ͯΔ
    • ύϑΥʔϚϯεʹ༏Εɺ֦ுੑ͕ߴ͘ɺAPIܦ༝ͰίϯτϩʔϧՄೳ
    https://www.envoyproxy.io/docs/envoy/latest/intro/what_is_envoy

    View full-size slide

  36. Envoy࿈ܞ
    • EnvoyͱOPAΛ࿈ܞͤͯ͞ೝՄػೳ෇͖ͷήʔτ΢ΣΠΛߏ੒Մೳ
    https://www.openpolicyagent.org/docs/latest/envoy-introduction/

    View full-size slide

  37. Envoy࿈ܞ
    • EnvoyͱOPAΛ࿈ܞͤͯ͞ೝՄػೳ෇͖ͷήʔτ΢ΣΠΛߏ੒Մೳ
    • ೝՄ͞ΕͨτϥϑΟοΫ͔͠௨ա͠ͳ͍
    • ޙஈͷΞϓϦέʔγϣϯαʔό౳ͷ࣮૷ָ͕ʹͳΔ

    View full-size slide

  38. TerraformʹϙϦγʔΛద༻͢Δʁ
    • terraform.analysisͱ͍͏ύοέʔδ͕༻ҙ͞Ε͍ͯΔ
    • terraform planͷ݁Ռ͕ҙਤ͍ͯ͠Δ͔Ͳ͏͔ͷνΣοΫ͕Ͱ͖Δ
    • CIͳͲʹ૊ΈࠐΉͱࣄނ๷ࢭͷνΣοΫʹͳΔ
    • ྫ
    • ෆ༻ҙʹIAMͷઃఆมߋ͕͞Ε͍ͯͳ͍͔ʁ
    • ࣮ߦͨ͠ਓ͕؅ཧऀͰ͋Ε͹OKͳͲͷίϯςΩετ΋࣋ͨͤΒΕͦ͏
    • Ұఆͷᮢ஋Ҏ্ͷมߋ͕Ұ౓ʹͳ͞Ε͍ͯͳ͍͔ʁ
    • ޡͬͯ؀ڥΛഁյ͞Εͳ͍Α͏ʹͰ͖ͦ͏

    View full-size slide

  39. Terraform࿈ܞͷྫ
    • terraform planͷ݁Ռʹରͯ͠ϙϦγʔΛద༻͢Δ

    View full-size slide

  40. ϩάʹϙϦγʔΛద༻͢ΔͱͲ͏ͳΔͷ͔
    • ϩά؂ࢹͰΑ͘΍Δ͜ͱ
    • ΤϥʔΧ΢ϯτ
    • ҟৗ஋ͷݕग़
    • ෳࡶͳ৚݅Ͱͷϩά؂ࢹ
    • ಛఆͷIPΞυϨε͔Βͷෆਖ਼ͳΞΫηε
    • S3όέοτͷՄࢹൣғͷઃఆมߋ
    • ͳͲ
    • ͦΕɺOPAͰݕग़Ͱ͖ΔͷͰ͸…ʁ
    • Կ͔ͷҙࢥܾఆΛ͢ΔҎ֎ʹ΋ɺಛఆ৚݅ͷΞΫςΟϏςΟͷݕग़ʹ΋͔ͭ͑Δʂ

    View full-size slide

  41. ΧελϚΠζ

    View full-size slide

  42. ͜͜·ͰͰ෼͔ͬͨ͜ͱ
    • Open Policy Agent͸೚ҙͷೖྗʹରͯ͠ϙϦγʔΛద༻ͨ݁͠ՌΛ
    ฦ͢ύʔπͱ͔ͯ͠ͳΓ൚༻ੑ͕ߴ͍
    • Open Policy AgentΛαʔϏεʹ૊ΈࠐΜͰ࢖͏ʹ͸
    Ͳ͏͢Ε͹Α͍ͷ͔

    View full-size slide

  43. REST API
    • input JSONͰPOST͢ΔͱɺϨεϙϯε͕JSONͰฦͬͯ͘Δ

    View full-size slide

  44. Go API
    • GolangͷϥΠϒϥϦͱͯ͠ݺͼग़͢͜ͱ͕Մೳ

    View full-size slide

  45. ΞϓϦӡ༻ηϯλʔͰͷ
    Open Policy AgentͷಋೖΞΠσΞ

    View full-size slide

  46. ΞϓϦӡ༻ηϯλʔͱ͸
    • 140ஹԁͷڊେࢢ৔ɺখചۀքͷ࠶ൃ໌ʹ௅Ή։ൃϓϩδΣΫτ
    https://speakerdeck.com/kurochan/retail-dx-project

    View full-size slide

  47. ΞϓϦӡ༻ηϯλʔͰ։ൃ͍ͯ͠ΔγεςϜͨͪ
    • ڠಇൢଅ޲͚γεςϜ
    • ձһΞϓϦ
    • ECαΠτ
    • σʔλج൫
    • ͜ΕΒͷ؅ཧը໘
    • ༷ʑͳϙϦγʔͰΞΫηε੍ޚΛ͍ͨ͠

    View full-size slide

  48. ΞϓϦӡ༻ηϯλʔͱ͸
    • খചۀքͷDXΛਪਐ͢ΔϓϩμΫτΛ։ൃ͢Δ෦ॺ
    • ͍ΖΜͳγεςϜΛ։ൃ͢Δ
    • ͍ΖΜͳγεςϜ = ͍ΖΜͳAPI
    • ೝূೝՄ͕༷ʑͳγʔϯͰൃੜ͢Δ
    • ͦΕͧΕϏδωεཁ݅΋ඍົʹҟͳΔͷͰ͖ͪΜͱϧʔϧͱͯ͠؅ཧ͍ͨ͠
    • Policy as Codeͷػӡ…ʂ
    • ೝূ => IdP, ೝՄ => ???

    View full-size slide

  49. Open Policy AgentΛ༻͍ͨRBAC
    ڋ൱
    ڐՄ
    ڐՄ
    ϦιʔεA
    ϦιʔεB
    A͞Μ
    B͞Μ
    σʔλϕʔεͷ஋ͳͲʹԠͯ͡ಈతʹΞΫηεΛڐՄ͢Δ͔൑அ͍ͨ͠

    View full-size slide

  50. Open Policy AgentΛ༻͍ͨRBAC
    https://www.openpolicyagent.org/docs/latest/external-data/
    • ϢʔβA͕ϦιʔεB΁ͷΞΫηεΛͯ͠Α͍͔͕ಈతʹมΘΔέʔε
    • External DataΛ༻͍ͯղܾ

    View full-size slide

  51. ൚༻తͳΞʔΩςΫνϟ
    • ೝূೝՄͷ͘͠ΈΛςϯϓϨʔτԽ͍ͨ͠
    • Envoy + OPA + Backend API
    • Envoy: ೝՄήʔτ΢ΣΠ
    • OPA: ೝՄϙϦγʔΤϯδϯ
    • Backend API: ϏδωεϩδοΫ

    View full-size slide

  52. ൚༻తͳΞʔΩςΫνϟ
    ΫϥΠΞϯτ
    ೝՄ৘ใ
    όοΫΤϯυ

    View full-size slide

  53. ϦΫΤετ ϨεϙϯεͷྲྀΕ
    ϦΫΤετ
    ΫϥΠΞϯτ
    ೝՄ৘ใ
    όοΫΤϯυ
    ڐՄ͞ΕΔύλʔϯ

    View full-size slide

  54. ϦΫΤετ ϨεϙϯεͷྲྀΕ
    ϦΫΤετ
    ΫϥΠΞϯτ
    ೝՄ৘ใ
    όοΫΤϯυ
    ڐՄ͞ΕΔύλʔϯ

    View full-size slide

  55. ϦΫΤετ ϨεϙϯεͷྲྀΕ
    ϦΫΤετ
    ΫϥΠΞϯτ
    ೝՄ৘ใ
    όοΫΤϯυ
    ڐՄ
    ೝՄ৘ใ
    ෇Ճ৘ใ
    ڐՄ͞ΕΔύλʔϯ

    View full-size slide

  56. ϦΫΤετ ϨεϙϯεͷྲྀΕ
    ϦΫΤετ
    ΫϥΠΞϯτ
    ೝՄ৘ใ
    όοΫΤϯυ
    ϦΫΤετ
    ڐՄ
    ೝՄ৘ใ
    ෇Ճ৘ใ
    ڐՄ͞ΕΔύλʔϯ

    View full-size slide

  57. ϦΫΤετ ϨεϙϯεͷྲྀΕ
    ϦΫΤετ
    ΫϥΠΞϯτ
    ೝՄ৘ใ
    όοΫΤϯυ
    ϦΫΤετ
    ڐՄ
    ೝՄ৘ใ
    ෇Ճ৘ใ
    ڐՄ͞ΕΔύλʔϯ

    View full-size slide

  58. ϦΫΤετ ϨεϙϯεͷྲྀΕ
    ϦΫΤετ
    ΫϥΠΞϯτ
    ೝՄ৘ใ
    όοΫΤϯυ
    ೝՄ৘ใ
    ෇Ճ৘ใ
    ڋ൱
    ڋ൱͞ΕΔύλʔϯ

    View full-size slide

  59. ·ͱΊ
    • Open Policy Agentͱ͍͏OSSʹ͍ͭͯ঺հ͠·ͨ͠
    • ϙϦγʔΤϯδϯͷ൚༻ੑʹ͍ͭͯ঺հ͠·ͨ͠
    • ΞϓϦӡ༻ηϯλʔͰͷಋೖΞΠσΞʹ͍͓ͭͯ࿩͠·ͨ͠
    • ࣮ࡍʹಋೖࣄྫ͕Ͱ͖ͨΒͲ͔͜Ͱൃද͍ͨ͠ͱࢥ͍·͢

    View full-size slide

  60. ͝ࢹௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ

    View full-size slide