10.2. Knative 서비스의 JSON Web Token 인증 설정

OpenShift Serverless에는 현재 사용자 정의 권한 부여 기능이 없습니다. 배포에 사용자 정의 권한 부여를 추가하려면 OpenShift Serverless를 Red Hat OpenShift Service Mesh와 통합한 다음 Knative 서비스에 대해 JSON 웹 토큰(JWT) 인증 및 사이드카 삽입을 구성해야 합니다.

10.2.1. Service Mesh 2.x 및 OpenShift Serverless에서 JSON 웹 토큰 인증 사용

Service Mesh 2.x 및 OpenShift Serverless를 사용하여 Knative 서비스와 함께 JSON 웹 토큰(JWT) 인증을 사용할 수 있습니다. 이렇게 하려면 ServiceMeshMemberRoll 오브젝트의 멤버인 애플리케이션 네임스페이스에서 인증 요청 및 정책을 생성해야 합니다. 서비스에 대한 사이드카 삽입도 활성화해야 합니다.

중요

knative-servingknative-serving-ingress와 같은 시스템 네임스페이스의 Pod에 Kourier가 활성화되어 있는 경우 사이드카를 삽입할 수 없습니다.

이러한 네임스페이스의 Pod에 사이드카를 삽입해야 하는 경우 Service Mesh와 OpenShift Serverless 통합에 대한 OpenShift Serverless 문서를 참조하십시오.

사전 요구 사항

  • OpenShift Serverless Operator, Knative Serving, Red Hat OpenShift Service Mesh를 클러스터에 설치했습니다.
  • OpenShift CLI(oc)를 설치합니다.
  • 프로젝트를 생성했거나 OpenShift Container Platform에서 애플리케이션 및 기타 워크로드를 생성하는 데 적절한 역할 및 권한이 있는 프로젝트에 액세스할 수 있습니다.

절차

  1. 서비스에 sidecar.istio.io/inject="true" 주석을 추가합니다.

    서비스의 예

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: <service_name>
    spec:
      template:
        metadata:
          annotations:
            sidecar.istio.io/inject: "true" 1
            sidecar.istio.io/rewriteAppHTTPProbers: "true" 2
    ...

    1
    sidecar.istio.io/inject="true" 주석을 추가합니다.
    2
    OpenShift Serverless 버전 1.14.0 이상은 기본적으로 Knative 서비스에 대한 준비 상태 프로브로 사용하므로 Knative 서비스에서 주석 sidecar.istio.io/rewriteAppHTTPProbers: "true" 를 설정해야 합니다.
  2. Service 리소스를 적용합니다.

    $ oc apply -f <filename>
  3. ServiceMeshMemberRoll 오브젝트의 멤버인 각 서버리스 애플리케이션 네임스페이스에 RequestAuthentication 리소스를 생성합니다.

    apiVersion: security.istio.io/v1beta1
    kind: RequestAuthentication
    metadata:
      name: jwt-example
      namespace: <namespace>
    spec:
      jwtRules:
      - issuer: testing@secure.istio.io
        jwksUri: https://raw.githubusercontent.com/istio/istio/release-1.8/security/tools/jwt/samples/jwks.json
  4. RequestAuthentication 리소스를 적용합니다.

    $ oc apply -f <filename>
  5. 다음 AuthorizationPolicy 리소스를 생성하여 ServiceMeshMemberRoll 오브젝트의 멤버인 각 서버리스 애플리케이션 네임스페이스의 시스템 Pod에서 RequestAuthenticaton 리소스에 대한 액세스를 허용합니다.

    apiVersion: security.istio.io/v1beta1
    kind: AuthorizationPolicy
    metadata:
      name: allowlist-by-paths
      namespace: <namespace>
    spec:
      action: ALLOW
      rules:
      - to:
        - operation:
            paths:
            - /metrics 1
            - /healthz 2
    1
    시스템 Pod별 지표를 수집하는 애플리케이션의 경로입니다.
    2
    시스템 Pod별로 검색할 애플리케이션의 경로입니다.
  6. AuthorizationPolicy 리소스를 적용합니다.

    $ oc apply -f <filename>
  7. ServiceMeshMemberRoll 오브젝트의 멤버인 각 서버리스 애플리케이션 네임스페이스에서 다음 AuthorizationPolicy 리소스를 생성합니다.

    apiVersion: security.istio.io/v1beta1
    kind: AuthorizationPolicy
    metadata:
      name: require-jwt
      namespace: <namespace>
    spec:
      action: ALLOW
      rules:
      - from:
        - source:
           requestPrincipals: ["testing@secure.istio.io/testing@secure.istio.io"]
  8. AuthorizationPolicy 리소스를 적용합니다.

    $ oc apply -f <filename>

검증

  1. Knative 서비스 URL을 가져오기 위해 curl 요청을 사용하면 해당 요청이 거부됩니다.

    명령 예

    $ curl http://hello-example-1-default.apps.mycluster.example.com/

    출력 예

    RBAC: access denied

  2. 유효한 JWT로 요청을 확인합니다.

    1. 유효한 JWT 토큰을 가져옵니다.

      $ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.8/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
    2. curl 요청 헤더에 유효한 토큰을 사용하여 서비스에 액세스합니다.

      $ curl -H "Authorization: Bearer $TOKEN"  http://hello-example-1-default.apps.example.com

      그러면 요청이 허용됩니다.

      출력 예

      Hello OpenShift!

10.2.2. Service Mesh 1.x 및 OpenShift Serverless에서 JSON 웹 토큰 인증 사용

Service Mesh 1.x 및 OpenShift Serverless를 사용하여 Knative 서비스와 함께 JSON 웹 토큰(JWT) 인증을 사용할 수 있습니다. 이렇게 하려면 ServiceMeshMemberRoll 오브젝트의 멤버인 애플리케이션 네임스페이스에 정책을 만들어야 합니다. 서비스에 대한 사이드카 삽입도 활성화해야 합니다.

중요

knative-servingknative-serving-ingress와 같은 시스템 네임스페이스의 Pod에 Kourier가 활성화되어 있는 경우 사이드카를 삽입할 수 없습니다.

이러한 네임스페이스의 Pod에 사이드카를 삽입해야 하는 경우 Service Mesh와 OpenShift Serverless 통합에 대한 OpenShift Serverless 문서를 참조하십시오.

사전 요구 사항

  • OpenShift Serverless Operator, Knative Serving, Red Hat OpenShift Service Mesh를 클러스터에 설치했습니다.
  • OpenShift CLI(oc)를 설치합니다.
  • 프로젝트를 생성했거나 OpenShift Container Platform에서 애플리케이션 및 기타 워크로드를 생성하는 데 적절한 역할 및 권한이 있는 프로젝트에 액세스할 수 있습니다.

절차

  1. 서비스에 sidecar.istio.io/inject="true" 주석을 추가합니다.

    서비스의 예

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: <service_name>
    spec:
      template:
        metadata:
          annotations:
            sidecar.istio.io/inject: "true" 1
            sidecar.istio.io/rewriteAppHTTPProbers: "true" 2
    ...

    1
    sidecar.istio.io/inject="true" 주석을 추가합니다.
    2
    OpenShift Serverless 버전 1.14.0 이상은 기본적으로 Knative 서비스에 대한 준비 상태 프로브로 사용하므로 Knative 서비스에서 주석 sidecar.istio.io/rewriteAppHTTPProbers: "true" 를 설정해야 합니다.
  2. Service 리소스를 적용합니다.

    $ oc apply -f <filename>
  3. 유효한 JWT(JSON 웹 토큰)가 있는 요청만 허용하는 ServiceMeshMemberRoll 오브젝트의 멤버인 서버리스 애플리케이션 네임스페이스에 정책을 생성합니다.

    중요

    /metrics/healthz 경로는 knative-serving 네임스페이스의 시스템 Pod에서 액세스하므로 excludedPaths에 포함되어야 합니다.

    apiVersion: authentication.istio.io/v1alpha1
    kind: Policy
    metadata:
      name: default
      namespace: <namespace>
    spec:
      origins:
      - jwt:
          issuer: testing@secure.istio.io
          jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/jwks.json"
          triggerRules:
          - excludedPaths:
            - prefix: /metrics 1
            - prefix: /healthz 2
      principalBinding: USE_ORIGIN
    1
    시스템 Pod별 지표를 수집하는 애플리케이션의 경로입니다.
    2
    시스템 Pod별로 검색할 애플리케이션의 경로입니다.
  4. Policy 리소스를 적용합니다.

    $ oc apply -f <filename>

검증

  1. Knative 서비스 URL을 가져오기 위해 curl 요청을 사용하면 해당 요청이 거부됩니다.

    $ curl http://hello-example-default.apps.mycluster.example.com/

    출력 예

    Origin authentication failed.

  2. 유효한 JWT로 요청을 확인합니다.

    1. 유효한 JWT 토큰을 가져옵니다.

      $ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
    2. curl 요청 헤더에 유효한 토큰을 사용하여 서비스에 액세스합니다.

      $ curl http://hello-example-default.apps.mycluster.example.com/ -H "Authorization: Bearer $TOKEN"

      그러면 요청이 허용됩니다.

      출력 예

      Hello OpenShift!