Profile picture

[Docker] 개인 인증 서버 구축하기 - Authentik

JaehyoJJAng2023년 05월 16일

◾️ Authentik

image


오픈소스 인증 프로젝트인 Authentik에 대해 자세히 알아보자.

Authentik은 로그인 처리가 없는 특정 페이지를 진입할 때 인증서버/페이지를 거쳐서 인증을 거치게 한 뒤에 진행하고자 하는 페이지로 리다이렉트 시켜준다.

보통 도커로 띄운 앱들 중에 로그인이 없는 앱들이 다수 있는데(dozzle,metube ..) 해당 앱들에 Authentik,Authelia 같은 인증 서버를 연동해주면 매우 간단하게 보안을 한층 더 높여줄 수 있다.

유사한 프로젝트로 **Authelia**가 있는데,
Authentik은 authelia 보다 관리 콘솔이 깔끔하고 확장성이 매우 강하여 개인적으로는 Authentik이 더 낫다고 생각한다.


▪️ 사전 준비

  • 구동 환경: Ubuntu 22.04
  • Docker / Docker Compose 설치 필수

1. NPM 구축

NPM(Nginx Proxy Manager) 구축하기


2. 서브 도메인 생성

Nginx Proxy Manager에 리버스 프록시를 위하여 authentik으로 들어오는 서브 도메인을 따로 생성해줄 것이다.


먼저 가비아 DNS 관리 페이지로 가자.
image
설정하고자 하는 도메인 지정 후 'DNS 설정' 클릭.


아래처럼 서브 도메인을 만들어주자.
image


▪️ Authentik 구축

1. Docker Compose 파일 다운로드

curl -L -O https://goauthentik.io/docker-compose.yml

2. 암호화 패키지 설치

sudo apt-get install -y pwgen

3. 아래 명령을 실행하여 Docker Compose에 사용될 환경 변수 값을 암호화하여 생성.

echo "PG_PASS=$(pwgen -s 40 1)" | tee .env
echo "AUTHENTIK_SECRET_KEY=$(pwgen -s 50 1)" | tee -a .env

4. Docker Compose 실행

docker-compose up -d --build
docker-compose ps
NAME                     COMMAND                  SERVICE             STATUS               PORTS
authentik-postgresql-1   "docker-entrypoint.s…"   postgresql          running (starting)   5432/tcp
authentik-redis-1        "docker-entrypoint.s…"   redis               running (starting)   6379/tcp
authentik-server-1       "dumb-init -- ak ser…"   server              running (starting)   0.0.0.0:9000->9000/tcp, 0.0.0.0:9443->9443/tcp, :::9000->9000/tcp, :::9443->9443/tcp
authentik-worker-1       "dumb-init -- ak wor…"   worker              running (starting)

컨테이너가 모두 생성됐으면 9000번 포트로 접속해보자.
(http://<SERVER_IP:9000/if/flow/initial-setup/)
image
기본 구성은 완료되었다!


▪️ Proxy Hosts 등록

먼저 현재 구동되어 있는 NPM 어드민 페이지의 Proxy Hosts로 이동하자.
image


아까 #2 서브 도메인 생성섹션에서 생성했었던 서브 도메인으로 Proxy Hosts를 생성해주자.
image


도메인으로 아래처럼 정상적으로 접근되는지 확인해보자. (http://authentik.waytothem.store)
image


▪️ 애플리케이션 등록

Authentik 로그인 후 우측 상단에 어드민 패널로 진입하자.
image
image
image
위 단계를 따라서 동일하게 선택해주면 된다.


1. provider 등록

여기서 external host에는 authentik을 적용할 도메인을 지정해주자.
나의 경우에는 이전에 만든 dozzle(https://dozzle.waytothem.store) 애플리케이션 도메인에 authentik을 적용할 것이다.
image


2. Application 지정

애플리케이션 생성 페이지로 넘어가자.
image


위에서 만들었던 provider를 지정해주면 된다.
image


3. Outpost 지정

마지막으로 outpost를 지정해주자.
기본으로 하나가 만들어져 있기 때문에 오른쪽에 연필 모양을 눌러 수정을 해주면 된다.
image


위에서 만든 애플리케이션을 체크하고, authentik_host에는 인증 도메인을 넣어주자.
image


▪️ Proxy 항목 편집

이제 NPM에서 호스트에 설정을 해줄 차례이다.
NPM 서버에 로그인 하고 'Proxy Hosts' 페이지로 들어가 애플리케이션의 프록시 항목을 편집해주자.
image


아래는 설정 내용이다.
proxy_pass http://auth.example.com/outpost.goauthentik.io; 항목을 아래처럼 구동 중인 호스트:포트로 수정 해주고 저장 해주면 끝이다.
-> proxy_pass http://waytothem.store:9000/outpost.goauthentik.io; 또는 proxy_pass http://3.44.0.100:9000/outpost.goauthentik.io;

# Increase buffer size for large headers
# This is needed only if you get 'upstream sent too big header while reading response
# header from upstream' error when trying to access an application protected by goauthentik
proxy_buffers 8 16k;
proxy_buffer_size 32k;

location / {
    # Put your proxy_pass to your application here
    proxy_pass          $forward_scheme://$server:$port;

    # authentik-specific config
    auth_request        /outpost.goauthentik.io/auth/nginx;
    error_page          401 = @goauthentik_proxy_signin;
    auth_request_set $auth_cookie $upstream_http_set_cookie;
    add_header Set-Cookie $auth_cookie;

    # translate headers from the outposts back to the actual upstream
    auth_request_set $authentik_username $upstream_http_x_authentik_username;
    auth_request_set $authentik_groups $upstream_http_x_authentik_groups;
    auth_request_set $authentik_email $upstream_http_x_authentik_email;
    auth_request_set $authentik_name $upstream_http_x_authentik_name;
    auth_request_set $authentik_uid $upstream_http_x_authentik_uid;

    proxy_set_header X-authentik-username $authentik_username;
    proxy_set_header X-authentik-groups $authentik_groups;
    proxy_set_header X-authentik-email $authentik_email;
    proxy_set_header X-authentik-name $authentik_name;
    proxy_set_header X-authentik-uid $authentik_uid;
}

# all requests to /outpost.goauthentik.io must be accessible without authentication
location /outpost.goauthentik.io {
    proxy_pass          https://authentik.waytothem.store/outpost.goauthentik.io;
    # ensure the host of this vserver matches your external URL you've configured
    # in authentik
    proxy_set_header    Host $host;
    proxy_set_header    X-Original-URL $scheme://$http_host$request_uri;
    add_header          Set-Cookie $auth_cookie;
    auth_request_set    $auth_cookie $upstream_http_set_cookie;

    # required for POST requests to work
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
}

# Special location for when the /auth endpoint returns a 401,
# redirect to the /start URL which initiates SSO
location @goauthentik_proxy_signin {
    internal;
    add_header Set-Cookie $auth_cookie;
    return 302 /outpost.goauthentik.io/start?rd=$request_uri;
    # For domain level, use the below error_page to redirect to your authentik server with the full redirect path
    # return 302 https://authentik.company/outpost.goauthentik.io/start?rd=$scheme://$http_host$request_uri;
}

▪️ Authentik 테스트

위 단계를 끝내고 애플리케이션(dozzle.waytothem.store)로 접속하면 아래처럼 Authentik 로그인 창으로 리다이렉트 된다.
image


Loading script...