Skip to content

Commit

Permalink
fix linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Bystritskiy authored and Nikolay Bystritskiy committed Dec 12, 2021
1 parent 171effa commit 89e695c
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 119 deletions.
6 changes: 5 additions & 1 deletion app/acme/dns_challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ var (
acmeV2Enpoint = "https://acme-v02.api.letsencrypt.org/directory"
)

// DNSChallenge represents an ACME DNS challenge
type DNSChallenge struct {
client *acme.Client
accountKey *rsa.PrivateKey
provider dnsprovider.Provider
}

// ScheduleCertificateRenewal schedules certificate renewal
func ScheduleCertificateRenewal(domains []string, provider string) error {
log.Printf("Scheduling certificate renewal for %v", domains)

Expand Down Expand Up @@ -114,7 +116,9 @@ func (d *DNSChallenge) acceptOrder(order *acme.Order) error {

defer func() {
for _, record := range addedRecords {
d.provider.RemoveRecord(record)
if err := d.provider.RemoveRecord(record); err != nil {
log.Printf("cleanup failed to remove TXT record: %v", err)
}
}
}()

Expand Down
3 changes: 2 additions & 1 deletion app/acme/dns_challenge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package acme

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,7 +16,7 @@ func TestMain(m *testing.M) {

// use staging environment for testing
acmeV2Enpoint = "https://acme-staging-v02.api.letsencrypt.org/directory"
m.Run()
os.Exit(m.Run())
}

func TestDNSChallenge_register(t *testing.T) {
Expand Down
142 changes: 70 additions & 72 deletions app/acme/dnsprovider/cloudns.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package dnsprovider

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
)

Expand All @@ -17,10 +14,10 @@ const (
envAuthPassword = envPrefix + "AUTH_PASSWORD"
)

const (
getZoneInfoEndpoint = "https://api.cloudns.net/dns/get-zone-info.json"
addRecordEnpoint = "https://api.cloudns.net/dns/add-record.json"
)
// const (
// // getZoneInfoEndpoint = "https://api.cloudns.net/dns/get-zone-info.json"
// //addRecordEnpoint = "https://api.cloudns.net/dns/add-record.json"
// )

type cloudnsProvider struct {
authID string
Expand All @@ -30,6 +27,7 @@ type cloudnsProvider struct {
client *http.Client
}

// NewCloudnsProvider creates a new CloudnsProvider DNS provider
func NewCloudnsProvider() (Provider, error) {
authID := os.Getenv(envAuthID)
if authID == "" {
Expand Down Expand Up @@ -64,68 +62,68 @@ func (p cloudnsProvider) RemoveRecord(record string) error {
return nil
}

func (p cloudnsProvider) doRequest(method string, endpoint string, params map[string]string) (json.RawMessage, error) {
reqURL, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
q := reqURL.Query()

for k, v := range params {
q.Set(k, v)
}

// these should be set for all requests
q.Set("sub-auth-id", p.subAuthID)
q.Set("auth-id", p.authID)

reqURL.RawQuery = q.Encode()

req, err := http.NewRequest(method, reqURL.String(), nil)
if err != nil {
return nil, err
}
resp, err := p.client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid status code %v", resp.Status)
}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

return body, nil
}

func (p cloudnsProvider) getZone(fqdn string) (string, error) {
// authZone, err := findZone(fqdn)
// if err != nil {
// return "", err
// }
authZone := fqdn

authZoneName := removeTrailingDot(authZone)
body, err := p.doRequest("GET", getZoneInfoEndpoint, map[string]string{"domain-name": authZoneName})
if err != nil {
return "", err
}

zone := struct {
Name string `json:"name"`
Type string `json:"type"`
Zone string `json:"zone"`
Status int `json:"status"`
}{}

if err = json.Unmarshal(body, &zone); err != nil {
return "", err
}

return zone.Name, nil
}
// func (p cloudnsProvider) doRequest(method, endpoint string, params map[string]string) (json.RawMessage, error) {
// reqURL, err := url.Parse(endpoint)
// if err != nil {
// return nil, err
// }
// q := reqURL.Query()

// for k, v := range params {
// q.Set(k, v)
// }

// // these should be set for all requests
// q.Set("sub-auth-id", p.subAuthID)
// q.Set("auth-id", p.authID)

// reqURL.RawQuery = q.Encode()

// req, err := http.NewRequest(method, reqURL.String(), http.NoBody)
// if err != nil {
// return nil, err
// }
// resp, err := p.client.Do(req)
// if err != nil {
// return nil, err
// }
// if resp.StatusCode != http.StatusOK {
// return nil, fmt.Errorf("invalid status code %v", resp.Status)
// }

// defer resp.Body.Close()

// body, err := io.ReadAll(resp.Body)
// if err != nil {
// return nil, err
// }

// return body, nil
// }

// func (p cloudnsProvider) getZone(fqdn string) (string, error) {
// // authZone, err := findZone(fqdn)
// // if err != nil {
// // return "", err
// // }
// authZone := fqdn

// authZoneName := removeTrailingDot(authZone)
// body, err := p.doRequest("GET", getZoneInfoEndpoint, map[string]string{"domain-name": authZoneName})
// if err != nil {
// return "", err
// }

// zone := struct {
// Name string `json:"name"`
// Type string `json:"type"`
// Zone string `json:"zone"`
// Status int `json:"status"`
// }{}

// if err := json.Unmarshal(body, &zone); err != nil {
// return "", err
// }

// return zone.Name, nil
// }
47 changes: 20 additions & 27 deletions app/acme/dnsprovider/common.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package dnsprovider

import (
"crypto/sha256"
"encoding/base64"
"fmt"
"time"
)

// probably we should add possibility to specify defautl nameserver resolver
// const defaultResolvConf = "/etc/resolv.conf"

// dnsTimeout is used to override the default DNS timeout of 10 seconds.
var dnsTimeout = 10 * time.Second
// var dnsTimeout = 10 * time.Second

var nameServers = []string{
"google-public-dns-a.google.com:53",
"google-public-dns-b.google.com:53",
}
// var nameServers = []string{
// "google-public-dns-a.google.com:53",
// "google-public-dns-b.google.com:53",
// }

// func findZone(fqdn string) (string, error) {
// var err error
Expand Down Expand Up @@ -116,18 +109,18 @@ var nameServers = []string{
// }

// remove trailing dot from a fqdn if any
func removeTrailingDot(name string) string {
n := len(name)
if n != 0 && name[n-1] == '.' {
return name[:n-1]
}
return name
}

func getTXTRecordAndValue(fqdn string, keyAuth string) (record string, value string) {
shaBytes := sha256.Sum256([]byte(keyAuth))
value = base64.RawStdEncoding.EncodeToString(shaBytes[:sha256.Size])
record = fmt.Sprintf("_acme-challenge.%s", fqdn)

return record, value
}
// func removeTrailingDot(name string) string {
// n := len(name)
// if n != 0 && name[n-1] == '.' {
// return name[:n-1]
// }
// return name
// }

// func getTXTRecordAndValue(fqdn string, keyAuth string) (record string, value string) {
// shaBytes := sha256.Sum256([]byte(keyAuth))
// value = base64.RawStdEncoding.EncodeToString(shaBytes[:sha256.Size])
// record = fmt.Sprintf("_acme-challenge.%s", fqdn)

// return record, value
// }
8 changes: 0 additions & 8 deletions app/acme/dnsprovider/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
package dnsprovider

import "testing"

func TestProvider(t *testing.T) {
//p := NewProvider("cloudns")
//p.AddRecord(map[string]string{"asd": "asd"})
// p.RemoveRecord(map[string]string{"asd": "asd"})
}
9 changes: 0 additions & 9 deletions app/acme/dnsprovider/record.go

This file was deleted.

4 changes: 3 additions & 1 deletion app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ func run() error {
}

if opts.SSL.DNSChallengeEnabled {
acme.ScheduleCertificateRenewal(sslConfig.FQDNs, opts.SSL.DNSProvider)
if err = acme.ScheduleCertificateRenewal(sslConfig.FQDNs, opts.SSL.DNSProvider); err != nil {
log.Printf("[WARN] ACME: failed to schedule certificate renewal: %v", err)
}
}

accessLog, alErr := makeAccessLogWriter()
Expand Down

0 comments on commit 89e695c

Please sign in to comment.