-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosscert.go
147 lines (124 loc) · 3.81 KB
/
osscert.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
135
136
137
138
139
140
141
142
143
144
145
146
147
package osscert
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"fmt"
"os"
"strings"
"time"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
"github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/challenge/http01"
"github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/registration"
)
func Run() (rst string, err error) {
var OSS_REGION = os.Getenv("OSS_REGION")
var OSS_BUCKET = os.Getenv("OSS_BUCKET")
var ACME_EMAIL = os.Getenv("ACME_EMAIL")
ossConfig := oss.LoadDefaultConfig().WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).WithRegion(OSS_REGION)
ossClient := oss.NewClient(ossConfig)
response, err := ossClient.ListCname(context.TODO(), &oss.ListCnameRequest{Bucket: &OSS_BUCKET})
if err != nil {
return
}
cname := response.Cnames[0]
domain := *cname.Domain
expiry, err := time.Parse("Jan _2 15:04:05 2006 GMT", *cname.Certificate.ValidEndDate) // "Dec 5 23:59:59 2024 GMT"
if err != nil {
return
}
days := time.Until(expiry).Hours() / 24
summary := fmt.Sprintf("Domain: %s\nExpiry: %s (%v days left)\n", domain, expiry, days)
if days > 31 {
return summary + "Certificate is OK!", nil
}
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return
}
user := User{
Email: ACME_EMAIL,
key: privateKey,
}
acmeConfig := lego.NewConfig(&user)
//acmeConfig.CADirURL = lego.LEDirectoryStaging
acmeConfig.Certificate.KeyType = certcrypto.RSA2048
acmeClient, err := lego.NewClient(acmeConfig)
if err != nil {
return
}
err = acmeClient.Challenge.SetHTTP01Provider(NewOSSFileChallengeProvider(ossClient, &OSS_BUCKET))
if err != nil {
return
}
reg, err := acmeClient.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
if err != nil {
return
}
user.Registration = reg
request := certificate.ObtainRequest{
Domains: []string{domain},
Bundle: true,
}
cert, err := acmeClient.Certificate.Obtain(request)
if err != nil {
return
}
certConfig := &oss.CertificateConfiguration{
Certificate: oss.Ptr(string(cert.Certificate)),
PrivateKey: oss.Ptr(string(cert.PrivateKey)),
Force: oss.Ptr(true),
DeleteCertificate: oss.Ptr(false),
}
_, err = ossClient.PutCname(context.TODO(), &oss.PutCnameRequest{Bucket: &OSS_BUCKET, BucketCnameConfiguration: &oss.BucketCnameConfiguration{Domain: &domain, CertificateConfiguration: certConfig}})
if err != nil {
return
}
return summary + "Certificate Updated!", nil
}
type User struct {
Email string
Registration *registration.Resource
key crypto.PrivateKey
}
func (u *User) GetEmail() string {
return u.Email
}
func (u User) GetRegistration() *registration.Resource {
return u.Registration
}
func (u *User) GetPrivateKey() crypto.PrivateKey {
return u.key
}
type OSSFileChallengeProvider struct {
Client *oss.Client
Bucket *string
}
func NewOSSFileChallengeProvider(client *oss.Client, bucket *string) *OSSFileChallengeProvider {
return &OSSFileChallengeProvider{Client: client, Bucket: bucket}
}
func buildChallengeFilePath(token string) *string {
return oss.Ptr(strings.TrimLeft(http01.ChallengePath(token), "/"))
}
func (p *OSSFileChallengeProvider) Present(domain, token, keyAuth string) error {
_, err := p.Client.PutObject(context.TODO(), &oss.PutObjectRequest{
Bucket: p.Bucket,
Key: buildChallengeFilePath(token),
Body: strings.NewReader(keyAuth),
Acl: oss.ObjectACLPublicRead,
})
return err
}
func (p *OSSFileChallengeProvider) CleanUp(domain, token, keyAuth string) error {
_, err := p.Client.DeleteObject(context.TODO(), &oss.DeleteObjectRequest{
Bucket: p.Bucket,
Key: buildChallengeFilePath(token),
})
return err
}