Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Bearer tokens #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func (p *Provider) setRecord(ctx context.Context, zone string, record libdns.Rec

// Append a dot to get the absolute record name.
recAbsoluteName := record.Name + "."
if !strings.HasSuffix(recAbsoluteName, zone) {
recAbsoluteName = recAbsoluteName + zone
}

req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/%s", domain.DomainRecordsHref, recAbsoluteName, record.Type), nil)
if err != nil {
Expand Down Expand Up @@ -77,6 +80,9 @@ func (p *Provider) deleteRecord(ctx context.Context, zone string, record libdns.

// Append a dot to get the absolute record name.
recAbsoluteName := record.Name + "."
if !strings.HasSuffix(recAbsoluteName, zone) {
recAbsoluteName = recAbsoluteName + zone
}

req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/%s", domain.DomainRecordsHref, recAbsoluteName, record.Type), nil)
if err != nil {
Expand Down Expand Up @@ -105,6 +111,9 @@ func (p *Provider) deleteRecord(ctx context.Context, zone string, record libdns.
}

req, err = http.NewRequestWithContext(ctx, "PUT", fmt.Sprintf("%s/%s/%s", domain.DomainRecordsHref, recAbsoluteName, record.Type), bytes.NewReader(raw))
if err != nil {
return err
}
} else {
// if there is only one entry, we make sure that the value to delete is matching the one we found
// otherwise we may delete the wrong record
Expand Down Expand Up @@ -142,6 +151,9 @@ func (p *Provider) getDomain(ctx context.Context, zone string) (gandiDomain, err
}

req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.gandi.net/v5/livedns/domains/%s", fqdn), nil)
if err != nil {
return gandiDomain{}, err
}

var domain gandiDomain

Expand All @@ -155,7 +167,12 @@ func (p *Provider) getDomain(ctx context.Context, zone string) (gandiDomain, err
}

func (p *Provider) doRequest(req *http.Request, result interface{}) (gandiStatus, error) {
req.Header.Set("Authorization", fmt.Sprintf("Apikey %s", p.APIToken))
auth, err := p.getAuthHeader()
if err != nil {
return gandiStatus{}, err
}

req.Header.Set("Authorization", auth)
req.Header.Set("Accept", "application/json")

resp, err := http.DefaultClient.Do(req)
Expand Down Expand Up @@ -187,3 +204,16 @@ func (p *Provider) doRequest(req *http.Request, result interface{}) (gandiStatus

return gandiStatus{}, nil
}

func (p *Provider) getAuthHeader() (string, error) {
switch {
case p.APIToken != "":
return fmt.Sprintf("Apikey %s", p.APIToken), nil

case p.BearerToken != "":
return fmt.Sprintf("Bearer %s", p.BearerToken), nil

default:
return "", fmt.Errorf("no auth token configured")
}
}
16 changes: 0 additions & 16 deletions gandi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@ type gandiStatus struct {
Errors []gandiErrors `json:"errors"`
}

type gandiZone struct {
Retry int `json:"retry"`
UUID string `json:"uuid"`
ZoneHref string `json:"zone_href"`
Minimum int `json:"minimum"`
DomainsHref string `json:"domains_href"`
Refresh int `json:"refresh"`
ZoneRecordsHref string `json:"zone_records_href"`
Expire int `json:"expire"`
SharingID string `json:"sharing_id"`
Serial int `json:"serial"`
Email string `json:"email"`
PrimaryNS string `json:"primary_ns"`
Name string `json:"name"`
}

type gandiDomain struct {
Fqdn string `json:"fqdn"`
AutomaticSnapshots bool `json:"automatic_snapshots"`
Expand Down
3 changes: 2 additions & 1 deletion provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

// Provider implements the libdns interfaces for Gandi.
type Provider struct {
APIToken string `json:"api_token,omitempty"`
APIToken string `json:"api_token,omitempty"`
BearerToken string `json:"bearer_token,omitempty"`

domains map[string]gandiDomain
mutex sync.Mutex
Expand Down