JupyterHub Authentication
Jupyerhub初始的auth type是dummy, 就是個偽認證莲绰,輸任意密碼就能登錄(可以通過修改config.yaml的auth:dummy:password改掉)翠勉,此外還支持OAuth2、LDAP的方式陨晶,可以通過GitHub、google帝璧、CILogon等賬號認證先誉,這些配置方式在官方文檔介紹的還蠻詳細的〉乃福基于常見數(shù)據(jù)庫進行身份驗證的官方資料沒找到褐耳,網(wǎng)上找了一些博客,寫了個mysql賬號密碼認證的模塊加到鏡像里去渴庆。
mysqauthenticator包
寫一個mysqlauthenticator的py包铃芦,大致功能就是連接數(shù)據(jù)庫驗證賬號密碼的常規(guī)操作。具體代碼
class MysqlAuthenticator(Authenticator):
"""JupyterHub Authenticator Based on Mysql"""
def __init__(self, **kwargs):
super(MysqlAuthenticator, self).__init__(**kwargs)
@gen.coroutine
def authenticate(self, handler, data):
db_url = "mysql+mysqlconnector://root:root@192.168.199.182:3306/jupyter"
session = init(db_url)
username = data['username']
passwd = data['password']
try:
user = session.query(User).filter(User.username == username).filter(User.password == passwd).one()
if user is not None:
return user.username
else:
return None
except:
return None
還要建一個mysql的服務襟雷,我直接開了個docker刃滓,建jupyter數(shù)據(jù)庫,建user表耸弄,id
username
password
三個字段咧虎。插入一些測試數(shù)據(jù)。
制作新的鏡像
原來的Dockerfile我還沒研究计呈,就直接在原有的容器里加入新的內(nèi)容砰诵,然后再commit成新的鏡像征唬。
# 首先啟動jupyterhub服務
# 在k8s-hub容器運行的節(jié)點,進入容器先安裝一些依賴的模塊
docker exec -it 6db8b54b25b8 bash
# 安裝wheel茁彭、sqlalchemy总寒、mysql-connector
pip3 install wheel
pip3 install sqlalchemy
pip3 install mysql-connector
# 退出容器,把mysqlauthenticator包拷貝到pip3的下載目錄
# 注意路徑尉间,要不然import不進去偿乖。在容器里是/home/jovyan/.local/lib/python3.6/site-packages/
docker cp /root/myspace/workspace/jupyter/jupter_auth_mysql/mysqlauthenticator 6db8b54b25b8:/home/jovyan/.local/lib/python3.6/site-packages/
# 打tag 這里我傳到自己搭建的docker倉庫里去,方便其他機子pull
docker tag 6db8b54b25b8 192.168.199.182:5000/jupyterhub/k8s-hub:v0.1.0
# push
docker push 192.168.199.182:5000/jupyterhub/k8s-hub:v0.1.0
修改config.yaml
首先把hub的鏡像改掉
hub:
image:
name: 192.168.199.182:5000/jupyterhub/k8s-hub
tag: v0.1.0
然后改掉auth哲嘲, 一處是改auth:type為custom贪薪,然后配置custom的class,這樣源碼里就會import上面的mysqlauthenticator.MysqlAuthenticator這個類眠副。我還配了一個admin權(quán)限的user画切,這個admin user的控制面板會有一個簡單的管理頁面。
auth:
type: custom
custom:
className: mysqlauthenticator.MysqlAuthenticator
whitelist:
users:
admin:
access: true
users:
- wenxinax
然后重啟一下jupyterhub服務就行了囱怕。輸入的賬號密碼沒有通過數(shù)據(jù)庫驗證的話提示 Invalid username or password 就算成功了霍弹。