Skip to content

Commit 0a00491

Browse files
committed
add support for administrators_authorized_keys to SetUserSSHPublicKeysPlugin
see https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement#administrative-user
1 parent c4ce26a commit 0a00491

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

cloudbaseinit/plugins/common/sshpublickeys.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
CONF = cloudbaseinit_conf.CONF
2626
LOG = oslo_logging.getLogger(__name__)
2727

28+
# The default Win32-OpenSSH config assumes that the built-in Administrators
29+
# group with SID S-1-5-32-544 does not have an internationalized name.
30+
ADMINISTRATORS = "Administrators"
31+
2832

2933
class SetUserSSHPublicKeysPlugin(base.BasePlugin):
3034

@@ -49,10 +53,31 @@ def execute(self, service, shared_data):
4953
os.makedirs(user_ssh_dir)
5054

5155
authorized_keys_path = os.path.join(user_ssh_dir, "authorized_keys")
52-
LOG.info("Writing SSH public keys in: %s" % authorized_keys_path)
53-
with open(authorized_keys_path, 'w') as f:
54-
for public_key in public_keys:
55-
# All public keys are space-stripped.
56-
f.write(public_key + "\n")
56+
authorized_keys_files = [authorized_keys_path]
57+
58+
admin_membership_conditions = (
59+
osutils.group_exists(ADMINISTRATORS),
60+
ADMINISTRATORS in CONF.groups
61+
)
62+
63+
if all(admin_membership_conditions):
64+
program_data_dir = os.getenv("PROGRAMDATA", "C:\ProgramData")
65+
LOG.debug("Program Data: %s" % program_data_dir)
66+
67+
program_data_ssh_dir = os.path.join(program_data_dir, "ssh")
68+
if not os.path.exists(program_data_ssh_dir):
69+
os.makedirs(program_data_ssh_dir)
70+
71+
administrators_authorized_keys_path = os.path.join(
72+
program_data_ssh_dir, "administrators_authorized_keys"
73+
)
74+
authorized_keys_files.append(administrators_authorized_keys_path)
75+
76+
for filepath in authorized_keys_files:
77+
LOG.info("Writing SSH public keys in: %s" % filepath)
78+
with open(filepath, 'w') as f:
79+
for public_key in public_keys:
80+
# All public keys are space-stripped.
81+
f.write(public_key + "\n")
5782

5883
return base.PLUGIN_EXECUTION_DONE, False

0 commit comments

Comments
 (0)