如何强制 'docker login' 命令忽略现有的凭证助手?
How to force 'docker login' command to ignore existing credentials helper?
我有一个系统,我正在尝试 运行 docker login
命令,它是一个无头 linux 系统,但不幸的是只有 Docker Credentials Helper docker-credential-secretservice 已安装。
这意味着我收到以下错误:
Error saving credentials: error storing credentials - err: exit status 1, out: `Cannot autolaunch D-Bus without X11 $DISPLAY`
我认为这是有道理的:
By default, Docker looks for the native binary on each of the
platforms, i.e. “osxkeychain” on macOS, “wincred” on windows, and
“pass” on Linux. A special case is that on Linux, Docker will fall
back to the “secretservice” binary if it cannot find the “pass”
binary. If none of these binaries are present, it stores the
credentials (i.e. password) in base64 encoding in the config files
described above.
并且由于 secretservice
助手使用 GUI 凭据存储,它会尝试打开 window,这在无头系统上是无法做到的。
我无法控制系统,所以我无法删除 /usr/bin/docker-credential-secretservice
文件以强制 docker login
回退到配置文件而不是使用 secretservice
帮手。
我能做的是在用户的主文件夹中创建和列出文件。我试过 运行 这样的命令:
docker --config ./docker login -u <user-name> -p <password> <repository>
我的印象是登录命令然后会在 ./docker 中创建一个 config.json
(我注意到 docker login
将创建文件夹,如果它不不存在)。这适用于未安装任何助手的系统,但不适用于有问题的系统。
我还尝试创建一个 ~/.docker/config.json
类似的东西:
echo '{"credStore":""}' > ~/.docker/config.json
希望 docker login
得到提示,不要为凭据存储使用任何助手。
有没有办法让非管理员强制 docker login
回退到:
stores the credentials (i.e. password) in base64 encoding in the config files described above.
Without deleting the credentials helper?
(作为旁注,我当然会要求删除 /usr/bin/docker-credential-secretservice
,但是,如果不可能或为了将来参考,是否有其他解决方案?)
不幸的是,Docker(截至 18.06)首先查找 docker-credential-*
二进制文件,如果找到其中任何一个,它将自动覆盖 "credsStore"
中的值 ~/.docker/config.json
.
您唯一的解决方法是在您的主目录中安装 docker-credential-pass
,这样 Docker 将使用它而不是 docker-credential-secretservice
。 docker-credential-pass
不需要 GUI。
安装步骤docker-credential-pass
:
docker login fails on a server with no X11 installed
要避免使用 credsStore
并在 docker 配置中存储明文身份验证令牌(例如 ~/.docker/config.json
),请从 [=] 中删除 "credsStore"
密钥29=] 配置文件并重新运行 docker login
.
当你运行 docker login
时,它会发出警告,但会将授权令牌保存到文件中。
$ docker login
Username: someuser
Password:
WARNING! Your password will be stored unencrypted in ~/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
生成的 docker 配置文件应如下所示:
{
"auths": {
"your.docker.registry": {
"auth": "dXNlcm5hbWU6cGFzc3dvcmQK="
}
}
}
auth
标记只是一个形式为 username:password
.
的 base64 编码字符串
这适用于 Docker 引擎版本 19 和 20。
您可以通过将所有内容发送到 /dev/null
来忽略所有输出,例如:
echo $TOKEN|docker login -u=user --password-stdin myregistry.com > /dev/null 2>&1
其中 $TOKEN
将是您之前导出的令牌或密码。
这对于 CI 在使用一些自动化时也很有用
在使用不同的用户名登录之前注销当前用户对我有用。注销删除了已保存的 docker 凭据。
docker logout <reponame>
docker login <reponame>
我查看了代码,似乎没有办法普遍禁用凭据助手。 但是您可以跳过每个注册表的代码路径。
https://github.com/docker/cli/blob/25eee83d6b8c475548254b2decc9c8e0490d229c/cli/config/configfile/file.go#L308 将从注册表主机名中查找 credHelpers
中要使用的助手(只是 docker-credential-
之后的后缀)。如果是 ""
它将使用正常的未加密文件存储。据我所知,这是未记录且可能是意外的行为。
这样,为了绕过特定注册表的凭据助手,请执行
mkdir ./docker
echo "{\"credHelpers\": {\"$REGISTRY_HOST\": \"\"}}" > ./docker/config.json
docker --config ./docker login -u $USERNAME -p $PASSWORD $REGISTRY_HOST
我认为 {"credsStore":""}
不起作用的原因是该字段上的 omitempty
序列化标记。设置为空和不设置一样。
不是问题的答案,但可能是问题的答案:
我们可以自己启动 dbus,然后 unlock/lock/query 来自 cli 的密钥环。
这些是我使用的 bash 函数:
function unlock-keyring () {
export $(dbus-launch)
read -rsp "Password: " pass
export $(echo -n "$pass" | gnome-keyring-daemon --unlock)
unset pass
}
function lock-keyring () {
dbus-send --dest=org.gnome.keyring --print-reply /org/freedesktop/secrets org.freedesktop.Secret.Service.LockService
}
function query-keyring-locked () {
busctl --user get-property org.freedesktop.secrets /org/freedesktop/secrets/collection/login org.freedesktop.Secret.Collection Locked
}
https://superuser.com/questions/700826/how-to-lock-a-unlocked-gnome-keyring
https://superuser.com/questions/1618970/query-status-of-gnome-keyring
https://unix.stackexchange.com/questions/602313/unlock-gnome-keyring-daemon-from-command-line
我有一个系统,我正在尝试 运行 docker login
命令,它是一个无头 linux 系统,但不幸的是只有 Docker Credentials Helper docker-credential-secretservice 已安装。
这意味着我收到以下错误:
Error saving credentials: error storing credentials - err: exit status 1, out: `Cannot autolaunch D-Bus without X11 $DISPLAY`
我认为这是有道理的:
By default, Docker looks for the native binary on each of the platforms, i.e. “osxkeychain” on macOS, “wincred” on windows, and “pass” on Linux. A special case is that on Linux, Docker will fall back to the “secretservice” binary if it cannot find the “pass” binary. If none of these binaries are present, it stores the credentials (i.e. password) in base64 encoding in the config files described above.
并且由于 secretservice
助手使用 GUI 凭据存储,它会尝试打开 window,这在无头系统上是无法做到的。
我无法控制系统,所以我无法删除 /usr/bin/docker-credential-secretservice
文件以强制 docker login
回退到配置文件而不是使用 secretservice
帮手。
我能做的是在用户的主文件夹中创建和列出文件。我试过 运行 这样的命令:
docker --config ./docker login -u <user-name> -p <password> <repository>
我的印象是登录命令然后会在 ./docker 中创建一个 config.json
(我注意到 docker login
将创建文件夹,如果它不不存在)。这适用于未安装任何助手的系统,但不适用于有问题的系统。
我还尝试创建一个 ~/.docker/config.json
类似的东西:
echo '{"credStore":""}' > ~/.docker/config.json
希望 docker login
得到提示,不要为凭据存储使用任何助手。
有没有办法让非管理员强制 docker login
回退到:
stores the credentials (i.e. password) in base64 encoding in the config files described above. Without deleting the credentials helper?
(作为旁注,我当然会要求删除 /usr/bin/docker-credential-secretservice
,但是,如果不可能或为了将来参考,是否有其他解决方案?)
不幸的是,Docker(截至 18.06)首先查找 docker-credential-*
二进制文件,如果找到其中任何一个,它将自动覆盖 "credsStore"
中的值 ~/.docker/config.json
.
您唯一的解决方法是在您的主目录中安装 docker-credential-pass
,这样 Docker 将使用它而不是 docker-credential-secretservice
。 docker-credential-pass
不需要 GUI。
安装步骤docker-credential-pass
:
docker login fails on a server with no X11 installed
要避免使用 credsStore
并在 docker 配置中存储明文身份验证令牌(例如 ~/.docker/config.json
),请从 [=] 中删除 "credsStore"
密钥29=] 配置文件并重新运行 docker login
.
当你运行 docker login
时,它会发出警告,但会将授权令牌保存到文件中。
$ docker login
Username: someuser
Password:
WARNING! Your password will be stored unencrypted in ~/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
生成的 docker 配置文件应如下所示:
{
"auths": {
"your.docker.registry": {
"auth": "dXNlcm5hbWU6cGFzc3dvcmQK="
}
}
}
auth
标记只是一个形式为 username:password
.
这适用于 Docker 引擎版本 19 和 20。
您可以通过将所有内容发送到 /dev/null
来忽略所有输出,例如:
echo $TOKEN|docker login -u=user --password-stdin myregistry.com > /dev/null 2>&1
其中 $TOKEN
将是您之前导出的令牌或密码。
这对于 CI 在使用一些自动化时也很有用
在使用不同的用户名登录之前注销当前用户对我有用。注销删除了已保存的 docker 凭据。
docker logout <reponame>
docker login <reponame>
我查看了代码,似乎没有办法普遍禁用凭据助手。 但是您可以跳过每个注册表的代码路径。
https://github.com/docker/cli/blob/25eee83d6b8c475548254b2decc9c8e0490d229c/cli/config/configfile/file.go#L308 将从注册表主机名中查找 credHelpers
中要使用的助手(只是 docker-credential-
之后的后缀)。如果是 ""
它将使用正常的未加密文件存储。据我所知,这是未记录且可能是意外的行为。
这样,为了绕过特定注册表的凭据助手,请执行
mkdir ./docker
echo "{\"credHelpers\": {\"$REGISTRY_HOST\": \"\"}}" > ./docker/config.json
docker --config ./docker login -u $USERNAME -p $PASSWORD $REGISTRY_HOST
我认为 {"credsStore":""}
不起作用的原因是该字段上的 omitempty
序列化标记。设置为空和不设置一样。
不是问题的答案,但可能是问题的答案:
我们可以自己启动 dbus,然后 unlock/lock/query 来自 cli 的密钥环。
这些是我使用的 bash 函数:
function unlock-keyring () {
export $(dbus-launch)
read -rsp "Password: " pass
export $(echo -n "$pass" | gnome-keyring-daemon --unlock)
unset pass
}
function lock-keyring () {
dbus-send --dest=org.gnome.keyring --print-reply /org/freedesktop/secrets org.freedesktop.Secret.Service.LockService
}
function query-keyring-locked () {
busctl --user get-property org.freedesktop.secrets /org/freedesktop/secrets/collection/login org.freedesktop.Secret.Collection Locked
}
https://superuser.com/questions/700826/how-to-lock-a-unlocked-gnome-keyring
https://superuser.com/questions/1618970/query-status-of-gnome-keyring
https://unix.stackexchange.com/questions/602313/unlock-gnome-keyring-daemon-from-command-line