<s> import psycopg2 <EOL> import yaml <EOL> import sys <EOL> import json <EOL> import time <EOL> import hashlib <EOL> from unpaddedbase64 import encode_base64 <EOL> from signedjson . key import read_signing_keys <EOL> from signedjson . sign import sign_json <EOL> from canonicaljson import encode_canonical_json <EOL> def select_v1_keys ( connection ) : <EOL> cursor = connection . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> rows = cursor . fetchall ( ) <EOL> cursor . close ( ) <EOL> results = { } <EOL> for server_name , key_id , verify_key in rows : <EOL> results . setdefault ( server_name , { } ) [ key_id ] = encode_base64 ( verify_key ) <EOL> return results <EOL> def select_v1_certs ( connection ) : <EOL> cursor = connection . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> rows = cursor . fetchall ( ) <EOL> cursor . close ( ) <EOL> results = { } <EOL> for server_name , tls_certificate in rows : <EOL> results [ server_name ] = tls_certificate <EOL> return results <EOL> def select_v2_json ( connection ) : <EOL> cursor = connection . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> rows = cursor . fetchall ( ) <EOL> cursor . close ( ) <EOL> results = { } <EOL> for server_name , key_id , key_json in rows : <EOL> results . setdefault ( server_name , { } ) [ key_id ] = json . loads ( str ( key_json ) . decode ( "<STR_LIT:utf-8>" ) ) <EOL> return results <EOL> def convert_v1_to_v2 ( server_name , valid_until , keys , certificate ) : <EOL> return { <EOL> "<STR_LIT>" : { } , <EOL> "<STR_LIT>" : server_name , <EOL> "<STR_LIT>" : { <EOL> key_id : { "<STR_LIT:key>" : key } <EOL> for key_id , key in keys . items ( ) <EOL> } , <EOL> "<STR_LIT>" : valid_until , <EOL> "<STR_LIT>" : [ fingerprint ( certificate ) ] , <EOL> } <EOL> def fingerprint ( certificate ) : <EOL> finger = hashlib . sha256 ( certificate ) <EOL> return { "<STR_LIT>" : encode_base64 ( finger . digest ( ) ) } <EOL> def rows_v2 ( server , json ) : <EOL> valid_until = json [ "<STR_LIT>" ] <EOL> key_json = encode_canonical_json ( json ) <EOL> for key_id in json [ "<STR_LIT>" ] : <EOL> yield ( server , key_id , "<STR_LIT:->" , valid_until , valid_until , buffer ( key_json ) ) <EOL> def main ( ) : <EOL> config = yaml . load ( open ( sys . argv [ <NUM_LIT:1> ] ) ) <EOL> valid_until = int ( time . time ( ) / ( <NUM_LIT> * <NUM_LIT> ) ) * <NUM_LIT:1000> * <NUM_LIT> * <NUM_LIT> <EOL> server_name = config [ "<STR_LIT>" ] <EOL> signing_key = read_signing_keys ( open ( config [ "<STR_LIT>" ] ) ) [ <NUM_LIT:0> ] <EOL> database = config [ "<STR_LIT>" ] <EOL> assert database [ "<STR_LIT:name>" ] == "<STR_LIT>" , "<STR_LIT>" <EOL> args = database [ "<STR_LIT:args>" ] <EOL> args . pop ( "<STR_LIT>" ) <EOL> args . pop ( "<STR_LIT>" ) <EOL> connection = psycopg2 . connect ( ** args ) <EOL> keys = select_v1_keys ( connection ) <EOL> certificates = select_v1_certs ( connection ) <EOL> json = select_v2_json ( connection ) <EOL> result = { } <EOL> for server in keys : <EOL> if not server in json : <EOL> v2_json = convert_v1_to_v2 ( <EOL> server , valid_until , keys [ server ] , certificates [ server ] <EOL> ) <EOL> v2_json = sign_json ( v2_json , server_name , signing_key ) <EOL> result [ server ] = v2_json <EOL> yaml . safe_dump ( result , sys . stdout , default_flow_style = False ) <EOL> rows = list ( <EOL> row for server , json in result . items ( ) <EOL> for row in rows_v2 ( server , json ) <EOL> ) <EOL> cursor = connection . cursor ( ) <EOL> cursor . executemany ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> rows <EOL> ) <EOL> connection . commit ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
