@@ -253,7 +253,8 @@ def present(
253253 3. Paste it into a new file.
254254
255255 options
256- The options passed to the key, pass a list object
256+ The options passed to the key, pass a list object.
257+ If set, this will overwrite the ``options`` to all keys in source file
257258
258259 config
259260 The location of the authorized keys file relative to the user's home
@@ -307,36 +308,16 @@ def present(
307308 if source != "" and not source_path :
308309 data = "no key"
309310 elif source != "" and source_path :
310- key = __salt__ ["cp.get_file_str" ](source , saltenv = __env__ )
311- filehasoptions = False
312- # check if this is of form {options} {enc} {key} {comment}
313- sshre = re .compile (r"^(sk-)?(ssh\-|ecds).*" )
314- key = key .rstrip ().split ("\n " )
315- for keyline in key :
316- filehasoptions = sshre .match (keyline )
317- if not filehasoptions :
318- data = __salt__ ["ssh.set_auth_key_from_file" ](
319- user ,
320- source ,
321- config = config ,
322- saltenv = __env__ ,
323- fingerprint_hash_type = fingerprint_hash_type ,
324- )
325- else :
326- # Split keyline to get key and comment
327- keyline = keyline .split (" " )
328- key_type = keyline [0 ]
329- key_value = keyline [1 ]
330- key_comment = keyline [2 ] if len (keyline ) > 2 else ""
331- data = __salt__ ["ssh.set_auth_key" ](
332- user ,
333- key_value ,
334- enc = key_type ,
335- comment = key_comment ,
336- options = options or [],
337- config = config ,
338- fingerprint_hash_type = fingerprint_hash_type ,
339- )
311+ # ssh.set_auth_key_from_file already reads and add/replace all keys
312+ # from source file.
313+ data = __salt__ ["ssh.set_auth_key_from_file" ](
314+ user ,
315+ source ,
316+ config = config ,
317+ saltenv = __env__ ,
318+ fingerprint_hash_type = fingerprint_hash_type ,
319+ options = options ,
320+ )
340321 else :
341322 data = __salt__ ["ssh.set_auth_key" ](
342323 user ,
@@ -454,32 +435,23 @@ def absent(
454435 )
455436 return ret
456437
457- # Extract Key from file if source is present
438+ # Get only the path to the file without env referrences to check if exists
458439 if source != "" :
459- key = __salt__ ["cp.get_file_str" ](source , saltenv = __env__ )
460- filehasoptions = False
461- # check if this is of form {options} {enc} {key} {comment}
462- sshre = re .compile (r"^(sk-)?(ssh\-|ecds).*" )
463- key = key .rstrip ().split ("\n " )
464- for keyline in key :
465- filehasoptions = sshre .match (keyline )
466- if not filehasoptions :
467- ret ["comment" ] = __salt__ ["ssh.rm_auth_key_from_file" ](
468- user ,
469- source ,
470- config ,
471- saltenv = __env__ ,
472- fingerprint_hash_type = fingerprint_hash_type ,
473- )
474- else :
475- # Split keyline to get key
476- keyline = keyline .split (" " )
477- ret ["comment" ] = __salt__ ["ssh.rm_auth_key" ](
478- user ,
479- keyline [1 ],
480- config = config ,
481- fingerprint_hash_type = fingerprint_hash_type ,
482- )
440+ source_path = __salt__ ["cp.get_url" ](source , None , saltenv = __env__ )
441+
442+ # Extract Key from file if source is present
443+ if source != "" and not source_path :
444+ data = "no key"
445+ elif source != "" and source_path :
446+ # ssh.rm_auth_key_from_file already reads and delete all keys
447+ # from source file.
448+ ret ["comment" ] = __salt__ ["ssh.rm_auth_key_from_file" ](
449+ user ,
450+ source ,
451+ config ,
452+ saltenv = __env__ ,
453+ fingerprint_hash_type = fingerprint_hash_type ,
454+ )
483455 else :
484456 # Get just the key
485457 sshre = re .
compile (
r"^(.*?)\s?((?:sk-)?(?:ssh\-|ecds)[\[email protected] ]+\s.+)$" )
0 commit comments