I'm trying to write an idempotent script for Linux (MIT kerberos) that applies a given keytab to /etc/krb5.keytab
by merging with its existing content. On MacOS (which, I believe uses Heimdal) it's easy:
ktutil copy /tmp/ktnew /etc/krb5.keytab
If the key(s) in /tmp/ktnew
are already in /etc/krb5.keytab
then it doesn't change (this can be confirmed with before and after hashes).
The MIT version of ktutil
seems to only work interactively and does not have an equivalent to copy
. Using rkt
and wkt
appends (and, therefore, duplicates) rather than merges and is therefore not idempotent.
It it possible to do this idempotently (and non-interactively) using the MIT tools commonly found on Linux systems?
As far as I know, no. MIT Krb5 also comes with the
k5srvutil
script which relies onkadmin
's non-interactivektrem
subcommand to remove superseded keys (those with a kvno older than the latest) but that's all it has.I would suggest that merging keytabs isn't the right thing to do in the first place – rather than putting everything in the "machine" keytab (and therefore having to grant all services access to the keytab), you should at most have
host/*
andnfs/*
in there, while everything else should be using distinct keytab files. (Services which don't support specifying a keytab natively will generally supportKRB5_KTNAME=
through environment.)The deployment of keytabs would then become as simple as overwriting the whole file.
In my own projects (specifically in a tool that tries to idempotently request a key via kadmin's
ktadd
), I parse the keytab via Python to determine whether it already has a key for the principal about to be added. Here's a starting point: