<s> import subprocess <EOL> import re <EOL> import struct <EOL> import base64 <EOL> import tempfile <EOL> import os , stat <EOL> def rotate ( record , newpassword ) : <EOL> """<STR_LIT>""" <EOL> oldPrivateKey = record . get ( '<STR_LIT>' ) <EOL> keyFileName = None <EOL> if oldPrivateKey : <EOL> pipe = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' . format ( record . password ) ] , <EOL> stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . DEVNULL ) <EOL> ( output1 , _ ) = pipe . communicate ( input = oldPrivateKey . encode ( ) , timeout = <NUM_LIT:3> ) <EOL> if pipe . poll ( ) == <NUM_LIT:0> : <EOL> keyFileName = tempfile . mktemp ( ) <EOL> keyFile = os . open ( keyFileName , os . O_WRONLY | os . O_CREAT , stat . S_IRUSR | stat . S_IWUSR ) <EOL> os . write ( keyFile , output1 ) <EOL> os . close ( keyFile ) <EOL> try : <EOL> pipe = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' . format ( newpassword ) , '<STR_LIT>' ] , <EOL> stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . DEVNULL ) <EOL> ( output1 , _ ) = pipe . communicate ( timeout = <NUM_LIT:3> ) <EOL> newPrivateKey = output1 . decode ( ) <EOL> pipe = subprocess . Popen ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" . format ( newpassword ) , "<STR_LIT>" ] , <EOL> stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . DEVNULL ) <EOL> ( output2 , _ ) = pipe . communicate ( input = output1 , timeout = <NUM_LIT:3> ) <EOL> newPublicKeyPEM = output2 . decode ( ) <EOL> pipe = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , stdin = subprocess . PIPE , <EOL> stdout = subprocess . PIPE , stderr = subprocess . DEVNULL ) <EOL> ( output3 , _ ) = pipe . communicate ( input = output2 , timeout = <NUM_LIT:3> ) <EOL> lines = output3 . decode ( ) . split ( '<STR_LIT:\n>' ) <EOL> modulus_hex = '<STR_LIT>' . join ( [ l . strip ( ) for l in lines if l . startswith ( '<STR_LIT:U+0020>' ) ] ) <EOL> mod = bytearray . fromhex ( '<STR_LIT>' . join ( modulus_hex . split ( sep = '<STR_LIT::>' ) ) ) <EOL> exponent = [ l for l in lines if l . startswith ( '<STR_LIT>' ) ] . pop ( <NUM_LIT:0> ) <EOL> m = re . search ( '<STR_LIT>' , exponent ) <EOL> exp = struct . pack ( '<STR_LIT>' , int ( m . group ( <NUM_LIT:1> ) ) ) <EOL> prefix = '<STR_LIT>' <EOL> parts = [ prefix . encode ( '<STR_LIT:ascii>' ) , exp , mod ] <EOL> ssh_key_bytes = b'<STR_LIT>' . join ( [ b'<STR_LIT>' . join ( [ struct . pack ( '<STR_LIT>' , len ( i ) ) , i ] ) for i in parts ] ) <EOL> newPublicKeySSH = '<STR_LIT:U+0020>' . join ( [ prefix , base64 . b64encode ( ssh_key_bytes ) . decode ( ) ] ) <EOL> hosts = [ cf [ '<STR_LIT:value>' ] for cf in record . custom_fields if cf [ '<STR_LIT:name>' ] == '<STR_LIT>' ] <EOL> if keyFileName : <EOL> oldPublicKey = record . get ( '<STR_LIT>' ) <EOL> for host in hosts : <EOL> try : <EOL> child = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , keyFileName , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' . format ( record . login , host ) , '<STR_LIT>' ] , stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> ( out_child , error_child ) = child . communicate ( timeout = <NUM_LIT:10> ) <EOL> if child . poll ( ) == <NUM_LIT:0> : <EOL> keys = out_child . decode ( ) . splitlines ( ) <EOL> keys = [ l for l in keys if len ( l ) > <NUM_LIT:0> ] <EOL> keys = [ l for l in keys if l != oldPublicKey ] <EOL> keys . append ( newPublicKeySSH ) <EOL> child = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , keyFileName , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' . format ( record . login , host ) , '<STR_LIT>' . format ( '<STR_LIT:\n>' . join ( keys ) ) ] , stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> ( out_child , error_child ) = child . communicate ( timeout = <NUM_LIT:10> ) <EOL> if error_child : <EOL> print ( '<STR_LIT>' . format ( host , error_child . decode ( ) ) ) <EOL> except Exception as e : <EOL> print ( '<STR_LIT>' . format ( host , e ) ) <EOL> record . set_field ( '<STR_LIT>' , newPrivateKey ) <EOL> record . set_field ( '<STR_LIT>' , newPublicKeyPEM ) <EOL> record . set_field ( '<STR_LIT>' , newPublicKeySSH ) <EOL> record . password = newpassword <EOL> return True <EOL> except Exception as e : <EOL> print ( e ) <EOL> return False <EOL> finally : <EOL> if keyFileName : <EOL> os . remove ( keyFileName ) </s>
