2019-09-25 04:38:13 -05:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-26 16:15:20 -05:00
|
|
|
"context"
|
2023-05-24 05:21:29 -05:00
|
|
|
"errors"
|
2019-09-25 04:38:13 -05:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflare-go"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/dnsdisc"
|
2022-06-27 11:22:36 -05:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-09-25 04:38:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-06-27 11:22:36 -05:00
|
|
|
cloudflareTokenFlag = &cli.StringFlag{
|
|
|
|
Name: "token",
|
|
|
|
Usage: "CloudFlare API token",
|
|
|
|
EnvVars: []string{"CLOUDFLARE_API_TOKEN"},
|
2019-09-25 04:38:13 -05:00
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
cloudflareZoneIDFlag = &cli.StringFlag{
|
2019-09-25 04:38:13 -05:00
|
|
|
Name: "zoneid",
|
|
|
|
Usage: "CloudFlare Zone ID (optional)",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type cloudflareClient struct {
|
|
|
|
*cloudflare.API
|
|
|
|
zoneID string
|
|
|
|
}
|
|
|
|
|
|
|
|
// newCloudflareClient sets up a CloudFlare API client from command line flags.
|
|
|
|
func newCloudflareClient(ctx *cli.Context) *cloudflareClient {
|
|
|
|
token := ctx.String(cloudflareTokenFlag.Name)
|
|
|
|
if token == "" {
|
2023-05-24 05:21:29 -05:00
|
|
|
exit(errors.New("need cloudflare API token to proceed"))
|
2019-09-25 04:38:13 -05:00
|
|
|
}
|
|
|
|
api, err := cloudflare.NewWithAPIToken(token)
|
|
|
|
if err != nil {
|
|
|
|
exit(fmt.Errorf("can't create Cloudflare client: %v", err))
|
|
|
|
}
|
|
|
|
return &cloudflareClient{
|
|
|
|
API: api,
|
|
|
|
zoneID: ctx.String(cloudflareZoneIDFlag.Name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deploy uploads the given tree to CloudFlare DNS.
|
|
|
|
func (c *cloudflareClient) deploy(name string, t *dnsdisc.Tree) error {
|
|
|
|
if err := c.checkZone(name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
records := t.ToTXT(name)
|
|
|
|
return c.uploadRecords(name, records)
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkZone verifies permissions on the CloudFlare DNS Zone for name.
|
|
|
|
func (c *cloudflareClient) checkZone(name string) error {
|
|
|
|
if c.zoneID == "" {
|
|
|
|
log.Info(fmt.Sprintf("Finding CloudFlare zone ID for %s", name))
|
|
|
|
id, err := c.ZoneIDByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.zoneID = id
|
|
|
|
}
|
|
|
|
log.Info(fmt.Sprintf("Checking Permissions on zone %s", c.zoneID))
|
2021-03-26 16:15:20 -05:00
|
|
|
zone, err := c.ZoneDetails(context.Background(), c.zoneID)
|
2019-09-25 04:38:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(name, "."+zone.Name) {
|
|
|
|
return fmt.Errorf("CloudFlare zone name %q does not match name %q to be deployed", zone.Name, name)
|
|
|
|
}
|
cmd/devp2p: require dns:read, dns:edit permissions for cloudflare deploy (#30326)
This PR adds the `dns:read` and `dns:edit` permissions to the required
set of permissions checked before deploying an ENR tree to Cloudflare.
These permissions are necessary for a successful publish.
**Background**:
The current logic for `devp2p dns to-cloudflare` checks for `zone:edit`
and `zone:read` permissions. However, when running the command with only
these two permissions, the following error occurs:
```
wrong permissions on zone REMOVED-ZONE: map[#zone:edit:false #zone:read:true]
```
Adding `zone:read` and `zone:edit` to the API token led to a different
error:
```
INFO [08-19|14:06:16.782] Retrieving existing TXT records on pos-nodes.hardfork.dev
Authentication error (10000)
```
This suggested that additional permissions were required. I added
`dns:read`, but encountered another error:
```
INFO [08-19|14:11:42.342] Retrieving existing TXT records on pos-nodes.hardfork.dev
INFO [08-19|14:11:42.851] Updating DNS entries
failed to publish REMOVED.pos-nodes.hardfork.dev: Authentication error (10000)
```
Finally, after adding both `dns:read` and `dns:edit` permissions, the
command executed successfully with the following output:
```
INFO [08-19|14:13:07.677] Checking Permissions on zone REMOVED-ZONE
INFO [08-19|14:13:08.014] Retrieving existing TXT records on pos-nodes.hardfork.dev
INFO [08-19|14:13:08.440] Updating DNS entries
INFO [08-19|14:13:08.440] "Updating pos-nodes.hardfork.dev from \"enrtree-root:v1 e=FSED3EDKEKRDDFMCLP746QY6CY l=FDXN3SN67NA5DKA4J2GOK7BVQI seq=1 sig=Glja2c9RviRqOpaaHR0MnHsQwU76nJXadJwFeiXpp8MRTVIhvL0LIireT0yE3ETZArGEmY5Ywz3FVHZ3LR5JTAE\" to \"enrtree-root:v1 e=AB66M4ULYD5OYN4XFFCPVZRLUM l=FDXN3SN67NA5DKA4J2GOK7BVQI seq=1 sig=H8cqDzu0FAzBplK4g3yudhSaNtszIebc2aj4oDm5a5ZE5PAg-xpCnQgVE_53CsgsqQpalD9byafx_FrUT61sagA\""
INFO [08-19|14:13:16.932] Updated DNS entries new=32 updated=1 untouched=100
INFO [08-19|14:13:16.932] Deleting stale DNS entries
INFO [08-19|14:13:24.663] Deleted stale DNS entries count=31
```
With this PR, the required permissions for deploying an ENR tree to
Cloudflare now include `zone:read`, `zone:edit`, `dns:read`, and
`dns:edit`. The initial check now includes all of the necessary
permissions and indicates in the error message which permissions are
missing:
```
INFO [08-19|14:17:20.339] Checking Permissions on zone REMOVED-ZONE
wrong permissions on zone REMOVED-ZONE: map[#dns_records:edit:false #dns_records:read:false #zone:edit:false #zone:read:true]
```
2024-08-20 08:59:16 -05:00
|
|
|
// Necessary permissions for Cloudlare management - Zone:Read, DNS:Read, Zone:Edit, DNS:Edit
|
|
|
|
needPerms := map[string]bool{"#zone:edit": false, "#zone:read": false, "#dns_records:read": false, "#dns_records:edit": false}
|
2019-09-25 04:38:13 -05:00
|
|
|
for _, perm := range zone.Permissions {
|
|
|
|
if _, ok := needPerms[perm]; ok {
|
|
|
|
needPerms[perm] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, ok := range needPerms {
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("wrong permissions on zone %s: %v", c.zoneID, needPerms)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// uploadRecords updates the TXT records at a particular subdomain. All non-root records
|
|
|
|
// will have a TTL of "infinity" and all existing records not in the new map will be
|
|
|
|
// nuked!
|
|
|
|
func (c *cloudflareClient) uploadRecords(name string, records map[string]string) error {
|
|
|
|
// Convert all names to lowercase.
|
|
|
|
lrecords := make(map[string]string, len(records))
|
|
|
|
for name, r := range records {
|
|
|
|
lrecords[strings.ToLower(name)] = r
|
|
|
|
}
|
|
|
|
records = lrecords
|
|
|
|
|
|
|
|
log.Info(fmt.Sprintf("Retrieving existing TXT records on %s", name))
|
2023-10-13 09:08:38 -05:00
|
|
|
entries, _, err := c.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(c.zoneID), cloudflare.ListDNSRecordsParams{Type: "TXT"})
|
2019-09-25 04:38:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
existing := make(map[string]cloudflare.DNSRecord)
|
|
|
|
for _, entry := range entries {
|
|
|
|
if !strings.HasSuffix(entry.Name, name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
existing[strings.ToLower(entry.Name)] = entry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the new records and inject anything missing.
|
2023-02-27 04:36:26 -06:00
|
|
|
log.Info("Updating DNS entries")
|
|
|
|
created := 0
|
|
|
|
updated := 0
|
|
|
|
skipped := 0
|
2019-09-25 04:38:13 -05:00
|
|
|
for path, val := range records {
|
|
|
|
old, exists := existing[path]
|
|
|
|
if !exists {
|
|
|
|
// Entry is unknown, push a new one to Cloudflare.
|
2023-02-27 04:36:26 -06:00
|
|
|
log.Debug(fmt.Sprintf("Creating %s = %q", path, val))
|
|
|
|
created++
|
2020-01-17 04:32:29 -06:00
|
|
|
ttl := rootTTL
|
2019-09-25 04:38:13 -05:00
|
|
|
if path != name {
|
2021-11-11 10:07:11 -06:00
|
|
|
ttl = treeNodeTTLCloudflare // Max TTL permitted by Cloudflare
|
2019-09-25 04:38:13 -05:00
|
|
|
}
|
2023-10-13 09:08:38 -05:00
|
|
|
record := cloudflare.CreateDNSRecordParams{Type: "TXT", Name: path, Content: val, TTL: ttl}
|
|
|
|
_, err = c.CreateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(c.zoneID), record)
|
2019-09-25 04:38:13 -05:00
|
|
|
} else if old.Content != val {
|
|
|
|
// Entry already exists, only change its content.
|
2023-04-19 05:46:56 -05:00
|
|
|
log.Info(fmt.Sprintf("Updating %s from %q to %q", path, old.Content, val))
|
2023-02-27 04:36:26 -06:00
|
|
|
updated++
|
2023-10-13 09:08:38 -05:00
|
|
|
|
|
|
|
record := cloudflare.UpdateDNSRecordParams{
|
|
|
|
Type: old.Type,
|
|
|
|
Name: old.Name,
|
|
|
|
Content: val,
|
|
|
|
Data: old.Data,
|
|
|
|
ID: old.ID,
|
|
|
|
Priority: old.Priority,
|
|
|
|
TTL: old.TTL,
|
|
|
|
Proxied: old.Proxied,
|
|
|
|
Tags: old.Tags,
|
|
|
|
}
|
|
|
|
_, err = c.UpdateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(c.zoneID), record)
|
2019-09-25 04:38:13 -05:00
|
|
|
} else {
|
2023-02-27 04:36:26 -06:00
|
|
|
skipped++
|
2021-03-26 16:15:20 -05:00
|
|
|
log.Debug(fmt.Sprintf("Skipping %s = %q", path, val))
|
2019-09-25 04:38:13 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to publish %s: %v", path, err)
|
|
|
|
}
|
|
|
|
}
|
2023-02-27 04:36:26 -06:00
|
|
|
log.Info("Updated DNS entries", "new", created, "updated", updated, "untouched", skipped)
|
2019-09-25 04:38:13 -05:00
|
|
|
// Iterate over the old records and delete anything stale.
|
2023-02-27 04:36:26 -06:00
|
|
|
deleted := 0
|
|
|
|
log.Info("Deleting stale DNS entries")
|
2019-09-25 04:38:13 -05:00
|
|
|
for path, entry := range existing {
|
|
|
|
if _, ok := records[path]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Stale entry, nuke it.
|
2023-02-27 04:36:26 -06:00
|
|
|
log.Debug(fmt.Sprintf("Deleting %s = %q", path, entry.Content))
|
|
|
|
deleted++
|
2023-10-13 09:08:38 -05:00
|
|
|
if err := c.DeleteDNSRecord(context.Background(), cloudflare.ZoneIdentifier(c.zoneID), entry.ID); err != nil {
|
2019-09-25 04:38:13 -05:00
|
|
|
return fmt.Errorf("failed to delete %s: %v", path, err)
|
|
|
|
}
|
|
|
|
}
|
2023-02-27 04:36:26 -06:00
|
|
|
log.Info("Deleted stale DNS entries", "count", deleted)
|
2019-09-25 04:38:13 -05:00
|
|
|
return nil
|
|
|
|
}
|