-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-circle.py
executable file
·152 lines (115 loc) · 4.97 KB
/
update-circle.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
#!/usr/bin/env python3
#
# This script updates circle-CI with stored secrets from production
# Secret Manager.
# Dennis DeMarco 1/5/2023
# In AWS Secrets manager create a secret name that matches the contexts
# name in organization settings. All secret values (key/value) in AWS will
# be updated in CircleCI under that same context name.
# Requires an aws profile with secret access and CircleAPI Token/Account ID
# defined in config.ini
import boto3, json
from botocore.exceptions import ClientError
import configparser
import http.client
class SecretMgr():
def __init__(self) -> None:
self.config = configparser.ConfigParser()
self.config.read('config.ini')
# setup the AWS boto3 client
self.session=boto3.Session(profile_name=self.config['DEFAULT']['PROFILE'])
self.sm_client = self.session.client(
service_name = 'secretsmanager',
region_name = self.config['DEFAULT']['REGION']
)
def get_aws_env_secrets(self, id):
try:
response = self.sm_client.get_secret_value(
SecretId=id
)
except ClientError as e:
raise e
return json.loads(response['SecretString'])
def get_aws_secrets(self,tags=['']):
filters=[ { 'Key': 'tag-key',
'Values': tags }
]
try:
response = self.sm_client.list_secrets(Filters=filters,MaxResults=100)
except ClientError as e:
raise e
return response['SecretList']
def get_circle_contexts(self):
headers = { 'Circle-Token': self.config['DEFAULT']['CIRCLE_TOKEN'] }
conn = http.client.HTTPSConnection('circleci.com')
conn.request('GET',
'/api/v2/context?owner-slug=bitbucket/onedayhq&page-token=NEXT_PAGE_TOKEN',
headers=headers)
res = conn.getresponse()
data = res.read()
return json.loads(data.decode('utf-8'))
def create_circle_context(self, context='default'):
headers = { 'content-type' : "application/json",
'Circle-Token' : self.config['DEFAULT']['CIRCLE_TOKEN']
}
payload = { 'name': context,
'owner': { 'id': self.config['DEFAULT']['ACCOUNT_ID]'],
'type': 'organization'
}
}
conn = http.client.HTTPSConnection('circleci.com')
conn.request("POST", "/api/v2/context",json.dumps(payload),headers)
res = conn.getresponse()
data = res.read()
return data.decode('utf-8')
def get_context_id(self,contexts,context):
for c in contexts['items']:
if context in c['name']:
return (c['id'])
return None
def delete_circle_context(self, context='default'):
headers = { 'content-type' : "application/json",
'Circle-Token' : self.config['DEFAULT']['CIRCLE_TOKEN']
}
contexts = self.get_circle_contexts()
context_id = self.get_context_id(contexts,context)
if context_id:
conn = http.client.HTTPSConnection('circleci.com')
conn.request("DELETE",
f"/api/v2/context/{context_id}", headers=headers)
conn.getresponse()
def add_circleci_env_variable(self,context_id,context,env_name,env_value):
headers = { 'content-type' : "application/json",
'Circle-Token' : self.config['DEFAULT']['CIRCLE_TOKEN']
}
payload = {'value': env_value}
conn = http.client.HTTPSConnection('circleci.com')
if context_id:
conn.request("PUT",
f"/api/v2/context/{context_id}/environment-variable/{env_name}",
json.dumps(payload), headers
)
conn.getresponse()
def sync_circle(self):
# Lets get the names from AWS secret manager and make them Contexts in CircleCI
secrets = self.get_aws_secrets(['CIRCLECI'])
for secret in secrets:
context = secret['Name']
print(f"Creating context: {context}")
self.create_circle_context(context)
# Getting the ID's for the contexts we just made
contexts = self.get_circle_contexts()
# Lets go through each secret name and set the environment variables from the secret into
# each circle context
for secret in secrets:
context = secret['Name']
print(f"Updating environment variables for {context}")
envs_pairs = self.get_aws_env_secrets(context)
for key, value in envs_pairs.items():
context_id = self.get_context_id(contexts,context)
self.add_circleci_env_variable(context_id,context,key,value)
def main():
secrets = SecretMgr()
secrets.sync_circle()
if __name__ == "__main__":
main()