-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
148 lines (127 loc) · 3.3 KB
/
index.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
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
'use strict'
const debug = require('debug')(require('./package.json').name)
const {
DnsManagementClient
} = require('@azure/arm-dns')
const { loginWithServicePrincipalSecret } = require('@azure/ms-rest-nodeauth')
const TXT_TYPE = 'TXT'
class Challenge {
constructor ({ subscriptionId, clientId, clientSecret, azureDomain, TTL = 3600 } = {}) {
this.subscriptionId = subscriptionId
this.clientId = clientId
this.clientSecret = clientSecret
this.azureDomain = azureDomain
this.TTL = TTL
if (!subscriptionId) {
throw new Error('Missing subscriptionId')
}
if (!clientId) {
throw new Error('Missing clientId')
}
if (!clientSecret) {
throw new Error('Missing clientSecret')
}
if (!azureDomain) {
throw new Error('Missing azureDomain')
}
}
static create (config) {
return new Challenge({ ...config, ...this.options })
}
async init () {
return null
}
async zones () {
const { clientId, clientSecret, azureDomain, subscriptionId } = this
const creds = await loginWithServicePrincipalSecret(clientId, clientSecret, azureDomain)
this._dnsClient = new DnsManagementClient(creds, subscriptionId)
const _zones = await this._dnsClient.zones.list()
const zones = _zones.map(z => z.name)
this.__zoneToResourceGroup = _zones.reduce((map, zone) => ({
...map,
[`${zone.name}`]: zone.id.split('/')[4]
}), {})
debug({ mapping: this.__zoneToResourceGroup, zones })
return zones
}
async set ({
challenge: {
dnsZone,
dnsPrefix,
keyAuthorizationDigest,
dnsAuthorization,
altname
}
}) {
const resourceGroup = this.__zoneToResourceGroup[dnsZone]
if (!resourceGroup) throw new Error(`Could not find resource group for ${altname}`)
const thisTXT = keyAuthorizationDigest || dnsAuthorization
const nextTXTs = [thisTXT]
try {
const result = await this._dnsClient.recordSets.get(
resourceGroup,
dnsZone,
dnsPrefix,
TXT_TYPE
)
nextTXTs.push(result.txtRecords[0].value[0])
} catch (err) {}
await this._dnsClient.recordSets.createOrUpdate(
resourceGroup,
dnsZone,
dnsPrefix,
TXT_TYPE, {
tTL: this.TTL,
txtRecords: [...new Set(nextTXTs)].map(v => ({ value: [v] }))
}
)
return thisTXT
}
async get ({
challenge: {
dnsZone,
dnsPrefix,
dnsAuthorization
}
}) {
const resourceGroup = this.__zoneToResourceGroup[dnsZone]
try {
const result = await this._dnsClient.recordSets.get(
resourceGroup,
dnsZone,
dnsPrefix,
TXT_TYPE
)
const verified = result.txtRecords.find(txt => txt.value.includes(dnsAuthorization))
if (!verified) return null
return {
dnsAuthorization
}
} catch (_err) {
return null
}
}
async remove ({
challenge: {
dnsZone,
dnsPrefix
}
}) {
const resourceGroup = this.__zoneToResourceGroup[dnsZone]
try {
await this._dnsClient.recordSets.deleteMethod(
resourceGroup,
dnsZone,
dnsPrefix,
TXT_TYPE
)
} catch (error) {
debug({
msg: error.message,
reason: '404 - already gone'
})
}
return null
}
}
module.exports = Challenge