2525CONF = cloudbaseinit_conf .CONF
2626LOG = 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
2933class 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