-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (63 loc) · 2.82 KB
/
main.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
import logging
import base64
import os
import CloudFlare
from google.cloud import secretmanager
from google_methods import CloudRunService, create_check_and_alert, remove_check_and_alert
logging.basicConfig(level=logging.INFO)
logging.getLogger('googleapicliet.discovery_cache').setLevel(logging.ERROR)
def get_secret(secret_name):
client = secretmanager.SecretManagerServiceClient()
secret_name = secret_name
project_id = os.environ["GCP_PROJECT"]
resource_name = f"projects/{project_id}/secrets/{secret_name}/versions/latest"
response = client.access_secret_version(name=resource_name)
return response.payload.data.decode('UTF-8')
def get_cloudflare_zone_id(cf, domain_name):
zones = cf.zones.get(params = {'per_page':100})
for zone in zones:
if domain_name == zone['name']:
return zone['id']
return None
def create_page_rule(cf, zone_id):
new_rule = {'targets': [{'target': 'url',
'constraint': {'operator': 'matches', 'value': 'will.gleich.tech/*'}}],
'actions': [{'id': 'forwarding_url',
'value': {'url': 'https://will.iam.gleich.tech', 'status_code': 302}}],
'priority': 2,
'status': 'active'}
return cf.zones.pagerules.post(zone_id, data=new_rule)
def delete_page_rule(cf, zone_id):
for rule in cf.zones.pagerules.get(zone_id):
if rule['targets'][0]['constraint']['value'].startswith("will.gleich.tech"):
return cf.zones.pagerules.delete(zone_id, rule['id'])
return None
def gleich_switch(event, context):
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
logging.info(f"REQUEST_BODY: {pubsub_message}")
project_id = os.environ["GCP_PROJECT"]
gleich_tech = CloudRunService("gleich-tech", project_id, "us-central1")
logging.info("started the function")
logging.info("initalized the cloud_run")
remove_check_and_alert('will.gleich.tech')
gleich_tech.allow_unauthenticated()
logging.info("set permissions on gleich-tech svc")
#Cloudflare section
cf = CloudFlare.CloudFlare(token=get_secret("cloudflare-api-key"))
zone_id = get_cloudflare_zone_id(cf, "gleich.tech")
create_page_rule(cf, zone_id)
logging.info(f"attached page rule")
return f"function moved through successfully"
def cleanup_switch():
project_id = os.environ["GCP_PROJECT"]
gleich_tech = CloudRunService("gleich-tech", project_id, "us-central1")
gleich_tech.disallow_unauthenticated()
create_check_and_alert('will.gleich.tech')
#Cloudflare section
cf = CloudFlare.CloudFlare(token=get_secret("cloudflare-api-key"))
zone_id = get_cloudflare_zone_id(cf, "gleich.tech")
logging.info("set permissions on gleich-tech svc")
delete_page_rule(cf, zone_id)
if __name__ == '__main__':
cleanup_switch()
pass