当缓存仍具有凭据时避免kinit

时间:2019-03-26 23:51:02

标签: linux kerberos systemd mit-kerberos

我有一个systemd服务,该服务调用Web服务定期(每分钟)执行一些维护。服务看起来像:

[Service]
Type=oneshot
ExecStart=/usr/bin/kinit -kt user.keytab user@DOMAIN
ExecStart=/usr/bin/curl --tlsv1.2 --cacert cert.pem --negotiate --user user: --url https://website/maintenance

现在,每次销毁都会销毁并重新初始化我的kerberos票。 初始化可能需要2-3分钟。

我想避免该步骤,仅在需要时kinit。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

尝试HTTP请求,然后使用状态码来确定是否需要尝试kinit。您可以像这样复制curl的输出:

curl -s -i http://www.example.com | grep "HTTP/" | tail -1

如果它是“ HTTP / 1.1 401未经授权”,请运行kinit并重试。 (如果愿意,请参见here,了解如何仅解析响应的数字部分)

“ tail -1”部分是确保仅获得最后的代码;由于采用了协商协议,因此通常会从grep命令获得多行,例如:

HTTP/1.1 401 Unauthorized
HTTP/1.1 200 OK

第一个是来自服务器的最初挑战;第二个是最终响应代码。

答案 1 :(得分:0)

经过更多研究后,我意识到在systemd服务中使用逻辑似乎不是一个好主意。因此,我决定接受Elliott Frisch的建议并为其创建脚本:

#!/bin/bash
# check if ticket is present and not expired
if [[ $(klist -l | awk 'tolower($0) ~ /user/ && tolower($0) !~ /expired/') ]]; then
    echo "using ticket cache"
else
    echo "no cache authentication for user, kinit needed"
    /usr/bin/kinit -kt /user.keytab user@DOMAIN
fi
/usr/bin/curl --tlsv1.2 --cacert cert.pem --negotiate --user user: --url https://website/maintenance

然后我在我的systemd服务中调用此脚本