gandi: use PAT token instead

https://api.gandi.net/docs/authentication/
This commit is contained in:
Girish Ramakrishnan
2024-10-08 17:06:53 +02:00
parent d68d5d5c51
commit ce5a2b1f0a
8 changed files with 141 additions and 66 deletions
+25 -14
View File
@@ -36,6 +36,24 @@ function injectPrivateFields(newConfig, currentConfig) {
if (newConfig.token === constants.SECRET_PLACEHOLDER) newConfig.token = currentConfig.token;
}
function createRequest(method, url, domainConfig) {
assert.strictEqual(typeof method, 'string');
assert.strictEqual(typeof url, 'string');
assert.strictEqual(typeof domainConfig, 'object');
const request = superagent(method, url).timeout(30 * 1000).ok(() => true);
// https://api.gandi.net/docs/authentication/
if (domainConfig.tokenType === 'ApiKey') {
request.set('X-Api-Key', domainConfig.token);
request.set('Authorization', `Apikey ${domainConfig.token}`);
} else { // PAT
request.set('Authorization', `Bearer ${domainConfig.token}`);
}
return request;
}
async function upsert(domainObject, location, type, values) {
assert.strictEqual(typeof domainObject, 'object');
assert.strictEqual(typeof location, 'string');
@@ -53,11 +71,8 @@ async function upsert(domainObject, location, type, values) {
'rrset_values': values // for mx records, value is already of the '<priority> <server>' format
};
const [error, response] = await safe(superagent.put(`${GANDI_API}/domains/${zoneName}/records/${name}/${type}`)
.set('X-Api-Key', domainConfig.token)
.timeout(30 * 1000)
.send(data)
.ok(() => true));
const [error, response] = await safe(createRequest('PUT', `${GANDI_API}/domains/${zoneName}/records/${name}/${type}`, domainConfig)
.send(data));
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error.message);
if (response.statusCode === 403 || response.statusCode === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
@@ -76,10 +91,7 @@ async function get(domainObject, location, type) {
debug(`get: ${name} in zone ${zoneName} of type ${type}`);
const [error, response] = await safe(superagent.get(`${GANDI_API}/domains/${zoneName}/records/${name}/${type}`)
.set('X-Api-Key', domainConfig.token)
.timeout(30 * 1000)
.ok(() => true));
const [error, response] = await safe(createRequest('GET', `${GANDI_API}/domains/${zoneName}/records/${name}/${type}`, domainConfig));
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error.message);
if (response.statusCode === 403 || response.statusCode === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
@@ -101,10 +113,7 @@ async function del(domainObject, location, type, values) {
debug(`del: ${name} in zone ${zoneName} of type ${type} with values ${JSON.stringify(values)}`);
const [error, response] = await safe(superagent.del(`${GANDI_API}/domains/${zoneName}/records/${name}/${type}`)
.set('X-Api-Key', domainConfig.token)
.timeout(30 * 1000)
.ok(() => true));
const [error, response] = await safe(createRequest('DELETE', `${GANDI_API}/domains/${zoneName}/records/${name}/${type}`, domainConfig));
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error.message);
if (response.statusCode === 404) return;
@@ -131,9 +140,11 @@ async function verifyDomainConfig(domainObject) {
zoneName = domainObject.zoneName;
if (!domainConfig.token || typeof domainConfig.token !== 'string') throw new BoxError(BoxError.BAD_FIELD, 'token must be a non-empty string');
if (domainConfig.tokenType !== 'PAT' && domainConfig.tokenType !== 'ApiKey') throw new BoxError(BoxError.BAD_FIELD, 'tokenType is required');
const credentials = {
token: domainConfig.token
token: domainConfig.token,
tokenType: domainConfig.tokenType,
};
const ip = '127.0.0.1';