-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
secrets.ts
222 lines (203 loc) · 5.84 KB
/
secrets.ts
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
import { Octokit } from "@octokit/rest";
import { seal } from "tweetsodium";
export interface PublicKey {
key: string;
key_id: string;
}
export interface Secrets {
publicKey: () => Promise<PublicKey>;
upsert: (name: string, value: string, key_id: string) => Promise<void>;
}
export const encrypt = (plaintext: string, publicKey: string): string =>
Buffer.from(
seal(Buffer.from(plaintext), Buffer.from(publicKey, "base64"))
).toString("base64");
export class GitHubRepositorySecrets implements Secrets {
protected readonly octokit: Octokit;
protected readonly owner: string;
protected readonly repo: string;
constructor(githubToken: string, owner: string, repo: string) {
this.octokit = new Octokit({ auth: githubToken });
this.owner = owner;
this.repo = repo;
}
async publicKey() {
return (
await this.octokit
.request("GET /repos/{owner}/{repo}/actions/secrets/public-key", {
owner: this.owner,
repo: this.repo,
})
.catch((e) => {
console.log(
`failed to fetch public key for ${this.owner}/${this.repo}`
);
console.log(e.headers);
throw e;
})
).data;
}
async upsert(secret_name: string, encrypted_value: string, key_id: string) {
await this.octokit
.request("PUT /repos/{owner}/{repo}/actions/secrets/{secret_name}", {
owner: this.owner,
repo: this.repo,
secret_name,
encrypted_value,
key_id,
})
.catch((e) => {
console.log(
`failed to upsert secret ${secret_name} for ${this.owner}/${this.repo}`
);
console.log(e.headers);
throw e;
});
}
}
export class GitHubRepositoryEnvironmentSecrets extends GitHubRepositorySecrets {
private readonly env: string;
constructor(githubToken: string, owner: string, repo: string, env: string) {
super(githubToken, owner, repo);
this.env = env;
}
async getRepoId() {
return (
await this.octokit
.request("GET /repos/{owner}/{repo}", {
owner: this.owner,
repo: this.repo,
})
.catch((e) => {
console.log(
`failed to fetch public key for ${this.owner}/${this.repo}`
);
console.log(e.headers);
throw e;
})
).data.id;
}
async publicKey() {
const repository_id = await this.getRepoId();
return (
await this.octokit
.request(
"GET /repositories/{repository_id}/environments/{environment_name}/secrets/public-key",
{
repository_id,
environment_name: this.env,
owner: this.owner,
repo: this.repo,
}
)
.catch((e) => {
console.log(
`failed to fetch public key for ${this.owner}/${this.repo}`
);
console.log(e.headers);
throw e;
})
).data;
}
async upsert(secret_name: string, encrypted_value: string, key_id: string) {
const repository_id = await this.getRepoId();
await this.octokit
.request(
"PUT /repositories/{repository_id}/environments/{environment_name}/secrets/{secret_name}",
{
repository_id,
environment_name: this.env,
secret_name,
encrypted_value,
key_id,
}
)
.catch((e) => {
console.log(
`failed to upsert secret ${secret_name} for ${this.owner}/${this.repo}`
);
console.log(e.headers);
throw e;
});
}
}
export class GitHubOrganizationSecrets implements Secrets {
private readonly octokit: Octokit;
private readonly organization: string;
constructor(githubToken: string, organization: string) {
this.octokit = new Octokit({ auth: githubToken });
this.organization = organization;
}
async publicKey() {
return (
await this.octokit
.request("GET /orgs/{org}/actions/secrets/public-key", {
org: this.organization,
})
.catch((e) => {
console.log(
`failed to fetch public key for organization ${this.organization}`
);
console.log(e.headers);
throw e;
})
).data;
}
async upsert(secret_name: string, encrypted_value: string, key_id: string) {
const oldSecret = await this.getSecret(secret_name);
const selected_repository_ids = await this.getSelectedRepositoryIdsOfSecret(
secret_name
).then((ids) => ids.map((id) => id.toString()));
await this.octokit
.request("PUT /orgs/{org}/actions/secrets/{secret_name}", {
org: this.organization,
secret_name,
encrypted_value,
key_id,
visibility: oldSecret.visibility,
selected_repository_ids,
})
.catch((e) => {
console.log(
`failed to upsert secret ${secret_name} for ${this.organization}`
);
console.log(e.headers);
throw e;
});
}
async getSecret(secret_name: string) {
try {
return (
await this.octokit.request(
"GET /orgs/{org}/actions/secrets/{secret_name}",
{
org: this.organization,
secret_name,
}
)
).data;
} catch (e) {
console.log(
`failed to get secret ${secret_name} for ${this.organization}`
);
throw e;
}
}
async getSelectedRepositoryIdsOfSecret(
secret_name: string
): Promise<Array<number>> {
const { data } = await this.octokit
.request("GET /orgs/{org}/actions/secrets/{secret_name}/repositories", {
org: this.organization,
secret_name,
})
.catch((e) => {
console.log(
`failed to get list of repositories using ${secret_name} for ${this.organization}`
);
console.log(e.headers);
throw e;
});
return data.repositories.map((r) => r.id);
}
}