/
client.go
134 lines (106 loc) · 3.26 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package client
import (
"context"
"crypto/rand"
"crypto/rsa"
"errors"
"fmt"
"strings"
"sync"
"github.com/golang/glog"
"golang.org/x/crypto/acme"
"github.com/tnozicka/openshift-acme/pkg/acme/challengeexposers"
)
var once sync.Once
func acceptTerms(tosURL string) bool {
once.Do(func() {
glog.Infof("By continuing running this program you agree to the CA's Terms of Service (%s). If you do not agree exit the program immediately!", tosURL)
})
return true
}
type Client struct {
Client *acme.Client
Account *acme.Account
}
func (c *Client) CreateAccount(ctx context.Context, a *acme.Account) error {
var err error
c.Client.Key, err = rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return err
}
c.Account, err = c.Client.Register(ctx, a, acceptTerms)
if err != nil {
return err
}
return nil
}
func (c *Client) DeactivateAccount(ctx context.Context, a *acme.Account) error {
return c.Client.RevokeAuthorization(ctx, a.URI)
}
func getStatisfiableCombinations(authorization *acme.Authorization, exposers map[string]challengeexposers.Interface) [][]int {
var combinations [][]int
for _, combination := range authorization.Combinations {
satisfiable := true
for _, challengeId := range combination {
if challengeId >= len(authorization.Challenges) {
glog.Warningf("ACME authorization has contains challengeId %d out of range; %#v", challengeId, authorization)
satisfiable = false
continue
}
if _, ok := exposers[authorization.Challenges[challengeId].Type]; !ok {
satisfiable = false
continue
}
}
if satisfiable {
combinations = append(combinations, combination)
}
}
return combinations
}
func (c *Client) AcceptAuthorization(
ctx context.Context,
authorization *acme.Authorization,
domain string,
exposers map[string]challengeexposers.Interface,
) (*acme.Authorization, error) {
glog.V(4).Infof("Found %d possible combinations for authorization", len(authorization.Combinations))
combinations := getStatisfiableCombinations(authorization, exposers)
if len(combinations) == 0 {
return nil, fmt.Errorf("none of %d combination could be satified", len(authorization.Combinations))
}
glog.V(4).Infof("Found %d valid combinations for authorization", len(combinations))
// TODO: sort combinations by preference
// TODO: consider using the remaining combinations if this one fails
combination := combinations[0]
for _, challengeId := range combination {
challenge := authorization.Challenges[challengeId]
exposer, ok := exposers[challenge.Type]
if !ok {
return nil, errors.New("internal error: unavailable exposer")
}
err := exposer.Expose(c.Client, domain, challenge.Token)
if err != nil {
return nil, err
}
challenge, err = c.Client.Accept(ctx, challenge)
if err != nil {
return nil, err
}
}
authorization, err := c.Client.GetAuthorization(ctx, authorization.URI)
if err != nil {
return nil, err
}
return authorization, nil
}
func GetAuthorizationErrors(authorization *acme.Authorization) string {
var res []string
for _, challenge := range authorization.Challenges {
if challenge.Status != "invalid" {
continue
}
res = append(res, fmt.Sprintf("%q challenge is %q: %v", challenge.Type, challenge.Status, challenge.Error))
}
return fmt.Sprintf("[%s]", strings.Join(res, ", "))
}