-
Notifications
You must be signed in to change notification settings - Fork 1
/
cf-tlsa-acmesh.go
283 lines (229 loc) · 7.44 KB
/
cf-tlsa-acmesh.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// SPDX-License-Identifier: MIT
// Copyright (c) 2023 Erik Junsved
package main
import (
"bytes"
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"log"
"net/http"
"os"
)
const (
cloudflareAPI = "https://api.cloudflare.com/client/v4/zones/"
port = 25
protocol = "tcp"
// If the values below are modified,
// the generateCert function also needs
// to be modified to reflect the changes.
usage = 3
selector = 1
matchingType = 1
)
type tlsaRecordsResponse struct {
Result []tlsaRecord `json:"result"`
}
type tlsaRecord struct {
ID string `json:"id"`
Data tlsaData `json:"data"`
}
type tlsaData struct {
Certificate string `json:"certificate"`
MatchingType int `json:"matching_type"`
Selector int `json:"selector"`
Usage int `json:"usage"`
}
func main() {
requiredEnvVars := []string{"KEY_FILE", "KEY_FILE_NEXT", "ZONE_ID", "API_TOKEN", "DOMAIN"}
for _, envVar := range requiredEnvVars {
if os.Getenv(envVar) == "" {
log.Fatalln("Fatal:", envVar, "environment variable is not defined")
}
}
cert, err := generateCert(os.Getenv("KEY_FILE"))
if err != nil {
log.Fatalln("Fatal: failed to generate current cert:", err)
}
certNext, err := generateCert(os.Getenv("KEY_FILE_NEXT"))
if err != nil {
log.Fatalln("Fatal: failed to generate next cert:", err)
}
log.Println("Current cert:", cert)
log.Println("Next cert:", certNext)
tlsaRecords, err := getTLSARecords()
if err != nil {
log.Fatalln("Fatal: failed to get TLSA records:", err)
}
for i, record := range tlsaRecords {
log.Printf("DNS record %d: ID: %s, cert: %s\n", i+1, record.ID, record.Data.Certificate)
}
if len(tlsaRecords) != 2 {
log.Println("Incorrect number of DNS entries. Deleting them and generating new ones.")
err = deleteAll(tlsaRecords)
if err != nil {
log.Fatalln("Fatal: failed to delete all TLSA recors:", err)
}
err = addRequest(certNext)
if err != nil {
log.Fatalln("Fatal: failed to add TLSA record for current cert:", err)
}
err = addRequest(cert)
if err != nil {
log.Fatalln("Fatal: failed to add TLSA record for next cert:", err)
}
os.Exit(0)
}
switch {
case (checkData(tlsaRecords[0], cert) && checkData(tlsaRecords[1], certNext)) ||
(checkData(tlsaRecords[0], certNext) && checkData(tlsaRecords[1], cert)):
log.Println("Nothing to do!")
case checkData(tlsaRecords[0], cert):
err = modifyRequest(certNext, tlsaRecords[1].ID)
case checkData(tlsaRecords[0], certNext):
err = modifyRequest(cert, tlsaRecords[1].ID)
case checkData(tlsaRecords[1], cert):
err = modifyRequest(certNext, tlsaRecords[0].ID)
case checkData(tlsaRecords[1], certNext):
err = modifyRequest(cert, tlsaRecords[0].ID)
default:
err = modifyRequest(certNext, tlsaRecords[1].ID)
if err != nil {
break
}
err = modifyRequest(cert, tlsaRecords[0].ID)
}
if err != nil {
log.Fatalln("Fatal: failed to modify TLSA records:", err)
}
os.Exit(0)
}
func getTLSARecords() ([]tlsaRecord, error) {
zoneID := os.Getenv("ZONE_ID")
authToken := os.Getenv("API_TOKEN")
domain := os.Getenv("DOMAIN")
url := fmt.Sprintf("%s%s/dns_records?name=_%d._%s.%s", cloudflareAPI, zoneID, port, protocol, domain)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %v", err)
}
req.Header.Set("Authorization", "Bearer "+authToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to make HTTP request: %v", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Println("Error closing HTTP body", err)
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed reading response body: %v", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("recieved %d HTTP response status code for GET request, response body: %s", resp.StatusCode, string(body))
}
var response tlsaRecordsResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, fmt.Errorf("failed to decode JSON response: %v", err)
}
return response.Result, nil
}
func generateCert(keyPath string) (string, error) {
keyBytes, err := os.ReadFile(keyPath)
if err != nil {
return "", fmt.Errorf("failed to read key file: %v", err)
}
block, _ := pem.Decode(keyBytes)
if block == nil {
return "", fmt.Errorf("failed to decode PEM block from key file")
}
key, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
return "", fmt.Errorf("failed to parse private key: %v", err)
}
publicKeyBytes, err := x509.MarshalPKIXPublicKey(&key.PublicKey)
if err != nil {
return "", fmt.Errorf("failed to marshal public key: %v", err)
}
hash := sha256.New()
hash.Write(publicKeyBytes)
hashSum := hash.Sum(nil)
return hex.EncodeToString(hashSum), nil
}
func deleteAll(tlsaRecords []tlsaRecord) error {
zoneID, authToken := os.Getenv("ZONE_ID"), os.Getenv("API_TOKEN")
for _, record := range tlsaRecords {
log.Println("Deleting DNS record:", record.ID)
url := cloudflareAPI + zoneID + "/dns_records/" + record.ID
resp, err := makeHTTPRequest("DELETE", url, authToken, nil)
err = handleResponse(resp, err, "DELETE")
if err != nil {
return err
}
}
return nil
}
func addRequest(hash string) error {
log.Println("Adding DNS record with hash:", hash)
zoneID, authToken, domain := os.Getenv("ZONE_ID"), os.Getenv("API_TOKEN"), os.Getenv("DOMAIN")
url := cloudflareAPI + zoneID + "/dns_records"
payload := fmt.Sprintf(
`{"type":"TLSA","name":"_%d._%s.%s","data":{"usage":%d,"selector":%d,"matching_type":%d,"certificate":"%s"}}`,
port, protocol, domain, usage, selector, matchingType, hash)
resp, err := makeHTTPRequest("POST", url, authToken, []byte(payload))
return handleResponse(resp, err, "POST")
}
func modifyRequest(hash, id string) error {
log.Println("Modifying DNS record:", id, "with hash:", hash)
zoneID, authToken, domain := os.Getenv("ZONE_ID"), os.Getenv("API_TOKEN"), os.Getenv("DOMAIN")
url := cloudflareAPI + zoneID + "/dns_records/" + id
payload := fmt.Sprintf(
`{"type":"TLSA","name":"_%d._%s.%s","data":{"usage":%d,"selector":%d,"matching_type":%d,"certificate":"%s"}}`,
port, protocol, domain, usage, selector, matchingType, hash)
resp, err := makeHTTPRequest("PUT", url, authToken, []byte(payload))
return handleResponse(resp, err, "PUT")
}
func makeHTTPRequest(method, url, authToken string, payload []byte) (*http.Response, error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+authToken)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
return client.Do(req)
}
func handleResponse(resp *http.Response, err error, action string) error {
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Println("Error closing HTTP body:", err)
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed reading response body: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("recieved %d HTTP response status code for %s request, response body: %s", resp.StatusCode, action, string(body))
}
return nil
}
func checkData(record tlsaRecord, hash string) (correct bool) {
return record.Data.Usage == usage &&
record.Data.Selector == selector &&
record.Data.MatchingType == matchingType &&
record.Data.Certificate == hash
}