-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-repository.py
279 lines (230 loc) · 7.42 KB
/
create-repository.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python
import os
import pathlib
import subprocess
import sys
GITSIGN_IDENTITY = "billy@chainguard.dev"
def main(location="../ossna-demo"):
os.environ["GITTUF_DEV"] = "1"
with open("verify-workflow.yml") as fp:
workflow = fp.read()
current_dir = os.getcwd()
keys_dir = os.path.join(current_dir, "keys")
if os.path.exists(location):
print("Target location exists")
sys.exit(1)
os.mkdir(location)
os.chdir(location)
result = subprocess.run(["git", "init", "-b", "main"])
if result.returncode != 0:
print("Unable to create Git repository in specified location")
sys.exit(1)
_set_root(keys_dir)
result = subprocess.run(
["gittuf", "trust", "init", "-k", f"{os.path.join(keys_dir, 'root')}"]
)
if result.returncode != 0:
print("Unable to initialize gittuf root of trust")
sys.exit(1)
result = subprocess.run(
[
"gittuf",
"trust",
"add-policy-key",
"-k",
f"{os.path.join(keys_dir, 'root')}",
"--policy-key",
f"{os.path.join(keys_dir, 'targets.pem')}",
]
)
if result.returncode != 0:
print("Unable to add policy key to gittuf root of trust")
sys.exit(1)
_set_targets(keys_dir)
result = subprocess.run(
["gittuf", "policy", "init", "-k", f"{os.path.join(keys_dir, 'targets')}"]
)
if result.returncode != 0:
print("Unable to initialize gittuf policy")
sys.exit(1)
result = subprocess.run(
[
"gittuf",
"policy",
"add-rule",
"-k",
f"{os.path.join(keys_dir, 'targets')}",
"--rule-name",
"protect-main-branch",
"--rule-pattern",
"git:refs/heads/main",
"--authorize-key",
f"{os.path.join(keys_dir, 'billy.pem')}",
"--authorize-key",
f"{os.path.join(keys_dir, 'aditya.pem')}",
"--threshold",
"2",
]
)
if result.returncode != 0:
print("Unable to add main branch protection rule")
sys.exit(1)
_set_billy(keys_dir)
result = subprocess.run(["git", "checkout", "-b", "feature"])
if result.returncode != 0:
print("Unable to checkout feature branch")
sys.exit(1)
with open("README.md", "w+") as fp:
fp.write("Hello, open source summit!\n")
pathlib.Path(".github/workflows").mkdir(parents=True, exist_ok=True)
with open(".github/workflows/verify.yml", "w+") as fp:
fp.write(workflow)
subprocess.run(["git", "add", "README.md"])
subprocess.run(["git", "add", ".github"])
result = subprocess.run(["git", "commit", "-S", "-m", "Initial commit"])
if result.returncode != 0:
print("Unable to create initial commit")
sys.exit(1)
result = subprocess.run(["gittuf", "rsl", "record", "feature"])
if result.returncode != 0:
print("Unable to create RSL entry")
sys.exit(1)
_set_aditya(keys_dir)
result = subprocess.run(
[
"gittuf",
"dev",
"authorize",
"--from-ref",
"refs/heads/feature",
"--signing-key",
f"{os.path.join(keys_dir, 'aditya')}",
"refs/heads/main",
]
)
if result.returncode != 0:
print("Unable to approve merging feature into main")
sys.exit(1)
result = subprocess.run(["git", "checkout", "-b", "main"])
if result.returncode != 0:
print("Unable to checkout main branch")
sys.exit(1)
_set_billy(keys_dir)
result = subprocess.run(["gittuf", "rsl", "record", "main"])
if result.returncode != 0:
print("Unable to create RSL entry")
sys.exit(1)
result = subprocess.run(["gittuf", "verify-ref", "main", "--verbose"])
if result.returncode != 0:
print("gittuf verification failed")
sys.exit(1)
def _set_aditya(keys_dir):
result = subprocess.run(["git", "config", "--local", "gpg.format", "ssh"])
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
result = subprocess.run(
[
"git",
"config",
"--local",
"user.signingkey",
f"{os.path.join(keys_dir, 'aditya')}",
]
)
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
result = subprocess.run(["git", "config", "--local", "user.name", "Aditya Sirish"])
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
result = subprocess.run(
["git", "config", "--local", "user.email", "aditya@example.com"]
)
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
def _set_billy(keys_dir):
result = subprocess.run(["git", "config", "--local", "gpg.format", "ssh"])
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
result = subprocess.run(
[
"git",
"config",
"--local",
"user.signingkey",
f"{os.path.join(keys_dir, 'billy')}",
]
)
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
result = subprocess.run(["git", "config", "--local", "user.name", "Billy Lynch"])
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
result = subprocess.run(
["git", "config", "--local", "user.email", "billy@example.com"]
)
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
def _set_root(keys_dir):
result = subprocess.run(["git", "config", "--local", "gpg.format", "ssh"])
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
result = subprocess.run(
[
"git",
"config",
"--local",
"user.signingkey",
f"{os.path.join(keys_dir, 'root')}",
]
)
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
result = subprocess.run(["git", "config", "--local", "user.name", "Root"])
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
result = subprocess.run(
["git", "config", "--local", "user.email", "root-ossna-demo@saky.in"]
)
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
def _set_targets(keys_dir):
result = subprocess.run(["git", "config", "--local", "gpg.format", "ssh"])
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
result = subprocess.run(
[
"git",
"config",
"--local",
"user.signingkey",
f"{os.path.join(keys_dir, 'targets')}",
]
)
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
result = subprocess.run(["git", "config", "--local", "user.name", "Policy"])
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
result = subprocess.run(
["git", "config", "--local", "user.email", "targets-ossna-demo@saky.in"]
)
if result.returncode != 0:
print("Unable to set Git config")
sys.exit(1)
if __name__ == "__main__":
main()