-
Notifications
You must be signed in to change notification settings - Fork 16
/
run_terminate_ec2_client_vpn.py
executable file
·170 lines (125 loc) · 5.22 KB
/
run_terminate_ec2_client_vpn.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
import time
from env import env
from run_common import AWSCli
from run_common import print_message
from run_common import print_session
options, args = dict(), list()
if __name__ == "__main__":
from run_common import parse_args
options, args = parse_args()
def run_terminate_client_vpn(name, settings):
vpc_region = settings['AWS_VPC_REGION']
aws_cli = AWSCli(vpc_region)
print_message(f'terminate {name}')
################################################################################
print_message('get endpoint id')
cmd = ['ec2', 'describe-client-vpn-endpoints']
result = aws_cli.run(cmd)
vpn_endpoint_id = None
for r in result['ClientVpnEndpoints']:
if not r['Tags']:
continue
for t in r['Tags']:
if t['Key'] == 'Name' and t['Value'] == name:
vpn_endpoint_id = r['ClientVpnEndpointId']
break
if vpn_endpoint_id:
break
if vpn_endpoint_id:
################################################################################
print_message('terminate all existing vpn connections')
cmd = ['ec2', 'terminate-client-vpn-connections']
cmd += ['--client-vpn-endpoint-id', vpn_endpoint_id]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('revoke authorizations for target network')
cmd = ['ec2', 'revoke-client-vpn-ingress']
cmd += ['--client-vpn-endpoint-id', vpn_endpoint_id]
cmd += ['--target-network-cidr', aws_cli.cidr_vpc['eb']]
cmd += ['--revoke-all-groups']
aws_cli.run(cmd, ignore_error=True)
cmd = ['ec2', 'revoke-client-vpn-ingress']
cmd += ['--client-vpn-endpoint-id', vpn_endpoint_id]
cmd += ['--target-network-cidr', aws_cli.cidr_vpc['rds']]
cmd += ['--revoke-all-groups']
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('disassociate target network')
cmd = ['ec2', 'describe-client-vpn-target-networks']
cmd += ['--client-vpn-endpoint-id', vpn_endpoint_id]
result = aws_cli.run(cmd)
for r in result['ClientVpnTargetNetworks']:
cmd = ['ec2', 'disassociate-client-vpn-target-network']
cmd += ['--client-vpn-endpoint-id', vpn_endpoint_id]
cmd += ['--association-id', r['AssociationId']]
aws_cli.run(cmd, ignore_error=True)
elapsed_time = 0
while True:
cmd = ['ec2', 'describe-client-vpn-endpoints']
cmd += ['--client-vpn-endpoint-ids', vpn_endpoint_id]
result = aws_cli.run(cmd)
result = result['ClientVpnEndpoints'][0]
if result['Status']['Code'] == 'pending-associate':
break
print('waiting for endpoint disassociated... (elapsed time: \'%d\' seconds)' % elapsed_time)
time.sleep(5)
elapsed_time += 5
if elapsed_time > 60 * 30:
raise Exception()
################################################################################
print_message('remove client vpn endpoint')
cmd = ['ec2', 'delete-client-vpn-endpoint']
cmd += ['--client-vpn-endpoint-id', vpn_endpoint_id]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete server certification')
cmd = ['acm', 'list-certificates']
result = aws_cli.run(cmd)
for r in result['CertificateSummaryList']:
if r['DomainName'] != 'server':
continue
cmd = ['acm', 'delete-certificate']
cmd += ['--certificate-arn', r['CertificateArn']]
aws_cli.run(cmd)
################################################################################
print_message('delete SAML identity provider')
cmd = ['iam', 'list-saml-providers']
result = aws_cli.run(cmd)
aa = None
for r in result['SAMLProviderList']:
if f'AWSClientVPN_SAML_{name}' in r['Arn']:
aa = r['Arn']
break
if not aa:
return
cmd = ['iam', 'delete-saml-provider']
cmd += ['--saml-provider-arn', aa]
aws_cli.run(cmd)
################################################################################
#
# start
#
################################################################################
print_session('terminate client vpn')
################################################################################
target_name = None
region = options.get('region')
is_target_exists = False
if len(args) > 1:
target_name = args[1]
for vpn_env in env.get('client_vpn', list()):
if target_name and vpn_env['NAME'] != target_name:
continue
if region and vpn_env['AWS_VPC_REGION'] != region:
continue
is_target_exists = True
run_terminate_client_vpn(vpn_env['NAME'], vpn_env)
if is_target_exists is False:
mm = list()
if target_name:
mm.append(target_name)
if region:
mm.append(region)
mm = ' in '.join(mm)
print(f'client vpn: {mm} is not found in config.json')