Skip to content

Commit

Permalink
cloudflare: support reading API token from file
Browse files Browse the repository at this point in the history
  • Loading branch information
cxuu committed Apr 18, 2023
1 parent eaabf71 commit c5a684d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/tutorials/cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Snippet from [Cloudflare - Getting Started](https://api.cloudflare.com/#getting-
API Token will be preferred for authentication if `CF_API_TOKEN` environment variable is set.
Otherwise `CF_API_KEY` and `CF_API_EMAIL` should be set to run ExternalDNS with Cloudflare.
You may provide the Cloudflare API token through a file by setting the
`CF_API_TOKEN="file:/path/to/token"`.

When using API Token authentication, the token should be granted Zone `Read`, DNS `Edit` privileges, and access to `All zones`.

Expand Down
12 changes: 11 additions & 1 deletion provider/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package cloudflare
import (
"context"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"

cloudflare "github.com/cloudflare/cloudflare-go"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -155,7 +157,15 @@ func NewCloudFlareProvider(domainFilter endpoint.DomainFilter, zoneIDFilter prov
err error
)
if os.Getenv("CF_API_TOKEN") != "" {
config, err = cloudflare.NewWithAPIToken(os.Getenv("CF_API_TOKEN"))
token := os.Getenv("CF_API_TOKEN")
if strings.HasPrefix(token, "file:") {
tokenBytes, err := ioutil.ReadFile(strings.TrimPrefix(token, "file:"))
if err != nil {
return nil, fmt.Errorf("failed to read CF_API_TOKEN from file: %v", err)
}
token = string(tokenBytes)
}
config, err = cloudflare.NewWithAPIToken(token)
} else {
config, err = cloudflare.New(os.Getenv("CF_API_KEY"), os.Getenv("CF_API_EMAIL"))
}
Expand Down

0 comments on commit c5a684d

Please sign in to comment.