dexidp/dex 및 AuthProxy Connector 테스트

9560 단어 OIDCdextech

TL;DR

  • Dex의 AuthProxy connector를 사용하면 ID token
  • 을 발행하기 위해basic 인증을 사용할 수 있습니다.
  • AuthProxy를 사용하려면nginx, appache 등 역방향 에이전트가 필요합니다.
  • /dex/calllback/: 인증된 id의 요청이 정확하면 X-Remote-User 헤더 추가
  • 개시하다


    dexidp/dex의connector를 봤는데 AuthProxy를 사용하면 Basic 인증을 받은 사용자에게 ID token을 발행할 수 있다고 생각합니다.
    문서에는 구체적인 흐름도가 없다. 손 옆에 테스트를 하기 전에 시간이 오래 걸렸기 때문에 재현의 상세한 절차를 적어야 한다.
    앞부분은 Getting Started의 설명이기 때문에 AuthProxy를 보고 싶으신 분들은 뒷부분부터 읽어주세요.

    dex 사용 방법


    dexidp.IO의Getting Started를 바탕으로 설명합니다.

    차리다


    $ git clone https://github.com/dexidp/dex.git
    $ cd dex/
    $ make build # dex 本体がビルドされ ./bin/dex に配置されます。
    
    검증을 위한 응용 프로그램도 만들 것입니다.
    $ make examples # example-app がビルドされ ./bin/example-app に配置されます
    
    검증용 example-app를 테스트하기 위해 준비된 config 파일을 dex에 제공한 후 시작해야 합니다.
    $ ./bin/dex serve examples/config-dev.yaml
    
    또한 다른 컨트롤러를 사용하여 example-app를 시작합니다.
    $ ./bin/example-app
    

    영패 획득


    다음 단계에서는 dex를 사용하여 ID 토큰을 가져오는 프로세스를 경험할 수 있습니다.

  • http://localhost:5555에서 기다리는 example-app
  • 방문
  • login를 누르면 dex
  • 로 리디렉션됩니다.
  • dex의 화면에서 인증 방법을 선택
  • Log in with Email

  • 각각 [email protected], password
  • examples/config-dev.yaml에 인증 정보
  • Log in With Example
  • 모듈식 내부 사용자 데이터 활용

  • 클릭Grant Access, 방문 승인
  • 획득한 ID 토큰
  • example-app에 표시
    다음은 실제 방문을 바탕으로 한 흐름도입니다.

    AuthProxy 설정 방법


    이번에는 인증 방법에서 AuthProxy를 사용하여 Basic 인증을 추가합니다.
    AuthProxy 문서
    먼저 프로세스를 다음과 같이 표시합니다.
    여기서 AuthProxy를 사용하기 위해 X-Remote-User 헤드의 지정은 필요하다임을 알 수 있다.
    따라서 /dex/callback/basicauth에서 온 요청에 대해basic인증을 실시하고 결과에 따라 X-Remote-User 헤더의 프록시 서버를 설정해야 한다.

    프록시 구성


    문서는 Apache2의 예를 보여 주었고 이번에는 Nginx로 구성되었다.
    nginx를 역 프록시 서버로 설정하는 데 필요한 설정은 다음과 같습니다.
    $ htpasswd -c -b htpasswd test password
    Adding password for user test
    
    $ ls
    nginx.conf htpasswd
    
    $ cat nginx.conf
    user nginx;
        worker_processes  3;
        error_log  /var/log/nginx/error.log;
        events {
          worker_connections  10240;
        }
    
        http {
          access_log    /dev/stdout;
          error_log     /dev/stderr  warn;
          
          server {
            listen 80;
            location /dex/callback/myBasicAuth {
              auth_basic "basic auth";
              auth_basic_user_file /etc/nginx/htpasswd;
              set $user "[email protected]";
    
              proxy_set_header X-Forwarded-Host $host:$server_port;
              proxy_set_header X-Forwarded-Server $host;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header X-Remote-User $user;
              proxy_pass "http://$HOST_IP:5556/dex/callback/myBasicAuth";
            }
    
            location /dex/ {
              proxy_set_header X-Forwarded-Host $host:$server_port;
              proxy_set_header X-Forwarded-Server $host;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_pass "http://$HOST_IP:5556/dex/";
            }
          }
        }
    
    docker로 시작합니다.
    $ docker run -it --rm --name nginx -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf -v $(pwd)/htpasswd:/etc/nginx/htpasswd -p 8080:80 nginx
    
    dex의config도 변경이 필요합니다.
    두 가지 변경점이 있습니다.
  • issuer를 localhost:8080/dex
  • 로 변경
  • Connector에 AuthProxy 추가
  • id는 무엇이든지 가능하지만,nginx가 지정한/dex/calllback/:id와 협조해야 합니다.
  • $ git diff examples/config-dev.yaml
    diff --git a/examples/config-dev.yaml b/examples/config-dev.yaml
    index 6cae823c..5441c587 100644
    --- a/examples/config-dev.yaml
    +++ b/examples/config-dev.yaml
    @@ -4,8 +4,9 @@
     # The base path of dex and the external name of the OpenID Connect service.
     # This is the canonical URL that all clients MUST use to refer to dex. If a
     # path is provided, dex's HTTP service will listen at a non-root URL.
    -issuer: http://127.0.0.1:5556/dex
    +#issuer: http://127.0.0.1:5556/dex
    +issuer: http://localhost:8080/dex
     # The storage configuration determines where dex stores its state. Supported
     # options include SQL flavors and Kubernetes third party resources.
     #
    @@ -120,6 +121,9 @@ connectors:
     - type: mockCallback
       id: mock
       name: Example
    +- type: authproxy
    +  id: myBasicAuth
    +  name: myBasicAuth
     # - type: google
     #   id: google
    

    Basic 인증을 통해 토큰을 획득해 보십시오.


    설정이 끝난 후 dex와nginx는 각각 다른 컨트롤러로 시작합니다.
    $ docker run -it --rm --name nginx -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf -v $(pwd)/htpasswd:/etc/nginx/htpasswd -p 8080:80 nginx
    /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
    /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
    /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
    10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
    10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
    /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
    /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
    /docker-entrypoint.sh: Configuration complete; ready for start up
    
    $ ./bin/dex serve examples/config-dev.yaml
    time="2022-04-27T03:12:07Z" level=info msg="Dex Version: fd15dd2248900a16849af9513d9a70c0a0e5103f-dirty, Go Version: go1.17.7, Go OS/ARCH: darwin arm64"
    time="2022-04-27T03:12:07Z" level=info msg="config issuer: http://localhost:8080/dex"
    time="2022-04-27T03:12:07Z" level=info msg="config storage: sqlite3"
    time="2022-04-27T03:12:07Z" level=info msg="config static client: Example App"
    time="2022-04-27T03:12:07Z" level=info msg="config connector: mock"
    time="2022-04-27T03:12:07Z" level=info msg="config connector: myBasicAuth"
    time="2022-04-27T03:12:07Z" level=info msg="config connector: local passwords enabled"
    time="2022-04-27T03:12:07Z" level=info msg="config refresh tokens rotation enabled: true"
    time="2022-04-27T03:12:07Z" level=info msg="listening (telemetry) on 0.0.0.0:5558"
    time="2022-04-27T03:12:07Z" level=info msg="listening (http) on 0.0.0.0:5556"
    
    이외에 example-app도 issuer를 시작할 수 있습니다.
    $ ./bin/example-app --issuer http://localhost:8080/dex
    2022/04/27 12:26:00 listening on http://127.0.0.1:5555
    
    이 상태에서localhost:5555에 방문하고 Login->Log in with myBasicAuth를 누르면basic인증을 요구합니다.

    test:password를 입력하고 인증 화면에 들어가면 ID token 표시가 성공합니다🎉

    시험을 준비하다


    dex의 설정에는 skipApprovalScreen이 있습니다.
    설정skipApprovalScreen: true을 통해 이전 절차의 승인 화면을 건너뛰고 인증 코드를 직접 획득할 수 있습니다.
    이 방법을 사용하면 curl 등 CLI 액세스에서도 인증 코드를 받을 수 있습니다.
    connector 덕분에 브라우저에서 SSO를 얻고, CLI 접근은basic auth를 사용하여 각각 idtoken을 얻는 구성도 고려할 수 있다.

    좋은 웹페이지 즐겨찾기