-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathrun_secure_tunnel_ci.py
92 lines (71 loc) · 3.15 KB
/
run_secure_tunnel_ci.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.
# Built-in
import argparse
import subprocess
import pathlib
import sys
from time import sleep
# Needs to be installed via pip
import boto3 # - for launching sample
current_folder = pathlib.Path(__file__).resolve()
def getSecretsAndLaunch(parsed_commands):
exit_code = 0
print("Creating secure tunnel client using Boto3")
tunnel_client = None
try:
tunnel_client = boto3.client(
"iotsecuretunneling", region_name=parsed_commands.sample_region)
except Exception:
print("Could not create tunnel client!")
exit(-1)
tunnel_data = None
try:
tunnel_data = tunnel_client.open_tunnel(
destinationConfig={'services': ['ssh', 'http', ]})
except Exception:
print("Could not open tunnel!")
exit(-1)
print("Launching Secure Tunnel samples...")
exit_code = launch_samples(parsed_commands, tunnel_data)
print("Closing tunnel...")
try:
tunnel_client.close_tunnel(
tunnelId=tunnel_data["tunnelId"], delete=True)
except Exception:
print("Could not close tunnel!")
exit(-1)
return exit_code
def launch_samples(parsed_commands, tunnel_data):
exit_code = 0
# Right now secure tunneling is only in C++, so we only support launching the sample in the C++ way
launch_arguments_destination = [
"--test", "--signing-region", parsed_commands.sample_region, "--access_token", tunnel_data["destinationAccessToken"]]
launch_arguments_source = ["--local_proxy_mode_source", "--region",
parsed_commands.sample_region, "--access_token", tunnel_data["sourceAccessToken"]]
destination_run = subprocess.Popen(
args=launch_arguments_destination, executable=parsed_commands.sample_file)
print("About to sleep before running source part of sample...")
sleep(10) # Sleep to give the destination some time to run
source_run = subprocess.Popen(
args=launch_arguments_source, executable=parsed_commands.sample_file)
# Wait for the source to finish
source_run.wait()
# Once finished, stop the destination run
destination_run.kill()
# finish!
return exit_code
def main():
argument_parser = argparse.ArgumentParser(
description="Utility to run Secure Tunneling sample in CI")
argument_parser.add_argument("--sample_file",
metavar="<CPP=C:\\repository\\PubSub.exe, Java=samples/BasicPubSub, Python=PubSub.py, Javascript=C:\\samples\\node\\pub_sub>",
required=True, default="", help="Sample to launch. Format varies based on programming language")
argument_parser.add_argument("--sample_region", metavar="<Name of region>",
required=True, default="us-east-1", help="The name of the region to use for accessing secrets")
parsed_commands = argument_parser.parse_args()
print("Starting to launch Secure Tunneling sample...")
sample_result = getSecretsAndLaunch(parsed_commands)
sys.exit(sample_result)
if __name__ == "__main__":
main()