Skip to content

Commit 028f0f0

Browse files
authored
Merge pull request #7 from pkg6/feat/certificates
Feat/certificates
2 parents 2ea55a3 + 2f3fcdc commit 028f0f0

12 files changed

+1157
-86
lines changed

.cli/elego/main.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"github.com/pkg6/elego"
77
"github.com/pkg6/elego/log"
8-
"golang.org/x/sync/errgroup"
98
"os"
109
"path"
1110
"runtime"
@@ -30,6 +29,7 @@ var (
3029
homePath string
3130
cachePath string
3231
defaultEmail string
32+
err error
3333
)
3434

3535
func init() {
@@ -58,23 +58,26 @@ func main() {
5858
}
5959
log.Printf("Accepted data : %#v", args)
6060

61-
saveDomainPrefix := args.Domain
62-
if strings.HasPrefix(saveDomainPrefix, "*") {
61+
if strings.HasPrefix(args.Domain, "*") {
6362
//https://github.com/go-acme/lego/issues/1867
6463
_ = os.Setenv("LEGO_DISABLE_CNAME_SUPPORT", strconv.FormatBool(true))
65-
saveDomainPrefix = "_" + saveDomainPrefix[1:]
6664
}
67-
keyPath := path.Join(args.Path, fmt.Sprintf("%s.key", saveDomainPrefix))
68-
cerPath := path.Join(args.Path, fmt.Sprintf("%s.cer", saveDomainPrefix))
69-
valid, daysLeft := elego.CheckCertExpiry(cerPath)
70-
if valid && args.Force == false {
71-
log.Printf("%s will expire in %.0f days", cerPath, daysLeft)
65+
66+
certificatesStorage, err := elego.NewCertificatesStorage(args.CachePath, "RC2")
67+
if err != nil {
68+
log.Fatal(err)
69+
}
70+
day, err := certificatesStorage.CheckExpire(args.Domain)
71+
if day > 0 && args.Force == false {
72+
log.Printf("%s will expire in %.0f days", args.Domain, day)
7273
return
7374
}
75+
7476
accountStorage, err := elego.NewAccountsStorage(args.CachePath, args.Email, elego.CADirURL)
7577
if err != nil {
7678
log.Fatal(err)
7779
}
80+
7881
var register elego.IRegister
7982
kid := os.Getenv("ELEGO_REGISTER_KID")
8083
hmacEncoded := os.Getenv("ELEGO_REGISTER_HMAC")
@@ -105,16 +108,11 @@ func main() {
105108
if err != nil {
106109
log.Fatal(err)
107110
}
108-
var (
109-
wg errgroup.Group
110-
)
111-
wg.Go(func() error {
112-
return elego.CopyFile(certificate.Certificate, cerPath)
113-
})
114-
wg.Go(func() error {
115-
return elego.CopyFile(certificate.PrivateKey, keyPath)
116-
})
117-
if err := wg.Wait(); err != nil {
111+
if err := certificatesStorage.SaveResource(certificate); err != nil {
112+
log.Fatal(err)
113+
}
114+
cerPath, keyPath, err := certificatesStorage.SaveNginx(args.Path, args.Domain)
115+
if err != nil {
118116
log.Fatal(err)
119117
}
120118
log.Printf("save key: %s", keyPath)

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ go.work.sum
2424
# env file
2525
.env
2626

27-
/.idea
27+
/.idea
28+
coverage.txt

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
TAG_VERSION := $(shell git describe --tags --always)
2+
3+
check:
4+
go install github.com/golangci/golangci-lint/cmd/[email protected]
5+
golangci-lint run ./...
6+
7+
test:
8+
go test -v -coverpkg=./... -race -covermode=atomic -coverprofile=coverage.txt ./... -run . -timeout=2m
9+
10+
build:
11+
go build -ldflags='-s -w -X main.TAG_version=${TAG_VERSION}' -o elego ./.cli/elego/main.go
12+

accounts_storage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ type AccountsStorage struct {
3232
}
3333

3434
// NewAccountsStorage Creates a new AccountsStorage.
35-
func NewAccountsStorage(accountSavePath, email, CADirURL string) (*AccountsStorage, error) {
35+
func NewAccountsStorage(savePath, email, CADirURL string) (*AccountsStorage, error) {
3636
serverURL, err := url.Parse(CADirURL)
3737
if err != nil {
3838
return nil, err
3939
}
4040
serverPath := strings.NewReplacer(":", "_", "/", string(os.PathSeparator)).Replace(serverURL.Host)
41-
accountsPath := filepath.Join(accountSavePath, serverPath)
41+
accountsPath := filepath.Join(savePath, serverPath)
4242
rootUserPath := filepath.Join(accountsPath, email)
4343
if err := CreateNonExistingFolder(rootUserPath); err != nil {
4444
return nil, err

accounts_storage_test.go

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package elego
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestAccountsStorage_GetEmail(t *testing.T) {
9+
type fields struct {
10+
Email string
11+
CADirURL string
12+
accountFilePath string
13+
keysPath string
14+
}
15+
tests := []struct {
16+
name string
17+
fields fields
18+
want string
19+
}{
20+
// TODO: Add test cases.
21+
}
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
s := &AccountsStorage{
25+
Email: tt.fields.Email,
26+
CADirURL: tt.fields.CADirURL,
27+
accountFilePath: tt.fields.accountFilePath,
28+
keysPath: tt.fields.keysPath,
29+
}
30+
if got := s.GetEmail(); got != tt.want {
31+
t.Errorf("GetEmail() = %v, want %v", got, tt.want)
32+
}
33+
})
34+
}
35+
}
36+
37+
func TestAccountsStorage_LoadAccount(t *testing.T) {
38+
type fields struct {
39+
Email string
40+
CADirURL string
41+
accountFilePath string
42+
keysPath string
43+
}
44+
tests := []struct {
45+
name string
46+
fields fields
47+
want *Account
48+
wantErr bool
49+
}{
50+
// TODO: Add test cases.
51+
}
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
s := &AccountsStorage{
55+
Email: tt.fields.Email,
56+
CADirURL: tt.fields.CADirURL,
57+
accountFilePath: tt.fields.accountFilePath,
58+
keysPath: tt.fields.keysPath,
59+
}
60+
got, err := s.LoadAccount()
61+
if (err != nil) != tt.wantErr {
62+
t.Errorf("LoadAccount() error = %v, wantErr %v", err, tt.wantErr)
63+
return
64+
}
65+
if !reflect.DeepEqual(got, tt.want) {
66+
t.Errorf("LoadAccount() got = %v, want %v", got, tt.want)
67+
}
68+
})
69+
}
70+
}
71+
72+
func TestAccountsStorage_Remove(t *testing.T) {
73+
type fields struct {
74+
Email string
75+
CADirURL string
76+
accountFilePath string
77+
keysPath string
78+
}
79+
tests := []struct {
80+
name string
81+
fields fields
82+
}{
83+
// TODO: Add test cases.
84+
}
85+
for _, tt := range tests {
86+
t.Run(tt.name, func(t *testing.T) {
87+
s := &AccountsStorage{
88+
Email: tt.fields.Email,
89+
CADirURL: tt.fields.CADirURL,
90+
accountFilePath: tt.fields.accountFilePath,
91+
keysPath: tt.fields.keysPath,
92+
}
93+
s.Remove()
94+
})
95+
}
96+
}
97+
98+
func TestAccountsStorage_Save(t *testing.T) {
99+
type fields struct {
100+
Email string
101+
CADirURL string
102+
accountFilePath string
103+
keysPath string
104+
}
105+
type args struct {
106+
account *Account
107+
}
108+
tests := []struct {
109+
name string
110+
fields fields
111+
args args
112+
wantErr bool
113+
}{
114+
// TODO: Add test cases.
115+
}
116+
for _, tt := range tests {
117+
t.Run(tt.name, func(t *testing.T) {
118+
s := &AccountsStorage{
119+
Email: tt.fields.Email,
120+
CADirURL: tt.fields.CADirURL,
121+
accountFilePath: tt.fields.accountFilePath,
122+
keysPath: tt.fields.keysPath,
123+
}
124+
if err := s.Save(tt.args.account); (err != nil) != tt.wantErr {
125+
t.Errorf("Save() error = %v, wantErr %v", err, tt.wantErr)
126+
}
127+
})
128+
}
129+
}
130+
131+
func TestNewAccountsStorage(t *testing.T) {
132+
type args struct {
133+
savePath string
134+
email string
135+
CADirURL string
136+
}
137+
tests := []struct {
138+
name string
139+
args args
140+
want *AccountsStorage
141+
wantErr bool
142+
}{
143+
// TODO: Add test cases.
144+
}
145+
for _, tt := range tests {
146+
t.Run(tt.name, func(t *testing.T) {
147+
got, err := NewAccountsStorage(tt.args.savePath, tt.args.email, tt.args.CADirURL)
148+
if (err != nil) != tt.wantErr {
149+
t.Errorf("NewAccountsStorage() error = %v, wantErr %v", err, tt.wantErr)
150+
return
151+
}
152+
if !reflect.DeepEqual(got, tt.want) {
153+
t.Errorf("NewAccountsStorage() got = %v, want %v", got, tt.want)
154+
}
155+
})
156+
}
157+
}

cert.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)