-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker-credentials.js
61 lines (56 loc) · 1.58 KB
/
docker-credentials.js
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
function credsToConfigJson(creds) {
const newCreds = {
auths: {}
};
for (let registry in creds.auths) {
const cred = creds.auths[registry];
const newCred = {};
newCred.Auth = btoa(cred.Username + ":" + cred.Password);
newCred.Email = cred.Email;
newCreds.auths[registry] = newCred;
}
return JSON.stringify(newCreds, null, " ");
}
function configJsonToImagePullSecret(configJson) {
const configJsonB64 = btoa(configJson);
const imagePullSecret =
'apiVersion: v1\n' +
'kind: Secret\n' +
'metadata:\n' +
' name: replace-before-injecting\n' +
'data:\n' +
' .dockerconfigjson: ' + configJsonB64 + '\n' +
'type: kubernetes.io/dockerconfigjson';
return imagePullSecret;
}
function imagePullSecretToConfigJson(kubeYaml) {
const lines = kubeYaml.split("\n");
for (let lidx in lines) {
const line = lines[lidx];
if (line.includes(".dockerconfigjson")) {
const parts = line.split(":");
if (parts.length == 2) {
const configJsonB64 = parts[1];
const configJson = atob(configJsonB64);
return configJson;
}
}
}
}
function configJsonToCreds(configJson) {
const creds = JSON.parse(configJson).auths;
const newCreds = [];
for (let idx in creds) {
const cred = creds[idx];
if (typeof(cred.Auth) !== 'undefined' && typeof(cred.Username) === 'undefined') {
const up = atob(cred.Auth);
const ups = up.split(":");
if (ups.length == 2) {
cred.Username = ups[0];
cred.Password = ups[1];
}
}
newCreds[idx] = cred;
}
return newCreds;
}