본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

OPA 플러그인

원문 보기 위키 갱신

OPA 플러그인 (OPA Plugin)

OPA 플러그인은 요청을 Open Policy Agent(OPA)로 전달해서, 인가 정책이 허용하는 경우에만 요청을 처리하게 해줘요. 요청이 들어오면 플러그인이 설정에 지정된 OPA 호스트로 관련 정보를 보내고, OPA가 승인하면 요청이 진행되고 거부하면 요청이 차단돼요. 이 문서에서는 Kong Gateway가 OPA로 전달하는 입력 JSON 형식과, 불리언 또는 객체 형태로 받는 OPA 응답을 설명해요

출처: 문서

본문

예를 들어 요청이 들어오면 OPA 플러그인이 플러그인 구성에 지정된 OPA 호스트(config.opa_host)로 관련 세부 정보를 보내요. OPA가 요청을 승인하면 요청은 계속 진행될 수 있어요. OPA가 요청을 거부하면 요청은 거절돼요.

OPA 입력 형식

OPA 플러그인이 활성화되면 Kong Gateway는 다음 JSON 구조를 사용해 요청 데이터를 OPA로 전달해요. 여기에는 요청 데이터, 헤더, 정규식 캡처 그룹과 함께 관련 Gateway Service, Route, Consumer에 대한 정보가 포함돼요. 아래 예시에 나오는 각 엔티티 파라미터의 정의는 각 엔티티의 스키마를 참고하세요.

{
 "input": {
   "request": { # details about the request from client to Kong
     "http": {
       "host": "example.org", # host header used by the client to make the request
       "port": "8000",        # port to which the request was made
       "tls": {},             # TLS details if the request was made on HTTPS and Kong terminated the TLS connection
       "method": "GET",       # HTTP method used in the request
       "scheme": "http",      # protocol used to make the request by the client, this can be either `http` or `https`
       "path": "/foo/bar",    # HTTP path used in the request
       "querystring": {       # Query string in the HTTP request as key-value pairs
         "foo" : "bar",
         "foo2" : "bar2",
       },
       "headers": {           # HTTP headers of the request
         "accept-encoding": "gzip, deflate",
         "connection": "keep-alive",
         "accept": "*\\/*"
       },
       "uri_captures": {      # The regex capture groups captured on the Kong Gateway Route's path field in the current request. Injected only if `include_uri_captures_in_opa_input` is set to `true`.
         "named": {},
         "unnamed": []
       }
     }
   },
   "client_ip": "127.0.0.1",# client IP address as interpreted by Kong
   "service": {             # The Kong Service resource that this request is forwarded to if OPA allows. Injected only if `include_service_in_opa_input` is set to `true`.
     "host": "httpbin.konghq.com",
     "created_at": 1612819937,
     "connect_timeout": 60000,
     "id": "e6fd8b19-89e5-44e6-8a2a-79e8bf3c31a5",
     "protocol": "http",
     "name": "foo",
     "read_timeout": 60000,
     "port": 80,
     "updated_at": 1612819937,
     "ws_id": "d6020dc4-67f5-4c62-8b45-e2f497c20f5c",
     "retries": 5,
     "write_timeout": 60000
   },
   "route": {               # The Kong Route that was matched for this request. Injected only if `include_route_in_opa_input` is set to `true`.
     "id": "bc6d8617-76a7-441f-aa40-32eb1f5be9e6",
     "paths": [
       "\\/"
     ],
     "protocols": [
       "http",
       "https"
     ],
     "strip_path": true,
     "created_at": 1612819949,
     "ws_id": "d6020dc4-67f5-4c62-8b45-e2f497c20f5c",
     "request_buffering": true,
     "updated_at": 1612819949,
     "preserve_host": false,
     "regex_priority": 0,
     "response_buffering": true,
     "https_redirect_status_code": 426,
     "path_handling": "v0",
     "service": {
       "id": "e6fd8b19-89e5-44e6-8a2a-79e8bf3c31a5"
     }
   },
   "consumer": {            # Kong Consumer that was used for authentication for this request. Injected only if `include_consumer_in_opa_input` is set to `true`.
     "id": "bc6d8617-76a7-431f-aa40-32eb1f5be7e6",
     "username": "kong-consumer-username"
   }
 }
}

이 구조를 OPA 문서와 함께 참고해서 정책을 만들 수 있어요. 예를 들어 input.request.http.querystring.page를 사용해 page 쿼리 파라미터를 참조할 수 있어요.

OPA 응답

OPA가 정책 실행을 마치면 플러그인은 정책 평가 결과를 불리언 또는 객체 형태로 기대해요. OPA가 다른 형식이나 200 OK 이외의 상태 코드를 반환하면 플러그인은 클라이언트에게 500 Internal Server Error를 반환해요.

불리언 응답

OPA는 정책 평가 후 true 또는 false 결과를 반환할 수 있어요. 입력 요청이 정의된 정책을 충족하면 OPA는 "result": true 응답을 보내야 해요. 요청이 정책을 위반하면 OPA는 "result": false 응답을 보내야 해요. 이 경우 응답의 다른 필드는 모두 무시돼요.

객체 응답

대부분의 사용 사례에서는 불리언 응답으로 충분해요. 하지만 필요하다면 정책이 객체를 반환하도록 구성할 수도 있어요. 이를 사용해 요청에 사용자 지정 HTTP 헤더를 주입하거나 거부된 요청의 HTTP 코드를 변경할 수 있어요.

이 경우 OPA 응답은 다음과 같은 구조를 가져요:

{
 "result": {
   "allow": true,
   "status": 201,
   "headers": {
     "key": "value",
     "key2": "value2"
   },
   "message": "value3 or object",
 }
}

이 응답에서 유일하게 필수인 필드는 불리언 값을 받는 result.allow예요.

result.allow가 true이면 result.headers의 키-값 쌍이 업스트림 서비스로 전달되기 전에 요청에 주입돼요.

result.allow가 false로 설정되면 result.headers의 키-값 쌍이 응답에 주입되고, 응답 메시지는 result.message로, 응답의 상태 코드는 result.status로 설정돼요. result.status가 없으면 기본 403 상태 코드가 전송돼요. result.message가 없으면 기본 unauthorized 메시지가 전송돼요.

관련 리소스

더 알아보기 (Learn more)