Skip to content

Commit

Permalink
chore: update to go1.20 (#1993)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Aug 19, 2023
1 parent c365d7e commit b37d60c
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Build and deploy documentation
runs-on: ubuntu-latest
env:
GO_VERSION: '1.20'
GO_VERSION: stable
HUGO_VERSION: '0.117.0'
CGO_ENABLED: 0

Expand Down
28 changes: 5 additions & 23 deletions .github/workflows/go-cross.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,19 @@ jobs:

strategy:
matrix:
go-version: [ '1.19', '1.20', 1.x ]
go-version: [ stable, oldstable ]
os: [ubuntu-latest, macos-latest, windows-latest]

steps:
# https://github.com/marketplace/actions/setup-go-environment
- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

# https://github.com/marketplace/actions/checkout
- name: Checkout code
uses: actions/checkout@v3

# https://github.com/marketplace/actions/cache
- name: Cache Go modules
uses: actions/cache@v3
# https://github.com/marketplace/actions/setup-go-environment
- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
# In order:
# * Module download cache
# * Build cache (Linux)
# * Build cache (Mac)
# * Build cache (Windows)
path: |
~/go/pkg/mod
~/.cache/go-build
~/Library/Caches/go-build
%LocalAppData%\go-build
key: ${{ runner.os }}-${{ matrix.go-version }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-${{ matrix.go-version }}-go-
go-version: ${{ matrix.go-version }}

- name: Test
run: go test -v -cover ./...
Expand Down
21 changes: 6 additions & 15 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,26 @@ jobs:
name: Main Process
runs-on: ubuntu-latest
env:
GO_VERSION: '1.20'
GOLANGCI_LINT_VERSION: v1.53.1
GO_VERSION: stable
GOLANGCI_LINT_VERSION: v1.54.1
HUGO_VERSION: '0.117.0'
CGO_ENABLED: 0
LEGO_E2E_TESTS: CI
MEMCACHED_HOSTS: localhost:11211

steps:

# https://github.com/marketplace/actions/setup-go-environment
- name: Set up Go ${{ env.GO_VERSION }}
uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}

# https://github.com/marketplace/actions/checkout
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: 0

# https://github.com/marketplace/actions/cache
- name: Cache Go modules
uses: actions/cache@v3
# https://github.com/marketplace/actions/setup-go-environment
- name: Set up Go ${{ env.GO_VERSION }}
uses: actions/setup-go@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
go-version: ${{ env.GO_VERSION }}

- name: Check and get dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Release version
runs-on: ubuntu-latest
env:
GO_VERSION: '1.20'
GO_VERSION: stable
CGO_ENABLED: 0

steps:
Expand Down
12 changes: 4 additions & 8 deletions certificate/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ func (c *Certifier) getAuthorizations(order acme.ExtendedOrder) ([]acme.Authoriz
}

var responses []acme.Authorization
failures := make(obtainError)

failures := newObtainError()
for i := 0; i < len(order.Authorizations); i++ {
select {
case res := <-resc:
responses = append(responses, res)
case err := <-errc:
failures[err.Domain] = err.Error
failures.Add(err.Domain, err.Error)
}
}

Expand All @@ -52,12 +53,7 @@ func (c *Certifier) getAuthorizations(order acme.ExtendedOrder) ([]acme.Authoriz
close(resc)
close(errc)

// be careful to not return an empty failures map;
// even if empty, they become non-nil error values
if len(failures) > 0 {
return responses, failures
}
return responses, nil
return responses, failures.Join()
}

func (c *Certifier) deactivateAuthorizations(order acme.ExtendedOrder, force bool) {
Expand Down
22 changes: 6 additions & 16 deletions certificate/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,19 @@ func (c *Certifier) Obtain(request ObtainRequest) (*Resource, error) {

log.Infof("[%s] acme: Validations succeeded; requesting certificates", strings.Join(domains, ", "))

failures := make(obtainError)
failures := newObtainError()
cert, err := c.getForOrder(domains, order, request.Bundle, request.PrivateKey, request.MustStaple, request.PreferredChain)
if err != nil {
for _, auth := range authz {
failures[challenge.GetTargetedDomain(auth)] = err
failures.Add(challenge.GetTargetedDomain(auth), err)
}
}

if request.AlwaysDeactivateAuthorizations {
c.deactivateAuthorizations(order, true)
}

// Do not return an empty failures map, because
// it would still be a non-nil error value
if len(failures) > 0 {
return cert, failures
}
return cert, nil
return cert, failures.Join()
}

// ObtainForCSR tries to obtain a certificate matching the CSR passed into it.
Expand Down Expand Up @@ -219,11 +214,11 @@ func (c *Certifier) ObtainForCSR(request ObtainForCSRRequest) (*Resource, error)

log.Infof("[%s] acme: Validations succeeded; requesting certificates", strings.Join(domains, ", "))

failures := make(obtainError)
failures := newObtainError()
cert, err := c.getForCSR(domains, order, request.Bundle, request.CSR.Raw, nil, request.PreferredChain)
if err != nil {
for _, auth := range authz {
failures[challenge.GetTargetedDomain(auth)] = err
failures.Add(challenge.GetTargetedDomain(auth), err)
}
}

Expand All @@ -236,12 +231,7 @@ func (c *Certifier) ObtainForCSR(request ObtainForCSRRequest) (*Resource, error)
cert.CSR = certcrypto.PEMEncode(request.CSR)
}

// Do not return an empty failures map,
// because it would still be a non-nil error value
if len(failures) > 0 {
return cert, failures
}
return cert, nil
return cert, failures.Join()
}

func (c *Certifier) getForOrder(domains []string, order acme.ExtendedOrder, bundle bool, privateKey crypto.PrivateKey, mustStaple bool, preferredChain string) (*Resource, error) {
Expand Down
36 changes: 23 additions & 13 deletions certificate/errors.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
package certificate

import (
"bytes"
"errors"
"fmt"
"sort"
)

// obtainError is returned when there are specific errors available per domain.
type obtainError map[string]error
type obtainError struct {
data map[string]error
}

func newObtainError() *obtainError {
return &obtainError{data: make(map[string]error)}
}

func (e obtainError) Error() string {
buffer := bytes.NewBufferString("error: one or more domains had a problem:\n")
func (e *obtainError) Add(domain string, err error) {
e.data[domain] = err
}

var domains []string
for domain := range e {
domains = append(domains, domain)
func (e *obtainError) Join() error {
if e == nil {
return nil
}
sort.Strings(domains)

for _, domain := range domains {
_, _ = fmt.Fprintf(buffer, "[%s] %s\n", domain, e[domain])
if len(e.data) == 0 {
return nil
}
return buffer.String()

var err error
for d, e := range e.data {
err = errors.Join(err, fmt.Errorf("%s: %w", d, e))
}

return fmt.Errorf("error: one or more domains had a problem:\n%w", err)
}

type domainError struct {
Expand Down
70 changes: 70 additions & 0 deletions certificate/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package certificate

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type TomatoError struct{}

func (t TomatoError) Error() string {
return "tomato"
}

type CarrotError struct{}

func (t CarrotError) Error() string {
return "carrot"
}

func Test_obtainError_Join(t *testing.T) {
failures := newObtainError()

failures.Add("example.com", &TomatoError{})

err := failures.Join()

to := &TomatoError{}
assert.ErrorAs(t, err, &to)
}

func Test_obtainError_Join_multiple_domains(t *testing.T) {
failures := newObtainError()

failures.Add("example.com", &TomatoError{})
failures.Add("example.org", &CarrotError{})

err := failures.Join()

to := &TomatoError{}
assert.ErrorAs(t, err, &to)

ca := &CarrotError{}
assert.ErrorAs(t, err, &ca)
}

func Test_obtainError_Join_no_error(t *testing.T) {
failures := newObtainError()

assert.NoError(t, failures.Join())
}

func Test_obtainError_Join_same_domain(t *testing.T) {
failures := newObtainError()

failures.Add("example.com", &TomatoError{})
failures.Add("example.com", &CarrotError{})

err := failures.Join()

to := &TomatoError{}
if errors.As(err, &to) {
require.Fail(t, "TomatoError should be overridden by CarrotError")
}

ca := &CarrotError{}
assert.ErrorAs(t, err, &ca)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-acme/lego/v4

go 1.19
go 1.20

require (
cloud.google.com/go/compute/metadata v0.2.3
Expand Down
2 changes: 1 addition & 1 deletion providers/dns/allinkl/internal/types_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type DNSRequest struct {
// ---

type GetDNSSettingsAPIResponse struct {
Response GetDNSSettingsResponse `json:"Response" mapstructure:"Response"`
Response GetDNSSettingsResponse `json:"Response" mapstructure:"Response"`
}

type GetDNSSettingsResponse struct {
Expand Down
29 changes: 20 additions & 9 deletions providers/dns/ipv64/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ import (
"time"

"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
"golang.org/x/oauth2"
)

const defaultBaseURL = "https://ipv64.net"

const authorizationHeader = "Authorization"

type Client struct {
apiKey string

baseURL *url.URL
HTTPClient *http.Client
}

func NewClient(apiKey string) *Client {
func NewClient(hc *http.Client) *Client {
baseURL, _ := url.Parse(defaultBaseURL)

if hc == nil {
hc = &http.Client{Timeout: 15 * time.Second}
}

return &Client{
apiKey: apiKey,
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 15 * time.Second},
HTTPClient: hc,
}
}

Expand Down Expand Up @@ -91,8 +91,6 @@ func (c Client) DeleteRecord(ctx context.Context, domain, prefix, recordType, co
}

func (c Client) do(req *http.Request, result any) error {
req.Header.Set(authorizationHeader, fmt.Sprintf("Bearer %s", c.apiKey))

if req.Method != http.MethodGet {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
Expand Down Expand Up @@ -140,3 +138,16 @@ func parseError(req *http.Request, resp *http.Response) error {

return errAPI
}

func OAuthStaticAccessToken(client *http.Client, accessToken string) *http.Client {
if client == nil {
client = &http.Client{Timeout: 15 * time.Second}
}

client.Transport = &oauth2.Transport{
Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken}),
Base: client.Transport,
}

return client
}
Loading

0 comments on commit b37d60c

Please sign in to comment.