<s> import logging <EOL> import datetime <EOL> import base64 <EOL> import json <EOL> import argparse <EOL> import sys <EOL> import requests <EOL> import boto3 <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . WARNING ) <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . WARNING ) <EOL> boto3 . set_stream_logger ( level = logging . WARNING ) <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . WARNING ) <EOL> def get_service ( url , from_context , to_context , auth_key , token_lifetime = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:result>' : False } <EOL> auth_context = { <EOL> '<STR_LIT>' : from_context , <EOL> '<STR_LIT:to>' : to_context <EOL> } <EOL> time_format = "<STR_LIT>" <EOL> now = datetime . datetime . utcnow ( ) <EOL> not_before = now . strftime ( time_format ) <EOL> _not_after = now + datetime . timedelta ( minutes = token_lifetime ) <EOL> not_after = _not_after . strftime ( time_format ) <EOL> payload = json . dumps ( { <EOL> '<STR_LIT>' : not_before , <EOL> '<STR_LIT>' : not_after <EOL> } ) <EOL> try : <EOL> kms = boto3 . client ( '<STR_LIT>' ) <EOL> except Exception : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> return ret <EOL> try : <EOL> token = kms . encrypt ( <EOL> KeyId = auth_key , <EOL> Plaintext = payload , <EOL> EncryptionContext = auth_context <EOL> ) [ '<STR_LIT>' ] <EOL> token = base64 . b64encode ( token ) <EOL> except Exception : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> return ret <EOL> try : <EOL> response = requests . get ( <EOL> '<STR_LIT>' . format ( url , from_context ) , <EOL> auth = ( from_context , token ) , <EOL> allow_redirects = False , <EOL> timeout = <NUM_LIT:2> <EOL> ) <EOL> except requests . ConnectionError : <EOL> logging . error ( '<STR_LIT>' ) <EOL> return ret <EOL> except requests . Timeout : <EOL> logging . error ( '<STR_LIT>' ) <EOL> return ret <EOL> if response . status_code == <NUM_LIT> : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return ret <EOL> elif response . status_code == <NUM_LIT> : <EOL> logging . error ( '<STR_LIT>' ) <EOL> return ret <EOL> elif response . status_code != <NUM_LIT:200> : <EOL> msg = '<STR_LIT>' <EOL> msg = msg . format ( response . status_code ) <EOL> logging . error ( msg ) <EOL> return ret <EOL> try : <EOL> data = response . json ( ) <EOL> except ValueError : <EOL> logging . error ( '<STR_LIT>' ) <EOL> return ret <EOL> ret [ '<STR_LIT>' ] = data <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> return ret <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = argparse . ArgumentParser ( <EOL> formatter_class = argparse . RawDescriptionHelpFormatter , <EOL> description = '<STR_LIT>' , <EOL> epilog = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> required = True , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> required = True , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> type = int , <EOL> help = '<STR_LIT>' , <EOL> default = <NUM_LIT:1> <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT:info>' <EOL> ) <EOL> args = parser . parse_args ( ) <EOL> numeric_loglevel = getattr ( logging , args . log_level . upper ( ) , None ) <EOL> if not isinstance ( numeric_loglevel , int ) : <EOL> raise ValueError ( '<STR_LIT>' . format ( args . loglevel ) ) <EOL> logging . basicConfig ( <EOL> level = numeric_loglevel , <EOL> format = '<STR_LIT>' , <EOL> stream = sys . stderr <EOL> ) <EOL> ret = get_service ( <EOL> args . url , <EOL> args . from_context , <EOL> args . to_context , <EOL> args . auth_key , <EOL> args . token_lifetime <EOL> ) <EOL> sys . stdout . write ( '<STR_LIT>' . format ( json . dumps ( ret ) ) ) </s>
