Skip to content

Enable the add-trailing-comma pre-commit fixer #661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
repos:
# - repo: https://github.com/asottile/add-trailing-comma.git
# rev: v2.0.1
# hooks:
# - id: add-trailing-comma
- repo: https://github.com/asottile/add-trailing-comma.git
rev: v2.0.1
hooks:
- id: add-trailing-comma
args:
- --py36-plus

# - repo: https://github.com/timothycrosley/isort.git
# rev: 5.4.2
Expand Down
33 changes: 20 additions & 13 deletions examples/https_connect_tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@
class HttpsConnectTunnelHandler(BaseTcpTunnelHandler):
"""A https CONNECT tunnel."""

PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT = memoryview(build_http_response(
httpStatusCodes.OK,
reason=b'Connection established'
))

PROXY_TUNNEL_UNSUPPORTED_SCHEME = memoryview(build_http_response(
httpStatusCodes.BAD_REQUEST,
headers={b'Connection': b'close'},
reason=b'Unsupported protocol scheme'
))
PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT = memoryview(
build_http_response(
httpStatusCodes.OK,
reason=b'Connection established',
),
)

PROXY_TUNNEL_UNSUPPORTED_SCHEME = memoryview(
build_http_response(
httpStatusCodes.BAD_REQUEST,
headers={b'Connection': b'close'},
reason=b'Unsupported protocol scheme',
),
)

def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
Expand All @@ -49,7 +53,8 @@ def handle_data(self, data: memoryview) -> Optional[bool]:
# Drop the request if not a CONNECT request
if self.request.method != httpMethods.CONNECT:
self.client.queue(
HttpsConnectTunnelHandler.PROXY_TUNNEL_UNSUPPORTED_SCHEME)
HttpsConnectTunnelHandler.PROXY_TUNNEL_UNSUPPORTED_SCHEME,
)
return True

# CONNECT requests are short and we need not worry about
Expand All @@ -61,7 +66,8 @@ def handle_data(self, data: memoryview) -> Optional[bool]:

# Queue tunnel established response to client
self.client.queue(
HttpsConnectTunnelHandler.PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT)
HttpsConnectTunnelHandler.PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT,
)

return None

Expand All @@ -70,7 +76,8 @@ def main() -> None:
# This example requires `threadless=True`
pool = AcceptorPool(
flags=Proxy.initialize(port=12345, num_workers=1, threadless=True),
work_klass=HttpsConnectTunnelHandler)
work_klass=HttpsConnectTunnelHandler,
)
try:
pool.setup()
while True:
Expand Down
20 changes: 13 additions & 7 deletions examples/pubsub_eventing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@
num_events_received = [0, 0]


def publisher_process(shutdown_event: multiprocessing.synchronize.Event,
dispatcher_queue: EventQueue) -> None:
def publisher_process(
shutdown_event: multiprocessing.synchronize.Event,
dispatcher_queue: EventQueue,
) -> None:
print('publisher starting')
try:
while not shutdown_event.is_set():
dispatcher_queue.publish(
request_id=process_publisher_request_id,
event_name=eventNames.WORK_STARTED,
event_payload={'time': time.time()},
publisher_id='eventing_pubsub_process'
publisher_id='eventing_pubsub_process',
)
except KeyboardInterrupt:
pass
Expand Down Expand Up @@ -70,7 +72,8 @@ def on_event(payload: Dict[str, Any]) -> None:
publisher_shutdown_event = multiprocessing.Event()
publisher = multiprocessing.Process(
target=publisher_process, args=(
publisher_shutdown_event, event_manager.event_queue, ))
publisher_shutdown_event, event_manager.event_queue, ),
)
publisher.start()

try:
Expand All @@ -80,7 +83,7 @@ def on_event(payload: Dict[str, Any]) -> None:
request_id=main_publisher_request_id,
event_name=eventNames.WORK_STARTED,
event_payload={'time': time.time()},
publisher_id='eventing_pubsub_main'
publisher_id='eventing_pubsub_main',
)
except KeyboardInterrupt:
print('bye!!!')
Expand All @@ -92,5 +95,8 @@ def on_event(payload: Dict[str, Any]) -> None:
subscriber.unsubscribe()
# Signal dispatcher to shutdown
event_manager.stop_event_dispatcher()
print('Received {0} events from main thread, {1} events from another process, in {2} seconds'.format(
num_events_received[0], num_events_received[1], time.time() - start_time))
print(
'Received {0} events from main thread, {1} events from another process, in {2} seconds'.format(
num_events_received[0], num_events_received[1], time.time() - start_time,
),
)
12 changes: 8 additions & 4 deletions examples/ssl_echo_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ def initialize(self) -> None:
conn = wrap_socket(
self.client.connection,
self.flags.keyfile,
self.flags.certfile)
self.flags.certfile,
)
conn.setblocking(False)
# Upgrade plain TcpClientConnection to SSL connection object
self.client = TcpClientConnection(
conn=conn, addr=self.client.addr)
conn=conn, addr=self.client.addr,
)

def handle_data(self, data: memoryview) -> Optional[bool]:
# echo back to client
Expand All @@ -49,8 +51,10 @@ def main() -> None:
num_workers=1,
threadless=True,
keyfile='https-key.pem',
certfile='https-signed-cert.pem'),
work_klass=EchoSSLServerHandler)
certfile='https-signed-cert.pem',
),
work_klass=EchoSSLServerHandler,
)
try:
pool.setup()
while True:
Expand Down
3 changes: 2 additions & 1 deletion examples/tcp_echo_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def main() -> None:
# This example requires `threadless=True`
pool = AcceptorPool(
flags=Proxy.initialize(port=12345, num_workers=1, threadless=True),
work_klass=EchoServerHandler)
work_klass=EchoServerHandler,
)
try:
pool.setup()
while True:
Expand Down
15 changes: 10 additions & 5 deletions examples/websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
def on_message(frame: WebsocketFrame) -> None:
"""WebsocketClient on_message callback."""
global client, num_echos, last_dispatch_time
print('Received %r after %d millisec' %
(frame.data, (time.time() - last_dispatch_time) * 1000))
assert(frame.data == b'hello' and frame.opcode ==
websocketOpcodes.TEXT_FRAME)
print(
'Received %r after %d millisec' %
(frame.data, (time.time() - last_dispatch_time) * 1000),
)
assert(
frame.data == b'hello' and frame.opcode ==
websocketOpcodes.TEXT_FRAME
)
if num_echos > 0:
client.queue(static_frame)
last_dispatch_time = time.time()
Expand All @@ -40,7 +44,8 @@ def on_message(frame: WebsocketFrame) -> None:
b'echo.websocket.org',
80,
b'/',
on_message=on_message)
on_message=on_message,
)
# Perform handshake
client.handshake()
# Queue some data for client
Expand Down
5 changes: 3 additions & 2 deletions proxy/common/flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self) -> None:
self.actions: List[str] = []
self.parser = argparse.ArgumentParser(
description='proxy.py v%s' % __version__,
epilog='Proxy.py not working? Report at: %s/issues/new' % __homepage__
epilog='Proxy.py not working? Report at: %s/issues/new' % __homepage__,
)

def add_argument(self, *args: Any, **kwargs: Any) -> argparse.Action:
Expand All @@ -46,7 +46,8 @@ def add_argument(self, *args: Any, **kwargs: Any) -> argparse.Action:
return action

def parse_args(
self, input_args: Optional[List[str]]) -> argparse.Namespace:
self, input_args: Optional[List[str]],
) -> argparse.Namespace:
"""Parse flags from input arguments."""
self.args = self.parser.parse_args(input_args)
return self.args
Expand Down
62 changes: 39 additions & 23 deletions proxy/common/pki.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ def remove_passphrase(
key_in_path: str,
password: str,
key_out_path: str,
timeout: int = 10) -> bool:
timeout: int = 10,
) -> bool:
"""Remove passphrase from a private key."""
command = [
'openssl', 'rsa',
'-passin', 'pass:%s' % password,
'-in', key_in_path,
'-out', key_out_path
'-out', key_out_path,
]
return run_openssl_command(command, timeout)

Expand All @@ -72,12 +73,13 @@ def gen_private_key(
key_path: str,
password: str,
bits: int = 2048,
timeout: int = 10) -> bool:
timeout: int = 10,
) -> bool:
"""Generates a private key."""
command = [
'openssl', 'genrsa', '-aes256',
'-passout', 'pass:%s' % password,
'-out', key_path, str(bits)
'-out', key_path, str(bits),
]
return run_openssl_command(command, timeout)

Expand All @@ -90,15 +92,16 @@ def gen_public_key(
alt_subj_names: Optional[List[str]] = None,
extended_key_usage: Optional[str] = None,
validity_in_days: int = 365,
timeout: int = 10) -> bool:
timeout: int = 10,
) -> bool:
"""For a given private key, generates a corresponding public key."""
with ssl_config(alt_subj_names, extended_key_usage) as (config_path, has_extension):
command = [
'openssl', 'req', '-new', '-x509', '-sha256',
'-days', str(validity_in_days), '-subj', subject,
'-passin', 'pass:%s' % private_key_password,
'-config', config_path,
'-key', private_key_path, '-out', public_key_path
'-key', private_key_path, '-out', public_key_path,
]
if has_extension:
command.extend([
Expand All @@ -112,13 +115,14 @@ def gen_csr(
key_path: str,
password: str,
crt_path: str,
timeout: int = 10) -> bool:
timeout: int = 10,
) -> bool:
"""Generates a CSR based upon existing certificate and key file."""
command = [
'openssl', 'x509', '-x509toreq',
'-passin', 'pass:%s' % password,
'-in', crt_path, '-signkey', key_path,
'-out', csr_path
'-out', csr_path,
]
return run_openssl_command(command, timeout)

Expand All @@ -133,7 +137,8 @@ def sign_csr(
alt_subj_names: Optional[List[str]] = None,
extended_key_usage: Optional[str] = None,
validity_in_days: int = 365,
timeout: int = 10) -> bool:
timeout: int = 10,
) -> bool:
"""Sign a CSR using CA key and certificate."""
with ext_file(alt_subj_names, extended_key_usage) as extension_path:
command = [
Expand All @@ -152,7 +157,8 @@ def sign_csr(

def get_ext_config(
alt_subj_names: Optional[List[str]] = None,
extended_key_usage: Optional[str] = None) -> bytes:
extended_key_usage: Optional[str] = None,
) -> bytes:
config = b''
# Add SAN extension
if alt_subj_names is not None and len(alt_subj_names) > 0:
Expand All @@ -169,12 +175,14 @@ def get_ext_config(
@contextlib.contextmanager
def ext_file(
alt_subj_names: Optional[List[str]] = None,
extended_key_usage: Optional[str] = None) -> Generator[str, None, None]:
extended_key_usage: Optional[str] = None,
) -> Generator[str, None, None]:
# Write config to temp file
config_path = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex)
with open(config_path, 'wb') as cnf:
cnf.write(
get_ext_config(alt_subj_names, extended_key_usage))
get_ext_config(alt_subj_names, extended_key_usage),
)

yield config_path

Expand All @@ -185,7 +193,8 @@ def ext_file(
@contextlib.contextmanager
def ssl_config(
alt_subj_names: Optional[List[str]] = None,
extended_key_usage: Optional[str] = None) -> Generator[Tuple[str, bool], None, None]:
extended_key_usage: Optional[str] = None,
) -> Generator[Tuple[str, bool], None, None]:
config = DEFAULT_CONFIG

has_extension = False
Expand All @@ -212,7 +221,7 @@ def run_openssl_command(command: List[str], timeout: int) -> bool:
cmd = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
stderr=subprocess.PIPE,
)
cmd.communicate(timeout=timeout)
return cmd.returncode == 0
Expand All @@ -221,7 +230,7 @@ def run_openssl_command(command: List[str], timeout: int) -> bool:
if __name__ == '__main__':
available_actions = (
'remove_passphrase', 'gen_private_key', 'gen_public_key',
'gen_csr', 'sign_csr'
'gen_csr', 'sign_csr',
)

parser = argparse.ArgumentParser(
Expand All @@ -231,7 +240,7 @@ def run_openssl_command(command: List[str], timeout: int) -> bool:
'action',
type=str,
default=None,
help='Valid actions: ' + ', '.join(available_actions)
help='Valid actions: ' + ', '.join(available_actions),
)
parser.add_argument(
'--password',
Expand Down Expand Up @@ -294,17 +303,24 @@ def run_openssl_command(command: List[str], timeout: int) -> bool:
if args.action == 'gen_private_key':
gen_private_key(args.private_key_path, args.password)
elif args.action == 'gen_public_key':
gen_public_key(args.public_key_path, args.private_key_path,
args.password, args.subject)
gen_public_key(
args.public_key_path, args.private_key_path,
args.password, args.subject,
)
elif args.action == 'remove_passphrase':
remove_passphrase(args.private_key_path, args.password,
args.private_key_path)
remove_passphrase(
args.private_key_path, args.password,
args.private_key_path,
)
elif args.action == 'gen_csr':
gen_csr(
args.csr_path,
args.private_key_path,
args.password,
args.public_key_path)
args.public_key_path,
)
elif args.action == 'sign_csr':
sign_csr(args.csr_path, args.crt_path, args.private_key_path, args.password,
args.public_key_path, str(int(time.time())), alt_subj_names=[args.hostname, ])
sign_csr(
args.csr_path, args.crt_path, args.private_key_path, args.password,
args.public_key_path, str(int(time.time())), alt_subj_names=[args.hostname],
)
Loading