Month: December 2024

  • [บันทึกกันลืม] JupyterHub Authenticated with OIDC

    ต่อจากตอนที่แล้ว [บันทึกกันลืม] JupyterHub ด้วย Docker

    คราวนี้ ถ้าต้องการให้ ยืนยันตัวตนด้วย OpenID เช่น PSU Passport เป็นต้น ก็ให้ทำดังนี้

    ในไฟล์ jupyterhub_config.py ใส่ configuration ประมาณนี้

    c = get_config()  #noqa
    c.Authenticator.allow_all = True
    c.JupyterHub.authenticator_class = "generic-oauth"
    c.JupyterHub.spawner_class = 'jupyterhub.spawner.LocalProcessSpawner'
    c.Spawner.new_user = True
    import pwd
    import subprocess
    def pre_spawn_hook(spawner):
        username = spawner.user.name
        try:
            pwd.getpwnam(username)
        except KeyError:
            import subprocess
            subprocess.check_call(['useradd', '-ms', '/bin/bash', username])
    c.Spawner.pre_spawn_hook = pre_spawn_hook
    c.Authenticator.admin_users=set(('admin'))
    c.GenericOAuthenticator.client_id = "__client__id__"
    c.GenericOAuthenticator.client_secret = "__client_secret__"
    c.GenericOAuthenticator.redirect_uri = '/oauth2/callback'
    c.GenericOAuthenticator.username_claim = 'preferred_username'
    c.GenericOAuthenticator.authorize_url = "https://IDP_URL/application/o/authorize/"
    c.GenericOAuthenticator.token_url = "https://IDP_URL/application/o/token/"
    c.GenericOAuthenticator.userdata_url = "https://IDP_URL/application/o/userinfo/"
    c.GenericOAuthenticator.scope = "openid email profile"
    c.GenericOAuthenticator.grant_type = "authorization_code"

    อธิบายเพิ่มเติมนิดนึง

    • c.JupyterHub.authenticator_class = “generic-oauth”
      ใช้ generic-oauth ในการยืนยันตัวตน
    • สร้าง pre_spawn_hook มา เพื่อให้สร้าง local user
    • c.Spawner.pre_spawn_hook = pre_spawn_hook
      จากนั้นบอก Spawner ให้ใช้ function pre_spawn_hook ข้างต้น
    • c.GenericOAuthenticator
      ต่าง ๆ เหล่านั้น เป็น config ให้ระบบไปติดต่อ IdP
    • c.GenericOAuthenticator.redirect_uri = ‘/oauth2/callback’
      อันนี้สำคัญ คือพอ Authenticate เสร็จก็ส่งมาที่นี่ เพื่อ เข้าสู่ระบบ
    • c.GenericOAuthenticator.username_claim = ‘preferred_username’
      อันนี้ ระบุว่า จะเอา field อะไรเป็น username
    • ทำตามนี้แหล่ะ ลองมาแล้ว

    จากนั้น

    docker cp jupyterhub_config.py jupyterhub01:/srv/jupyterhub/jupyterhub_config.py 
    docker restart jupyterhub01 
    docker logs -f jupyterhub01 

    ถ้าทุกอยากเรียบร้อยดี ก็จะได้หน้าจอประมาณนี้ (อันนี้ทำเร็ว ๆ ยังไม่ได้ใส่ HTTPS)

    เมื่อคลิก Sign in with OAuth2.0 ก็จะวิ่งไปหา IdP ที่ตั้งไว้ (ในที่นี้ใช้ Authentik ของหน่วยงาน ซึ่ง แยกออกจากของมหาวิทยาลัยอีกที — เดี๋ยวเขียนวิธีการทำอีกที)

    ไหลไปเรื่อย ๆ ก็จะเจอหน้า Authentication ของมหาวิทยาลัย

    แล้วก็กลับมาที่ IdP ของเรา

    จากนั้นก็เข้าใช้งาน JupyterHub ได้ตามปรกติ

    หวังว่าจะเป็นประโยขน์ครับ

  • [บันทึกกันลืม] JupyterHub ด้วย Docker

    นาน ๆ ทำที บันทึกไว้ก่อน

    รันคำสั่งนี้ เพื่อ ติดตั้ง JupyterHub รุ่นที่ทดลอง 5.2.1 ใน Docker

    docker run -d -p 8000:8000 --name jupyterhub01 quay.io/jupyterhub/jupyterhub jupyterhub
    docker exec -it jupyterhub01 bash -c "pip install jupyterlab oauthenticator"
    docker exec -it jupyterhub01 bash -c "jupyterhub --generate-config"
    docker exec -it jupyterhub01 bash -c "adduser admin"
    docker exec -it jupyterhub01 bash -c "adduser jupyteruser01"

    จะสร้าง user ชื่อ admin และ jupyteruser01 โดย admin จะเป็น Administrator ส่วน jupyteruser01 ให้เป็น user ทั่วไป ตรงนี้จะสร้าง user อยู่ภายใน docker container (ขั้นตอนนี้จะมี prompt ให้ตั้งรหัสผ่านของ admin และ jupyteruser01 ตามลำดับ)

    container นี้ จะชื่อว่า ‘jupyterhub01’ เข้าถึงได้ที่ port 8000 (หากต้องการเปลี่ยนก็ทำได้)

    จานั้น copy file จากข้างใน docker ออกมา

    docker cp jupyterhub01:/srv/jupyterhub/jupyterhub_config.py .

    จากนั้น แก้ไข jupyterhub_config.py อย่างน้อย ต้องมีการตั้งค่าดังนี้

    c = get_config()  #noqa
    c.JupyterHub.authenticator_class = 'jupyterhub.auth.PAMAuthenticator'
    c.Authenticator.admin_users = ('admin')
    c.Authenticator.allow_all = True

    บรรทัดล่างสุดนั้น เป็นการอนุญาตให้ทุกคนที่เข้าใช้งานได้ และให้ ‘admin’ มีหน้าที่เป็น Admin

    จากนั้น ให้ copy file นี้ เข้าไปใน Docker ด้วยคำสั่ง

    docker cp jupyterhub_config.py jupyterhub01:/srv/jupyterhub/jupyterhub_config.py
    docker restart jupyterhub01

    ซึ่งคนที่เป็น admin จะมีหน้า Admin ให้ใช้งาน จากการคลิก File > Control Panel > Admin หน้าตาดังภาพ

    admin สามารถเข้าไปใน Session ของผู้ใช้คนอื่นได้ เหมาะสำหรับใช้กับงานด้าน การเรียนการสอน

    หวังว่าจะเป็นประโยชน์ครับ