-
-
Notifications
You must be signed in to change notification settings - Fork 895
Expand file tree
/
Copy pathclient_obtain.go
More file actions
218 lines (189 loc) · 6.28 KB
/
Copy pathclient_obtain.go
File metadata and controls
218 lines (189 loc) · 6.28 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package certacme
import (
"context"
"crypto"
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/go-acme/lego/v5/acme"
"github.com/go-acme/lego/v5/certcrypto"
"github.com/go-acme/lego/v5/certificate"
"github.com/go-acme/lego/v5/challenge/dns01"
"github.com/go-acme/lego/v5/challenge/http01"
"github.com/go-acme/lego/v5/log"
"github.com/samber/lo"
"github.com/certimate-go/certimate/internal/certacme/certifiers"
"github.com/certimate-go/certimate/internal/domain"
xcert "github.com/certimate-go/certimate/pkg/utils/cert"
)
type ObtainCertificateRequest struct {
DomainOrIPs []string
PrivateKeyType certcrypto.KeyType
PrivateKeyPEM string
ValidityNotBefore time.Time
ValidityNotAfter time.Time
NoCommonName bool
// 提供商相关
_ struct{}
ChallengeType string
Provider domain.ACMEChallengeProviderType
ProviderAccessConfig map[string]any
ProviderExtendedConfig map[string]any
// 解析相关
_ struct{}
DisableFollowCNAME bool
Nameservers []string
// DNS-01 质询相关
_ struct{}
DnsPropagationWait int
DnsPropagationTimeout int
DnsTTL int
// HTTP-01 质询相关
_ struct{}
HttpDelayWait int
// ACME 相关
_ struct{}
PreferredChain string
ACMEProfile string
// ARI 相关
_ struct{}
DisableARI bool
ARIReplacesAccountUrl string
ARIReplacesCertId string
}
type ObtainCertificateResponse struct {
CAProvider domain.CAProviderType
CSR string
FullChainCertificate string
IssuerCertificate string
PrivateKey string
ACMEAccountUrl string
ACMECertificateUrl string
ARIInfo *ARIInfo
ARIError string
ARIReplaced bool
}
func (c *ACMEClient) ObtainCertificate(ctx context.Context, request *ObtainCertificateRequest) (*ObtainCertificateResponse, error) {
if request == nil {
return nil, fmt.Errorf("the request is nil")
}
os.Setenv("LEGO_DISABLE_CNAME_SUPPORT", strconv.FormatBool(request.DisableFollowCNAME))
const CHALLENGE_TYPE_DNS01 = "dns-01"
const CHALLENGE_TYPE_HTTP01 = "http-01"
switch strings.ToLower(request.ChallengeType) {
case CHALLENGE_TYPE_DNS01:
{
providerFactory, err := certifiers.ACMEDns01Registries.Get(domain.ACMEDns01ProviderType(request.Provider))
if err != nil {
return nil, err
}
provider, err := providerFactory(&certifiers.ProviderFactoryOptions{
ProviderAccessConfig: request.ProviderAccessConfig,
ProviderExtendedConfig: request.ProviderExtendedConfig,
DnsPropagationTimeout: request.DnsPropagationTimeout,
DnsTTL: request.DnsTTL,
})
if err != nil {
return nil, fmt.Errorf("failed to initialize dns-01 provider '%s': %w", request.Provider, err)
}
opts := &dns01.Options{}
opts.RecursiveNameservers = request.Nameservers
dns01.SetDefaultClient(dns01.NewClient(opts))
c.client.Challenge.SetDNS01Provider(
provider,
dns01.CondOptions(
request.DnsPropagationWait > 0,
dns01.PropagationWait(time.Duration(request.DnsPropagationWait)*time.Second, true),
),
dns01.CondOptions(
len(request.Nameservers) > 0 || request.DnsPropagationWait > 0,
dns01.DisableAuthoritativeNssPropagationRequirement(),
),
)
}
case CHALLENGE_TYPE_HTTP01:
{
providerFactory, err := certifiers.ACMEHttp01Registries.Get(domain.ACMEHttp01ProviderType(request.Provider))
if err != nil {
return nil, err
}
provider, err := providerFactory(&certifiers.ProviderFactoryOptions{
ProviderAccessConfig: request.ProviderAccessConfig,
ProviderExtendedConfig: request.ProviderExtendedConfig,
})
if err != nil {
return nil, fmt.Errorf("failed to initialize http-01 provider '%s': %w", request.Provider, err)
}
c.client.Challenge.SetHTTP01Provider(
provider,
http01.SetDelay(time.Duration(request.HttpDelayWait)*time.Second),
)
}
default:
return nil, fmt.Errorf("unsupported challenge type: '%s'", request.ChallengeType)
}
var privkey crypto.Signer
if request.PrivateKeyPEM != "" {
pk, err := certcrypto.ParsePEMPrivateKey([]byte(request.PrivateKeyPEM))
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %w", err)
}
privkey = pk
}
req := certificate.ObtainRequest{
Domains: request.DomainOrIPs,
KeyType: request.PrivateKeyType,
PrivateKey: privkey,
Bundle: true,
EnableCommonName: !request.NoCommonName,
PreferredChain: request.PreferredChain,
Profile: request.ACMEProfile,
NotBefore: request.ValidityNotBefore,
NotAfter: request.ValidityNotAfter,
ReplacesCertID: lo.If(request.ARIReplacesAccountUrl == c.account.ACMEAccountUrl, request.ARIReplacesCertId).Else(""),
}
resp, err := c.client.Certificate.Obtain(ctx, req)
if err != nil {
ariErr := &acme.AlreadyReplacedError{}
if !errors.As(err, &ariErr) {
return nil, err
}
log.Warn("the certificate has already been replaced, try to obtain again without ARI ...")
// reset ARI and retry if failure
req.ReplacesCertID = ""
resp, err = c.client.Certificate.Obtain(ctx, req)
if err != nil {
return nil, err
}
}
// lego 自 v5 起返回的私钥 PEM 内容使用 PKCS#8 格式编码,
// 这里转换为 PKCS#1 或 SEC1 格式编码,以满足更好的兼容性。
privkeyPEM := strings.TrimSpace(string(resp.PrivateKey))
if t1, err := xcert.ParsePrivateKeyFromPEM(privkeyPEM); err == nil {
if t2, err := xcert.ConvertPrivateKeyToPEM(t1, false); err == nil {
privkeyPEM = t2
}
}
obtainResp := &ObtainCertificateResponse{
CAProvider: domain.CAProviderType(c.account.CA),
CSR: strings.TrimSpace(string(resp.CSR)),
FullChainCertificate: strings.TrimSpace(string(resp.Certificate)),
IssuerCertificate: strings.TrimSpace(string(resp.IssuerCertificate)),
PrivateKey: privkeyPEM,
ACMEAccountUrl: c.account.ACMEAccountUrl,
ACMECertificateUrl: resp.CertURL,
ARIReplaced: req.ReplacesCertID != "",
}
if !request.DisableARI {
ariInfo, err := c.GetARIInfo(ctx, obtainResp.FullChainCertificate)
if err != nil {
obtainResp.ARIError = err.Error()
} else {
obtainResp.ARIInfo = ariInfo
}
}
return obtainResp, nil
}