forked from adyanth/cloudflare-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare_api.go
548 lines (477 loc) · 15.7 KB
/
cloudflare_api.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package controllers
import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/cloudflare/cloudflare-go"
"github.com/go-logr/logr"
)
// TXT_PREFIX is the prefix added to TXT records for whom the corresponding DNS records are managed by the operator.
const TXT_PREFIX = "_managed."
// CloudflareAPI config object holding all relevant fields to use the API
type CloudflareAPI struct {
Log logr.Logger
TunnelName string
TunnelId string
AccountName string
AccountId string
Domain string
APIToken string
APIKey string
APIEmail string
ValidAccountId string
ValidTunnelId string
ValidTunnelName string
ValidZoneId string
CloudflareClient *cloudflare.API
}
// CloudflareTunnelCredentialsFile object containing the fields that make up a Cloudflare Tunnel's credentials
type CloudflareTunnelCredentialsFile struct {
AccountTag string `json:"AccountTag"`
TunnelID string `json:"TunnelID"`
TunnelName string `json:"TunnelName"`
TunnelSecret string `json:"TunnelSecret"`
}
// DnsManagedRecordTxt object that represents each managed DNS record in a separate TXT record
type DnsManagedRecordTxt struct {
DnsId string // DnsId of the managed record
TunnelName string // TunnelName of the managed record
TunnelId string // TunnelId of the managed record
}
// CreateCloudflareTunnel creates a Cloudflare Tunnel and returns the tunnel Id and credentials file
func (c *CloudflareAPI) CreateCloudflareTunnel() (string, string, error) {
if _, err := c.GetAccountId(); err != nil {
c.Log.Error(err, "error code in getting account ID")
return "", "", err
}
// Generate 32 byte random string for tunnel secret
randSecret := make([]byte, 32)
if _, err := rand.Read(randSecret); err != nil {
return "", "", err
}
tunnelSecret := base64.StdEncoding.EncodeToString(randSecret)
params := cloudflare.TunnelCreateParams{
Name: c.TunnelName,
Secret: tunnelSecret,
// Indicates if this is a locally or remotely configured tunnel "local" or "cloudflare"
ConfigSrc: "local",
}
ctx := context.Background()
rc := cloudflare.AccountIdentifier(c.ValidAccountId)
tunnel, err := c.CloudflareClient.CreateTunnel(ctx, rc, params)
if err != nil {
c.Log.Error(err, "error creating tunnel")
return "", "", err
}
c.ValidTunnelId = tunnel.ID
c.ValidTunnelName = tunnel.Name
credentialsFile := CloudflareTunnelCredentialsFile{
AccountTag: c.ValidAccountId,
TunnelID: tunnel.ID,
TunnelName: tunnel.Name,
TunnelSecret: tunnelSecret,
}
// Marshal the tunnel credentials into a string
creds, err := json.Marshal(credentialsFile)
return tunnel.ID, string(creds), err
}
// DeleteCloudflareTunnel deletes a Cloudflare Tunnel
func (c *CloudflareAPI) DeleteCloudflareTunnel() error {
if err := c.ValidateAll(); err != nil {
c.Log.Error(err, "Error in validation")
return err
}
ctx := context.Background()
rc := cloudflare.AccountIdentifier(c.ValidAccountId)
// Deletes any inactive connections on a tunnel
err := c.CloudflareClient.CleanupTunnelConnections(ctx, rc, c.ValidTunnelId)
if err != nil {
c.Log.Error(err, "error cleaning tunnel connections", "tunnelId", c.TunnelId)
return err
}
ctx = context.Background()
err = c.CloudflareClient.DeleteTunnel(ctx, rc, c.ValidTunnelId)
if err != nil {
c.Log.Error(err, "error deleting tunnel", "tunnelId", c.TunnelId)
return err
}
return nil
}
// ValidateAll validates the contents of the CloudflareAPI struct
func (c *CloudflareAPI) ValidateAll() error {
c.Log.Info("In validation")
if _, err := c.GetAccountId(); err != nil {
return err
}
if _, err := c.GetTunnelId(); err != nil {
return err
}
if _, err := c.GetZoneId(); err != nil {
return err
}
c.Log.Info("Validation successful")
return nil
}
// GetAccountId gets AccountId from Account Name
func (c *CloudflareAPI) GetAccountId() (string, error) {
if c.ValidAccountId != "" {
return c.ValidAccountId, nil
}
if c.AccountId == "" && c.AccountName == "" {
err := fmt.Errorf("both account ID and Name cannot be empty")
c.Log.Error(err, "Both accountId and accountName cannot be empty")
return "", err
}
if c.validateAccountId() {
c.ValidAccountId = c.AccountId
} else {
c.Log.Info("Account ID failed, falling back to Account Name")
accountIdFromName, err := c.getAccountIdByName()
if err != nil {
return "", fmt.Errorf("error fetching Account ID by Account Name")
}
c.ValidAccountId = accountIdFromName
}
return c.ValidAccountId, nil
}
func (c CloudflareAPI) validateAccountId() bool {
if c.AccountId == "" {
c.Log.Info("Account ID not provided")
return false
}
ctx := context.Background()
account, _, err := c.CloudflareClient.Account(ctx, c.AccountId)
if err != nil {
c.Log.Error(err, "error retrieving account details", "accountId", c.AccountId)
return false
}
return account.ID == c.AccountId
}
func (c *CloudflareAPI) getAccountIdByName() (string, error) {
ctx := context.Background()
params := cloudflare.AccountsListParams{
Name: c.AccountName,
}
accounts, _, err := c.CloudflareClient.Accounts(ctx, params)
if err != nil {
c.Log.Error(err, "error listing accounts", "accountName", c.AccountName)
}
switch len(accounts) {
case 0:
err := fmt.Errorf("no account in response")
c.Log.Error(err, "found no account, check accountName", "accountName", c.AccountName)
return "", err
case 1:
return accounts[0].ID, nil
default:
err := fmt.Errorf("more than one account in response")
c.Log.Error(err, "found more than one account, check accountName", "accountName", c.AccountName)
return "", err
}
}
// GetTunnelId gets Tunnel Id from available information
func (c *CloudflareAPI) GetTunnelId() (string, error) {
if c.ValidTunnelId != "" {
return c.ValidTunnelId, nil
}
if c.TunnelId == "" && c.TunnelName == "" {
err := fmt.Errorf("both tunnel ID and Name cannot be empty")
c.Log.Error(err, "Both tunnelId and tunnelName cannot be empty")
return "", err
}
if c.validateTunnelId() {
c.ValidTunnelId = c.TunnelId
return c.TunnelId, nil
}
c.Log.Info("Tunnel ID failed, falling back to Tunnel Name")
tunnelIdFromName, err := c.getTunnelIdByName()
if err != nil {
return "", fmt.Errorf("error fetching Tunnel ID by Tunnel Name")
}
c.ValidTunnelId = tunnelIdFromName
c.ValidTunnelName = c.TunnelName
return c.ValidTunnelId, nil
}
func (c *CloudflareAPI) validateTunnelId() bool {
if c.TunnelId == "" {
c.Log.Info("Tunnel ID not provided")
return false
}
if _, err := c.GetAccountId(); err != nil {
c.Log.Error(err, "error in getting account ID")
return false
}
ctx := context.Background()
rc := cloudflare.AccountIdentifier(c.ValidAccountId)
tunnel, err := c.CloudflareClient.GetTunnel(ctx, rc, c.TunnelId)
if err != nil {
c.Log.Error(err, "error retrieving tunnel", "tunnelId", c.TunnelId)
return false
}
c.ValidTunnelName = tunnel.Name
return tunnel.ID == c.TunnelId
}
func (c *CloudflareAPI) getTunnelIdByName() (string, error) {
if _, err := c.GetAccountId(); err != nil {
c.Log.Error(err, "error in getting account ID")
return "", err
}
ctx := context.Background()
rc := cloudflare.AccountIdentifier(c.ValidAccountId)
params := cloudflare.TunnelListParams{
Name: c.TunnelName,
}
tunnels, _, err := c.CloudflareClient.ListTunnels(ctx, rc, params)
if err != nil {
c.Log.Error(err, "error listing tunnels by name", "tunnelName", c.TunnelName)
return "", err
}
switch len(tunnels) {
case 0:
err := fmt.Errorf("no tunnel in response")
c.Log.Error(err, "found no tunnel, check tunnelName", "tunnelName", c.TunnelName)
return "", err
case 1:
c.ValidTunnelName = tunnels[0].Name
return tunnels[0].ID, nil
default:
err := fmt.Errorf("more than one tunnel in response")
c.Log.Error(err, "found more than one tunnel, check tunnelName", "tunnelName", c.TunnelName)
return "", err
}
}
// GetTunnelCreds gets Tunnel Credentials from Tunnel secret
func (c *CloudflareAPI) GetTunnelCreds(tunnelSecret string) (string, error) {
if _, err := c.GetAccountId(); err != nil {
c.Log.Error(err, "error in getting account ID")
return "", err
}
if _, err := c.GetTunnelId(); err != nil {
c.Log.Error(err, "error in getting tunnel ID")
return "", err
}
creds, err := json.Marshal(map[string]string{
"AccountTag": c.ValidAccountId,
"TunnelSecret": tunnelSecret,
"TunnelID": c.ValidTunnelId,
"TunnelName": c.ValidTunnelName,
})
return string(creds), err
}
// GetZoneId gets Zone Id from DNS domain
func (c *CloudflareAPI) GetZoneId() (string, error) {
if c.ValidZoneId != "" {
return c.ValidZoneId, nil
}
if c.Domain == "" {
err := fmt.Errorf("domain cannot be empty")
c.Log.Error(err, "Domain cannot be empty")
return "", err
}
zoneIdFromName, err := c.getZoneIdByName()
if err != nil {
return "", fmt.Errorf("error fetching Zone ID by Zone Name")
}
c.ValidZoneId = zoneIdFromName
return c.ValidZoneId, nil
}
func (c *CloudflareAPI) getZoneIdByName() (string, error) {
ctx := context.Background()
zones, err := c.CloudflareClient.ListZones(ctx, c.Domain)
if err != nil {
c.Log.Error(err, "error listing zones, check domain", "domain", c.Domain)
return "", err
}
switch len(zones) {
case 0:
err := fmt.Errorf("no zone in response")
c.Log.Error(err, "found no zone, check domain", "domain", c.Domain, "zones", zones)
return "", err
case 1:
return zones[0].ID, nil
default:
err := fmt.Errorf("more than one zone in response")
c.Log.Error(err, "found more than one zone, check domain", "domain", c.Domain)
return "", err
}
}
// Been a while writing Go... returns a pointer to a type
func ptr[T any](v T) *T {
return &v
}
// InsertOrUpdateCName upsert DNS CNAME record for the given FQDN to point to the tunnel
func (c *CloudflareAPI) InsertOrUpdateCName(fqdn, dnsId string) (string, error) {
ctx := context.Background()
rc := cloudflare.ZoneIdentifier(c.ValidZoneId)
if dnsId != "" {
c.Log.Info("Updating existing record", "fqdn", fqdn, "dnsId", dnsId)
updateParams := cloudflare.UpdateDNSRecordParams{
ID: dnsId,
Type: "CNAME",
Name: fqdn,
Content: fmt.Sprintf("%s.cfargotunnel.com", c.ValidTunnelId),
Comment: "Managed by cloudflare-operator",
TTL: 1, // Automatic TTL
Proxied: ptr(true), // For Cloudflare tunnels
}
err := c.CloudflareClient.UpdateDNSRecord(ctx, rc, updateParams)
if err != nil {
c.Log.Error(err, "error code in setting/updating DNS record, check fqdn", "fqdn", fqdn)
return "", err
}
c.Log.Info("DNS record updated successfully", "fqdn", fqdn)
return dnsId, nil
} else {
c.Log.Info("Inserting DNS record", "fqdn", fqdn)
createParams := cloudflare.CreateDNSRecordParams{
Type: "CNAME",
Name: fqdn,
Content: fmt.Sprintf("%s.cfargotunnel.com", c.ValidTunnelId),
Comment: "Managed by cloudflare-operator",
TTL: 1, // Automatic TTL
Proxied: ptr(true), // For Cloudflare tunnels
}
resp, err := c.CloudflareClient.CreateDNSRecord(ctx, rc, createParams)
if err != nil {
c.Log.Error(err, "error creating DNS record, check fqdn", "fqdn", fqdn)
return "", err
}
c.Log.Info("DNS record created successfully", "fqdn", fqdn)
return resp.Result.ID, nil
}
}
// DeleteDNSId deletes DNS entry for the given dnsId
func (c *CloudflareAPI) DeleteDNSId(fqdn, dnsId string, created bool) error {
// Do not delete if we did not create the DNS in this cycle
if !created {
return nil
}
ctx := context.Background()
rc := cloudflare.ZoneIdentifier(c.ValidZoneId)
err := c.CloudflareClient.DeleteDNSRecord(ctx, rc, dnsId)
if err != nil {
c.Log.Error(err, "error deleting DNS record, check fqdn", "dnsId", dnsId, "fqdn", fqdn)
return err
}
return nil
}
// GetDNSCNameId returns the ID of the CNAME record requested
func (c *CloudflareAPI) GetDNSCNameId(fqdn string) (string, error) {
if _, err := c.GetZoneId(); err != nil {
c.Log.Error(err, "error in getting Zone ID")
return "", err
}
ctx := context.Background()
rc := cloudflare.ZoneIdentifier(c.ValidZoneId)
params := cloudflare.ListDNSRecordsParams{
Type: "CNAME",
Name: fqdn,
}
records, _, err := c.CloudflareClient.ListDNSRecords(ctx, rc, params)
if err != nil {
c.Log.Error(err, "error listing DNS records, check fqdn", "fqdn", fqdn)
return "", err
}
switch len(records) {
case 0:
err := fmt.Errorf("no records returned")
c.Log.Info("no records returned for fqdn", "fqdn", fqdn)
return "", err
case 1:
return records[0].ID, nil
default:
err := fmt.Errorf("multiple records returned")
c.Log.Error(err, "multiple records returned for fqdn", "fqdn", fqdn)
return "", err
}
}
// GetManagedDnsTxt gets the TXT record corresponding to the fqdn
func (c *CloudflareAPI) GetManagedDnsTxt(fqdn string) (string, DnsManagedRecordTxt, bool, error) {
if _, err := c.GetZoneId(); err != nil {
c.Log.Error(err, "error in getting Zone ID")
return "", DnsManagedRecordTxt{}, false, err
}
ctx := context.Background()
rc := cloudflare.ZoneIdentifier(c.ValidZoneId)
params := cloudflare.ListDNSRecordsParams{
Type: "TXT",
Name: fmt.Sprintf("%s%s", TXT_PREFIX, fqdn),
}
records, _, err := c.CloudflareClient.ListDNSRecords(ctx, rc, params)
if err != nil {
c.Log.Error(err, "error listing DNS records, check fqdn", "fqdn", fqdn)
return "", DnsManagedRecordTxt{}, false, err
}
switch len(records) {
case 0:
c.Log.Info("no TXT records returned for fqdn", "fqdn", fqdn)
return "", DnsManagedRecordTxt{}, true, nil
case 1:
var dnsTxtResponse DnsManagedRecordTxt
if err := json.Unmarshal([]byte(records[0].Content), &dnsTxtResponse); err != nil {
// TXT record exists, but not in JSON
c.Log.Error(err, "could not read TXT content in getting zoneId, check domain", "domain", c.Domain)
return records[0].ID, dnsTxtResponse, false, err
} else if dnsTxtResponse.TunnelId == c.ValidTunnelId {
// TXT record exists and controlled by our tunnel
return records[0].ID, dnsTxtResponse, true, nil
}
default:
err := fmt.Errorf("multiple records returned")
c.Log.Error(err, "multiple TXT records returned for fqdn", "fqdn", fqdn)
return "", DnsManagedRecordTxt{}, false, err
}
return "", DnsManagedRecordTxt{}, false, err
}
// InsertOrUpdateTXT upsert DNS TXT record for the given FQDN to point to the tunnel
func (c *CloudflareAPI) InsertOrUpdateTXT(fqdn, txtId, dnsId string) error {
content, err := json.Marshal(DnsManagedRecordTxt{
DnsId: dnsId,
TunnelId: c.ValidTunnelId,
TunnelName: c.ValidTunnelName,
})
if err != nil {
c.Log.Error(err, "error marhsalling txt record json", "fqdn", fqdn)
return err
}
ctx := context.Background()
rc := cloudflare.ZoneIdentifier(c.ValidZoneId)
if txtId != "" {
c.Log.Info("Updating existing TXT record", "fqdn", fqdn, "dnsId", dnsId, "txtId", txtId)
updateParams := cloudflare.UpdateDNSRecordParams{
ID: txtId,
Type: "TXT",
Name: fmt.Sprintf("%s%s", TXT_PREFIX, fqdn),
Content: string(content),
Comment: "Managed by cloudflare-operator",
TTL: 1, // Automatic TTL
Proxied: ptr(false), // TXT cannot be proxied
}
err := c.CloudflareClient.UpdateDNSRecord(ctx, rc, updateParams)
if err != nil {
c.Log.Error(err, "error in updating DNS record, check fqdn", "fqdn", fqdn)
return err
}
c.Log.Info("DNS record updated successfully", "fqdn", fqdn)
return nil
} else {
c.Log.Info("Inserting DNS TXT record", "fqdn", fqdn)
createParams := cloudflare.CreateDNSRecordParams{
Type: "TXT",
Name: fmt.Sprintf("%s%s", TXT_PREFIX, fqdn),
Content: string(content),
Comment: "Managed by cloudflare-operator",
TTL: 1, // Automatic TTL
Proxied: ptr(false), // For Cloudflare tunnels
}
_, err := c.CloudflareClient.CreateDNSRecord(ctx, rc, createParams)
if err != nil {
c.Log.Error(err, "error creating DNS record, check fqdn", "fqdn", fqdn)
return err
}
c.Log.Info("DNS TXT record created successfully", "fqdn", fqdn)
return nil
}
}