Opafiber


Fiber를 위한 Open Policy Agent 지원.

참고: Go 1.19 이상 필요

Installation

go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/contrib/opafiber/v2

Signatures

opafiber.New(config opafiber.Config) fiber.Handler

Config

Property
Type
설명
Default

RegoQuery

string

필수 - Rego 쿼리

-

RegoPolicy

io.Reader

필수 - Rego 정책

-

IncludeQueryString

bool

쿼리 문자열을 Rego 정책의 입력으로 포함

false

DeniedStatusCode

int

정책이 요청을 거부할 때 반환할 HTTP 상태 코드

400

DeniedResponseMessage

string

정책이 요청을 거부할 때 반환할 HTTP 응답 본문 텍스트

""

IncludeHeaders

[]string

헤더를 Rego 정책의 입력으로 포함

-

InputCreationMethod

InputCreationFunc

OPA에 대한 입력을 제공할 사용자 정의 함수 사용

func defaultInput(ctx *fiber.Ctx) (map[string]interface{}, error)

Types

type InputCreationFunc func(c *fiber.Ctx) (map[string]interface{}, error)

Examples

OPA Fiber 미들웨어는 다음과 같은 예시 데이터를 정책 엔진에 입력으로 전송합니다:

{
  "method": "GET",
  "path": "/somePath",
  "query": {
    "name": ["John Doe"]
  },
  "headers": {
    "Accept": "application/json",
    "Content-Type": "application/json"
  }
}
package main

import (
  "github.com/gofiber/fiber/v2"
  "github.com/gofiber/contrib/opafiber/v2"
)

func main() {
  app := fiber.New()

  module := `
    package example.authz

    default allow := false
    
    allow {
      input.method == "GET"  
    }
  `

  cfg := opafiber.Config{
    RegoQuery:             "data.example.authz.allow",
    RegoPolicy:            bytes.NewBufferString(module),
    IncludeQueryString:    true,
    DeniedStatusCode:      fiber.StatusForbidden,
    DeniedResponseMessage: "상태 금지됨",
    IncludeHeaders:        []string{"Authorization"},
    InputCreationMethod: func(ctx *fiber.Ctx) (map[string]interface{}, error) {
      return map[string]interface{}{
        "method": ctx.Method(),
        "path":   ctx.Path(),
      }, nil
    },
  }

  app.Use(opafiber.New(cfg))

  app.Get("/", func(ctx *fiber.Ctx) error {
    return ctx.SendStatus(200)
  })

  app.Listen(":8080")
}

Last updated