-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
137 lines (120 loc) · 3.65 KB
/
index.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
import fs from 'fs';
import * as k8s from '@kubernetes/client-node';
import { InstallationAccessTokenAuthentication } from '@octokit/auth-app';
import dotenv from 'dotenv';
import { Probot, Context } from 'probot';
if (process.env.NODE_ENV !== 'production') {
dotenv.config();
}
const EXPIRATION_THRESHOLD = 5 * 60000; // 5 minutes in milliseconds
const SECRET_NAME_PREFIX = 'probot-';
// K8s client for using k8s apis.
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sContext = kc.getCurrentContext();
const k8sNamespace = (() => {
if (k8sContext === 'inClusterContext') {
return fs.readFileSync(
'/var/run/secrets/kubernetes.io/serviceaccount/namespace',
'utf8'
);
} else {
const ctx = kc.getContextObject(k8sContext);
return ctx?.namespace || 'default';
}
})();
type ApiConstructor<T extends k8s.ApiType> = new (server: string) => T;
export const useApi = <T extends k8s.ApiType>(
apiClientType: ApiConstructor<T>
): T => kc.makeApiClient(apiClientType);
export const getNamespace = () => k8sNamespace;
export const APIS = k8s;
export const useK8sTokenStore = (app: Probot) => {
app.on(
'installation.created',
async (context: Context<'installation.created'>) => {
await createTokenSecret(context);
}
);
app.on(
'installation.deleted',
async (context: Context<'installation.deleted'>) => {
await deleteTokenSecret(context);
}
);
app.onAny(async (context) => {
await updateTokenSecret(context);
});
};
export const getTokenSecretName = (context: any) => {
return SECRET_NAME_PREFIX + context.payload.installation.id;
};
const unpackExceptionMessage = (err: any) => {
throw err?.body?.message || err;
};
const createSecretPayload = async (context: any) => {
const appAuth = (await context.octokit.auth({
type: 'installation',
})) as InstallationAccessTokenAuthentication;
// orgName may not exist in payload
const orgName =
context.payload.installation?.account?.login ||
context.payload.organization?.login;
return {
metadata: {
name: getTokenSecretName(context),
labels: {
'app.kubernetes.io/created-by': 'probot',
},
annotations: {
expiresAt: appAuth.expiresAt,
},
},
stringData: {
token: appAuth.token,
orgName: orgName,
},
} as k8s.V1Secret;
};
export const createTokenSecret = async (context: any) => {
return useApi(k8s.CoreV1Api)
.createNamespacedSecret(getNamespace(), await createSecretPayload(context))
.catch(unpackExceptionMessage);
};
export const deleteTokenSecret = async (context: any) => {
return useApi(k8s.CoreV1Api)
.deleteNamespacedSecret(
SECRET_NAME_PREFIX + context.payload.installation.id,
getNamespace()
)
.catch(unpackExceptionMessage);
};
export const updateTokenSecret = async (context: any) => {
const appSecret = await useApi(k8s.CoreV1Api)
.readNamespacedSecret(
SECRET_NAME_PREFIX + context.payload.installation.id,
k8sNamespace
)
.catch(unpackExceptionMessage);
const current_date = new Date();
const expiry_date = new Date(
appSecret.body?.metadata?.annotations?.expiresAt || 0
);
// check if token not expired
if (expiry_date.getTime() > current_date.getTime() + EXPIRATION_THRESHOLD) {
return Promise.resolve();
}
return useApi(k8s.CoreV1Api)
.patchNamespacedSecret(
SECRET_NAME_PREFIX + context.payload.installation.id,
getNamespace(),
await createSecretPayload(context),
undefined,
undefined,
undefined,
undefined,
undefined,
{ headers: { 'content-type': 'application/merge-patch+json' } }
)
.catch(unpackExceptionMessage);
};