-
Notifications
You must be signed in to change notification settings - Fork 6
/
provider.go
136 lines (109 loc) · 2.92 KB
/
provider.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package client
import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"
pgpcrypto "github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/adrg/xdg"
"github.com/siderolabs/go-api-signature/pkg/pgp"
)
const (
keyLifetime = 4 * time.Hour
)
// KeyProvider handles loading/saving client keys.
type KeyProvider struct {
dataFileDirectory string
keyLifetime time.Duration
}
// NewKeyProvider creates a new KeyProvider.
func NewKeyProvider(dataFileDirectory string) *KeyProvider {
return &KeyProvider{
dataFileDirectory: dataFileDirectory,
keyLifetime: keyLifetime,
}
}
// ReadValidKey reads a PGP key from the filesystem.
//
// If the key is missing or invalid (e.g. expired, revoked), an error will be returned.
func (provider *KeyProvider) ReadValidKey(context, email string) (*Key, error) {
keyPath, err := provider.getKeyFilePath(context, email)
if err != nil {
return nil, err
}
keyF, err := os.Open(keyPath)
if err != nil {
return nil, err
}
defer keyF.Close() //nolint:errcheck
key, err := pgpcrypto.NewKeyFromArmoredReader(keyF)
if err != nil {
return nil, err
}
pgpKey, err := pgp.NewKey(key)
if err != nil {
return nil, err
}
err = pgpKey.Validate()
if err != nil {
return nil, err
}
unlocked, err := pgpKey.IsUnlocked()
if err != nil {
return nil, err
}
if !unlocked {
return nil, fmt.Errorf("private key is locked")
}
return &Key{
Key: pgpKey,
context: context,
identity: email,
}, nil
}
// GenerateKey generates a new PGP key pair.
func (provider *KeyProvider) GenerateKey(context, email, clientNameWithVersion string) (*Key, error) {
name := clientNameWithVersion
comment := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
key, err := pgp.GenerateKey(name, comment, email, provider.keyLifetime)
if err != nil {
return nil, err
}
return &Key{
Key: key,
context: context,
identity: email,
}, nil
}
// DeleteKey deletes the key pair from disk.
func (provider *KeyProvider) DeleteKey(context, email string) error {
keyPath, err := provider.getKeyFilePath(context, email)
if err != nil {
return err
}
return os.Remove(keyPath)
}
// WriteKey saves the key pair to disk and returns the save path.
func (provider *KeyProvider) WriteKey(c *Key) (string, error) {
armored, err := c.Armor()
if err != nil {
return "", err
}
keyPath, err := provider.getKeyFilePath(c.context, c.identity)
if err != nil {
return "", err
}
err = os.WriteFile(keyPath, []byte(armored), 0o600)
if err != nil {
return "", err
}
return keyPath, err
}
func (provider *KeyProvider) getKeyFilePath(context, identity string) (string, error) {
keyName := fmt.Sprintf("%s-%s.pgp", context, identity)
return xdg.DataFile(filepath.Join(provider.dataFileDirectory, keyName))
}