I couldn't quite figure out what is wrong with the code as written, but the properties in modAuthorizedKey relating to the file modes/ownership get applied before the properties to create the directory and file are applied, so if they don't already exist then you get an error.
Indeed, I think I've fixed it by making the permissions fixup come
after
the property that creates the file, rather thanbefore
.Making a host key known to a brand new user
requires
the owner of a nonexistent file to be set, if I understand the code correctly. Removing the "requires"-lines from the function modKnownHost makes the problem go away, but that's probably not the correct solution.I see it; changed it to use
before
so the file creation/modification comes before any chmodding.