JWT 클레임을 HTTP 헤더로 복사하기
JWT 클레임을 HTTP 헤더로 복사하기 (Copy JWT Claims to HTTP Headers)
이 작업은 Istio 요청 인증 정책을 통해 JWT 인증이 성공적으로 완료된 후 유효한 JWT 클레임을 HTTP 헤더로 복사하는 방법을 보여드려요. 이 기능은 현재 개발 중이라 실험적으로 간주돼요.
출처: Istio 문서
본문
[!note] 이 기능은 활발히 개발 중이며 실험적(experimental)인 것으로 간주돼요. 이 작업은 Istio 요청 인증 정책을 통해 JWT 인증이 성공적으로 완료된 후 유효한 JWT 클레임을 HTTP 헤더로 복사하는 방법을 보여드려요. [!note] string, boolean, integer 유형의 클레임만 지원돼요. 배열 유형 클레임은 현재 지원되지 않아요.
시작하기 전에 (Before you begin)
이 작업을 시작하기 전에 다음을 수행하세요.
- Istio 최종 사용자 인증 지원에 익숙해지세요.
- Istio 설치 가이드를 사용해서 Istio를 설치하세요.
- 사이드카 주입을 활성화한 상태로
foo네임스페이스에httpbin과curl워크로드를 배포하세요. 다음 명령으로 예시 네임스페이스와 워크로드를 배포하세요.
$ kubectl create ns foo
$ kubectl label namespace foo istio-injection=enabled
$ kubectl apply -f @samples/httpbin/httpbin.yaml@ -n foo
$ kubectl apply -f @samples/curl/curl.yaml@ -n foo
curl이httpbin과 성공적으로 통신하는지 다음 명령으로 확인하세요.
$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl http://httpbin.foo:8000/ip -sS -o /dev/null -w "%{http_code}\n"
200
[!note] 예상 출력이 안 보이면 몇 초 후에 다시 시도하세요. 캐싱과 전파로 인해 지연이 발생할 수 있어요.
유효한 JWT 및 목록 유형 클레임이 있는 요청 허용하기
- 다음 명령은
foo네임스페이스의httpbin워크로드에 대한jwt-example요청 인증 정책을 만들어요. 이 정책은[email protected]가 발급한 JWT를 수락하고foo클레임의 값을X-Jwt-Claim-FooHTTP 헤더로 복사해요.
$ kubectl apply -f - <<EOF
apiVersion: security.istio.io/v1
kind: RequestAuthentication
metadata:
name: "jwt-example"
namespace: foo
spec:
selector:
matchLabels:
app: httpbin
jwtRules:
- issuer: "[email protected]"
jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.31/security/tools/jwt/samples/jwks.json"
outputClaimToHeaders:
- header: "x-jwt-claim-foo"
claim: "foo"
EOF
- 잘못된 JWT가 있는 요청이 거부되는지 확인하세요.
$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer ***" -w "%{http_code}\n"
401
[email protected]가 발급하고 키foo의 클레임이 있는 JWT를 가져오세요.
$ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.31/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
{"exp":4685989700,"foo":"bar","iat":1532389700,"iss":"[email protected]","sub":"[email protected]"}
- 유효한 JWT가 있는 요청이 허용되는지 확인하세요.
$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer ***" -w "%{http_code}\n"
200
- 요청에 JWT 클레임 값이 있는 유효한 HTTP 헤더가 포함되는지 확인하세요.
$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -H "Authorization: Bearer ***" | jq '.headers["X-Jwt-Claim-Foo"][0]'
"bar"
정리 (Clean up)
foo 네임스페이스를 제거하세요.
$ kubectl delete namespace foo
더 알아보기 (Learn more)
- 요청 인증(RequestAuthentication) 정책에 대한 자세한 내용은 인증 정책 문서를 참고하세요.
- Istio 최종 사용자 인증 지원에 대해서는 해당 문서를 참고하세요.