Skip to content

Commit

Permalink
Merge pull request #18 from lfaoro/add-token-refactor
Browse files Browse the repository at this point in the history
Add token refactor
  • Loading branch information
lfaoro committed Oct 16, 2021
2 parents 5fd6482 + c9fa07b commit ff60f98
Show file tree
Hide file tree
Showing 56 changed files with 65 additions and 12,067 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ idea.md
.dockerignore
.vscode/
.idea/
dist/
dist/
vendor/
18 changes: 11 additions & 7 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ backup:
stage: build
retry: 2
script: |
# CloudFlare API key is here:
# https://dash.cloudflare.com/profile -> Global API Key -> View
# Fetch your CloudFlare API token from here:
# https://dash.cloudflare.com/profile/api-tokens
# -> Create Token
# -> Edit zone DNS
# -> Permission: read
# -> Zone resources: Include -> All zones
docker run -t --rm \
-e CF_API_KEY="$CF_API_KEY" \
-e CF_API_EMAIL="$CF_API_EMAIL" \
lfaoro/flares --all > dns-backup-bind.txt
-e CF_API_TOKEN="$CF_API_TOKEN" \
lfaoro/flares --all > dns_backup.bind
artifacts:
untracked: true
# uncomment if needed (default is forever on gitlab.com)
# expire_in: 4 week
name: "backup-${date}"
name: "bck-${date}"
paths:
- dns-backup-bind.txt
- dns_backup.bind
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ files.

### Docker
```bash
# Fetch your CloudFlare API key from here:
# https://dash.cloudflare.com/profile/api-tokens -> Create Token with Zone.DNS permission

$ export CF_API_TOKEN=abcdef1234567890
# Fetch your CloudFlare API token from here:
# https://dash.cloudflare.com/profile/api-tokens
# -> Create Token
# -> Edit zone DNS
# -> Permission: read
# -> Zone resources: Include -> All zones

$ export CF_API_TOKEN="KClp4y8BgD2LQiz2..."

$ docker run -it --rm \
-e CF_API_TOKEN="$CF_API_TOKEN" \
Expand Down
15 changes: 11 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
module github.com/lfaoro/flares

go 1.12
go 1.17

require (
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0
github.com/urfave/cli v1.20.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
github.com/urfave/cli v1.22.5
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
)
26 changes: 20 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
21 changes: 11 additions & 10 deletions internal/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package cloudflare
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
Expand All @@ -19,24 +20,24 @@ import (

const (
errDomainNotFound = "cloudflare: domain not found"
errNoAuthorization = "cloudflare: missing required AuthKey, AuthEmail"
errNoAuthorization = "cloudflare: missing required API token ($ export CF_API_TOKEN=\"\")"
)

// Cloudflare sets up authorization to the API.
type Cloudflare struct {
API string
ApiToken string
Client http.Client
API string
Token string
Client http.Client
}

// New returns a Cloudflare client
func New(apiToken string) Cloudflare {
if apiToken == "" {
panic(errNoAuthorization)
func New(token string) Cloudflare {
if token == "" {
log.Fatal(errNoAuthorization)
}
client := Cloudflare{
API: "https://api.cloudflare.com/client/v4",
ApiToken: apiToken,
API: "https://api.cloudflare.com/client/v4",
Token: token,
Client: http.Client{
Timeout: time.Second * 30,
},
Expand Down Expand Up @@ -172,7 +173,7 @@ func (cf Cloudflare) zoneIDFor(domain string) (string, error) {
}

func (cf Cloudflare) setAuthHeaders(req *http.Request) {
req.Header.Add("Authorization", "Bearer "+cf.ApiToken)
req.Header.Add("Authorization", "Bearer "+cf.Token)
}

type response struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/cloudflare/cloudflare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestNew(t *testing.T) {
"token",
},
Cloudflare{
API: "https://api.cloudflare.com/client/v4",
ApiToken: "token",
API: "https://api.cloudflare.com/client/v4",
Token: "token",
Client: http.Client{
Timeout: time.Second * 30,
},
Expand Down
15 changes: 0 additions & 15 deletions vendor/github.com/davecgh/go-spew/LICENSE

This file was deleted.

145 changes: 0 additions & 145 deletions vendor/github.com/davecgh/go-spew/spew/bypass.go

This file was deleted.

38 changes: 0 additions & 38 deletions vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

This file was deleted.

Loading

0 comments on commit ff60f98

Please sign in to comment.