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

OpenID Connect: Kong OAuth 토큰 인증

원문 보기 위키 갱신

OpenID Connect: Kong OAuth 토큰 인증 (OpenID Connect: Kong OAuth token authentication)

이 문서는 OpenID Connect 플러그인을 설정해서 Kong OAuth 2.0 플러그인이 발급한 액세스 토큰을 검증하는 방법을 안내해요. 클라이언트와 게이트웨이, 업스트림 서비스 사이에서 인증이 어떻게 진행되는지 다이어그램으로 쉽게 설명해 줘요. 필요한 사전 요구 사항과 환경 변수, decK를 활용한 플러그인 설정 예시까지 차근차근 살펴볼 수 있어요

출처: 문서

본문

Kong OAuth 토큰 인증v1.0+

OpenID Connect 플러그인을 설정해서 Kong OAuth 2.0 플러그인이 발급한 토큰을 검증하세요.

Kong OAuth2 인증이 동작하는 방식은 다음과 같아요:

 
sequenceDiagram
    autonumber
    participant client as Client 
(e.g. mobile app) participant kong as Gateway participant httpbin as Upstream
(upstream service,
e.g. httpbin) activate client activate kong client->>kong: Service with
access token deactivate client kong->>kong: load access token kong->>kong: verify kong
oauth token activate httpbin kong->>httpbin: request with
access token httpbin->>kong: response deactivate httpbin activate client kong->>client: response deactivate kong deactivate client

이 예제에서 OpenID Connect 플러그인은 헤더로 전송된 bearer 토큰만 받아들이지만, bearer_token_param_type 파라미터를 body, query, cookie 또는 이 값들의 조합으로 설정할 수도 있어요.

Keycloak을 사용해서 Kong OAuth2 토큰으로 인증하는 전체 예제는 Kong OAuth2로 OpenID Connect 구성하기 튜토리얼을 참고하세요.

참고: config.client_auth를 client_secret_post로 설정하면 IdP와의 연결을 테스트할 수 있지만, 프로덕션 환경에서는 더 안전한 인증 방법을 사용하는 걸 추천해요. 지원되는 클라이언트 인증 방법 중 어떤 것이든 사용할 수 있어요.

전제 조건

  • 구성된 identity provider(IdP)

  • Kong OAuth2 자격 증명을 가진 Consumer

  • 보호하려는 Service 또는 Route에 구성된 OAuth2 플러그인

환경 변수

  • ISSUER: IdP의 issuer 인증 URL이에요. 예를 들어, Keycloak을 IdP로 사용한다면 issuer URL은 다음과 같아요: http://localhost:8080/realms/example-realm

  • CLIENT_ID: 플러그인이 IdP의 인증된 엔드포인트를 호출할 때 사용하는 클라이언트 ID예요.

  • CLIENT_SECRET: IdP에 연결하는 데 필요한 클라이언트 시크릿이에요.

  • TOKEN_SALT: 매번 동기화할 때마다 세션 자격 증명이 다시 생성되지 않도록 decK가(v1.59 이상) 필요로 하는 랜덤 문자열이에요. 이 파라미터는 다른 포맷에서는 선택 사항이에요. 예를 들어, openssl rand -base64 16으로 하나 생성하세요.

플러그인 설정하기

전역

decK

다음 섹션을 kong.yaml 설정 파일에 추가하세요:

_format_version: "3.0"
plugins:
  - name: openid-connect
    config:
      issuer: ${{ env "DECK_ISSUER" }}
      client_id:
      - ${{ env "DECK_CLIENT_ID" }}
      client_secret:
      - ${{ env "DECK_CLIENT_SECRET" }}
      client_auth:
      - client_secret_post
      auth_methods:
      - kong_oauth2
      bearer_token_param_type:
      - header
      cache_tokens_salt: ${{ env "DECK_TOKEN_SALT" }}

{: data-file="kong.yaml" data-tool="deck" }

Admin API

다음 요청을 보내세요:

curl -i -X POST http://localhost:8001/plugins/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "name": "openid-connect",
      "config": {
        "issuer": "'$ISSUER'",
        "client_id": [
          "'$CLIENT_ID'"
        ],
        "client_secret": [
          "'$CLIENT_SECRET'"
        ],
        "client_auth": [
          "client_secret_post"
        ],
        "auth_methods": [
          "kong_oauth2"
        ],
        "bearer_token_param_type": [
          "header"
        ],
        "cache_tokens_salt": "'$TOKEN_SALT'"
      }
    }
    '
KIC
echo "
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
  name:
  namespace: kong
  annotations:
    kubernetes.io/ingress.class: kong
  labels:
    global: 'true'
config:
  issuer: '$ISSUER'
  client_id:
  - '$CLIENT_ID'
  client_secret:
  - '$CLIENT_SECRET'
  client_auth:
  - client_secret_post
  auth_methods:
  - kong_oauth2
  bearer_token_param_type:
  - header
  cache_tokens_salt: '$TOKEN_SALT'
plugin: openid-connect
" | kubectl apply -f -

Service

decK

다음 섹션을 kong.yaml 설정 파일에 추가하세요:

_format_version: "3.0"
plugins:
  - name: openid-connect
    service: serviceName|Id
    config:
      issuer: ${{ env "DECK_ISSUER" }}
      client_id:
      - ${{ env "DECK_CLIENT_ID" }}
      client_secret:
      - ${{ env "DECK_CLIENT_SECRET" }}
      client_auth:
      - client_secret_post
      auth_methods:
      - kong_oauth2
      bearer_token_param_type:
      - header
      cache_tokens_salt: ${{ env "DECK_TOKEN_SALT" }}

{: data-file="kong.yaml" data-tool="deck" }

다음 플레이스홀더를 여러분의 값으로 바꾸세요:

  • serviceName|Id: 플러그인 설정이 대상으로 할 service의 id 또는 name이에요.
Admin API

다음 요청을 보내세요:

curl -i -X POST http://localhost:8001/services/{serviceName|Id}/plugins/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "name": "openid-connect",
      "config": {
        "issuer": "'$ISSUER'",
        "client_id": [
          "'$CLIENT_ID'"
        ],
        "client_secret": [
          "'$CLIENT_SECRET'"
        ],
        "client_auth": [
          "client_secret_post"
        ],
        "auth_methods": [
          "kong_oauth2"
        ],
        "bearer_token_param_type": [
          "header"
        ],
        "cache_tokens_salt": "'$TOKEN_SALT'"
      }
    }
    '

다음 플레이스홀더를 여러분의 값으로 바꾸세요:

  • serviceName|Id: 플러그인 설정이 대상으로 할 service의 id 또는 name이에요.
KIC
echo "
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name:
  namespace: kong
  annotations:
    kubernetes.io/ingress.class: kong
config:
  issuer: '$ISSUER'
  client_id:
  - '$CLIENT_ID'
  client_secret:
  - '$CLIENT_SECRET'
  client_auth:
  - client_secret_post
  auth_methods:
  - kong_oauth2
  bearer_token_param_type:
  - header
  cache_tokens_salt: '$TOKEN_SALT'
plugin: openid-connect
" | kubectl apply -f -

다음으로, service 리소스에 어노테이션을 붙여서 KongPlugin 리소스를 적용하세요:

kubectl annotate -n kong service SERVICE_NAME konghq.com/plugins=

Route

decK

다음 섹션을 kong.yaml 설정 파일에 추가하세요:

_format_version: "3.0"
plugins:
  - name: openid-connect
    route: routeName|Id
    config:
      issuer: ${{ env "DECK_ISSUER" }}
      client_id:
      - ${{ env "DECK_CLIENT_ID" }}
      client_secret:
      - ${{ env "DECK_CLIENT_SECRET" }}
      client_auth:
      - client_secret_post
      auth_methods:
      - kong_oauth2
      bearer_token_param_type:
      - header
      cache_tokens_salt: ${{ env "DECK_TOKEN_SALT" }}

{: data-file="kong.yaml" data-tool="deck" }

다음 플레이스홀더를 여러분의 값으로 바꾸세요:

  • routeName|Id: 플러그인 설정이 대상으로 할 route의 id 또는 name이에요.
Admin API

다음 요청을 보내세요:

curl -i -X POST http://localhost:8001/routes/{routeName|Id}/plugins/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "name": "openid-connect",
      "config": {
        "issuer": "'$ISSUER'",
        "client_id": [
          "'$CLIENT_ID'"
        ],
        "client_secret": [
          "'$CLIENT_SECRET'"
        ],
        "client_auth": [
          "client_secret_post"
        ],
        "auth_methods": [
          "kong_oauth2"
        ],
        "bearer_token_param_type": [
          "header"
        ],
        "cache_tokens_salt": "'$TOKEN_SALT'"
      }
    }
    '

다음 플레이스홀더를 여러분의 값으로 바꾸세요:

  • routeName|Id: 플러그인 설정이 대상으로 할 route의 id 또는 name이에요.
KIC
echo "
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name:
  namespace: kong
  annotations:
    kubernetes.io/ingress.class: kong
config:
  issuer: '$ISSUER'
  client_id:
  - '$CLIENT_ID'
  client_secret:
  - '$CLIENT_SECRET'
  client_auth:
  - client_secret_post
  auth_methods:
  - kong_oauth2
  bearer_token_param_type:
  - header
  cache_tokens_salt: '$TOKEN_SALT'
plugin: openid-connect
" | kubectl apply -f -

다음으로, httproute 또는 ingress 리소스에 어노테이션을 붙여서 KongPlugin 리소스를 적용하세요:

Gateway API
kubectl annotate -n kong httproute  konghq.com/plugins=
Ingress
kubectl annotate -n kong ingress  konghq.com/plugins=

관련 리소스

더 알아보기 (Learn more)