某一次harbor服務重啟后無法登陸,密碼肯定是正確的,但就是無法登陸,由於是內網部署,也無法使用郵箱找回之類的措施,所以就直接在harbor數據庫中修改密碼。 以下為筆記內容 。
毫無疑問我們只能進harbor的后台mysql進行修改,但是查資料發現,這個harbor中的mysql的密碼是采用pbkdf2算法,調用的Hash函數為Sha1,迭代4096次,密鑰長度為int型16位得出的,所以你常規的用明文密碼去update是不行的,必須要通過算法將密鑰算出來,然后update可以成功。
下面是密鑰計算算法,計算明文為123QWEqwe, 鹽值為gktqer4zml32472wmht9xeuixvg5pvjd, 迭代次數為4096, 密鑰長度int型16位
運行環境為python2 版本, 注: python 3的模塊名稱已經修改,運行的時候會報錯。
import hmac import hashlib from struct import Struct from operator import xor from itertools import izip, starmap _pack_int = Struct('>I').pack def pbkdf2_hex(data, salt, iterations=4096, keylen=16, hashfunc=None): return pbkdf2_bin(data, salt, iterations, keylen, hashfunc).encode('hex') def pbkdf2_bin(data, salt, iterations=4096, keylen=16, hashfunc=None): hashfunc = hashfunc or hashlib.sha1 mac = hmac.new(data, None, hashfunc) def _pseudorandom(x, mac=mac): h = mac.copy() h.update(x) return map(ord, h.digest()) buf = [] for block in xrange(1, -(-keylen // mac.digest_size) + 1): rv = u = _pseudorandom(salt + _pack_int(block)) for i in xrange(iterations - 1): u = _pseudorandom(''.join(map(chr, u))) rv = starmap(xor, izip(rv, u)) buf.extend(rv) return ''.join(map(chr, buf))[:keylen] rv = pbkdf2_hex('123QWEqwe', 'gktqer4zml32472wmht9xeuixvg5pvjd', 4096, 16) print(rv)
修改好明文密碼和salt值后直接運行python文件
# python xx.py
500026b9f02e84d1f41e7546b9b2d524
現在開始修改harbor密碼
docker exec -it b07b3206fea5 /bin/bash
psql (9.6.14)
Type "help" for help.
postgres=# help
You are using psql, the command-line interface to PostgreSQL.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
postgres=# psql -U postgres -d postgres -h 127.0.0.1 -p 5432 # 進入psttsql客戶端
postgres=# \c registry #進入registry 數據庫
You are now connected to database "registry" as user "postgres".
postgres=# select * from harbor_user; #查詢所有用戶
#更新用戶密碼 password會加密后的密碼,salt為鹽值,按select查詢結果中顯示的為准
postgres=# update harbor_user set password='500026b9f02e84d1f41e7546b9b2d524', salt='oafrcwi1rh83bem3cnfldltaw4cf9pqm' where username='admin';
postgres=# \q #退出postsql數據庫
修改后重新啟動一下harbor-db這個cainte既可看到效果