Skip to content

Commit 4004584

Browse files
committed
add support for administrators_authorized_keys to UsersPlugin
see https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement#administrative-user Change-Id: Id39cb6fbbf5d41a1202eebc9d47a385902404fce
1 parent cc8575b commit 4004584

File tree

1 file changed

+27
-0
lines changed
  • cloudbaseinit/plugins/common/userdataplugins/cloudconfigplugins

1 file changed

+27
-0
lines changed

cloudbaseinit/plugins/common/userdataplugins/cloudconfigplugins/users.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@
2424
base
2525
)
2626

27+
2728
CONF = cloudbaseinit_conf.CONF
2829
LOG = oslo_logging.getLogger(__name__)
2930

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

3136
class UsersPlugin(base.BaseCloudConfigPlugin):
3237
"""Creates users given in the cloud-config format."""
@@ -154,6 +159,7 @@ def process(self, data):
154159
"Can't process the type of data %r" % type(data))
155160

156161
osutils = osutils_factory.get_os_utils()
162+
administrators_authorized_keys = []
157163
for item in data:
158164
if not isinstance(item, dict):
159165
continue
@@ -172,4 +178,25 @@ def process(self, data):
172178
LOG.warning("An error occurred during user '%s' creation: '%s"
173179
% (user_name, ex))
174180

181+
if ADMINISTRATORS in self._get_groups(item):
182+
admin_public_keys = item.get('ssh_authorized_keys', [])
183+
administrators_authorized_keys.extend(admin_public_keys)
184+
185+
if osutils.group_exists(ADMINISTRATORS):
186+
program_data_dir = os.getenv("PROGRAMDATA", "C:\ProgramData")
187+
program_data_ssh_dir = os.path.join(program_data_dir, "ssh")
188+
if not os.path.exists(program_data_ssh_dir):
189+
os.makedirs(program_data_ssh_dir)
190+
191+
administrators_authorized_keys_path = os.path.join(
192+
program_data_ssh_dir, "administrators_authorized_keys"
193+
)
194+
195+
LOG.info("Writing SSH public keys in: %s",
196+
administrators_authorized_keys_path)
197+
198+
with open(administrators_authorized_keys_path, 'w') as f:
199+
for authorized_key in administrators_authorized_keys:
200+
f.write(authorized_key + "\n")
201+
175202
return False

0 commit comments

Comments
 (0)