-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdoppler.js
115 lines (106 loc) · 3.5 KB
/
doppler.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
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
import https from "https";
import { VERSION } from "./meta.js";
/**
* Fetch secrets from Doppler the API
* @param {string} dopplerToken
* @param {string | null} [dopplerProject]
* @param {string | null} [dopplerConfig]
* @param {string} apiDomain
* @returns {Promise<Record<string, Record>>}
*/
export async function fetch(dopplerToken, dopplerProject, dopplerConfig, apiDomain) {
return new Promise(function (resolve, reject) {
const encodedAuthData = Buffer.from(`${dopplerToken}:`).toString("base64");
const authHeader = `Basic ${encodedAuthData}`;
const userAgent = `secrets-fetch-github-action/${VERSION}`;
const url = new URL(`https://${apiDomain}/v3/configs/config/secrets`);
if (dopplerProject && dopplerConfig) {
url.searchParams.append("project", dopplerProject);
url.searchParams.append("config", dopplerConfig);
}
https
.get(
url.href,
{
headers: {
Authorization: authHeader,
"user-agent": userAgent,
"accepts": "application/json",
},
},
(res) => {
let payload = "";
res.on("data", (data) => (payload += data));
res.on("end", () => {
if (res.statusCode === 200) {
resolve(JSON.parse(payload).secrets);
} else {
try {
const error = JSON.parse(payload).messages.join(" ");
reject(new Error(`Doppler API Error: ${error}`));
} catch (error) {
// In the event an upstream issue occurs and no JSON payload is supplied
reject(new Error(`Doppler API Error: ${res.statusCode} ${res.statusMessage}`));
}
}
});
}
)
.on("error", (error) => {
reject(new Error(`Doppler API Error: ${error}`));
});
});
}
/**
* Exchange an OIDC token for a short lived Doppler service account token
* @param {string} identityId
* @param {string} oidcToken
* @param {string} apiDomain
* @returns {Promise<string>}
*/
export async function oidcAuth(identityId, oidcToken, apiDomain) {
return new Promise(function (resolve, reject) {
const userAgent = `secrets-fetch-github-action/${VERSION}`;
const url = new URL(`https://${apiDomain}/v3/auth/oidc`);
const body = JSON.stringify({
identity: identityId,
token: oidcToken
});
const request = https
.request(
url.href,
{
headers: {
"user-agent": userAgent,
"accepts": "application/json",
"Content-Type": "application/json",
"Content-Length": body.length,
},
method: 'POST'
},
(res) => {
let payload = "";
res.on("data", (data) => (payload += data));
res.on("end", () => {
if (res.statusCode === 200) {
resolve(JSON.parse(payload).token);
} else {
try {
const error = JSON.parse(payload).messages.join(" ");
reject(new Error(`Doppler API Error: ${error}`));
} catch (error) {
// In the event an upstream issue occurs and no JSON payload is supplied
reject(new Error(`Doppler API Error: ${res.statusCode} ${res.statusMessage}`));
}
}
});
}
);
request
.on("error", (error) => {
reject(new Error(`Doppler API Error: ${error}`));
});
request.write(body);
request.end()
});
}