-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_account.go
312 lines (268 loc) · 8.38 KB
/
service_account.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package auth
import (
"context"
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
"github.com/golang-jwt/jwt/v4"
"golang.org/x/sync/singleflight"
)
type ServiceAccount struct {
PrivateKey *rsa.PrivateKey
PublicKeyID string
ServiceAccountID string
}
type ServiceAccountReader interface {
ServiceAccount(context.Context) (ServiceAccount, error)
}
// CachedServiceAccount is a [ServiceAccountReader] decorator that caches the [ServiceAccount].
type CachedServiceAccount struct {
reader ServiceAccountReader
group singleflight.Group
mu sync.RWMutex
cache *ServiceAccount
}
var _ ServiceAccountReader = (*CachedServiceAccount)(nil)
// NewCachedServiceAccount returns a decorated [ServiceAccountReader] that caches the [ServiceAccount].
func NewCachedServiceAccount(reader ServiceAccountReader) *CachedServiceAccount {
return &CachedServiceAccount{
reader: reader,
group: singleflight.Group{},
mu: sync.RWMutex{},
cache: nil,
}
}
func (c *CachedServiceAccount) ServiceAccount(ctx context.Context) (ServiceAccount, error) {
c.mu.RLock()
cache := c.cache
c.mu.RUnlock()
if cache != nil {
return *cache, nil
}
res, err, _ := c.group.Do("", func() (interface{}, error) {
account, err := c.reader.ServiceAccount(ctx)
if err != nil {
return nil, err
}
c.mu.Lock()
c.cache = &account
c.mu.Unlock()
return account, nil
})
if err != nil {
return ServiceAccount{}, err
}
return res.(ServiceAccount), nil //nolint:errcheck // ok to panic
}
// StaticServiceAccount is a [ServiceAccountReader] that always returns a fixed [ServiceAccount].
type StaticServiceAccount ServiceAccount
var _ ServiceAccountReader = StaticServiceAccount{}
// NewStaticServiceAccount returns a [ServiceAccountReader] that always returns a fixed [ServiceAccount].
func NewStaticServiceAccount(sa ServiceAccount) StaticServiceAccount {
return StaticServiceAccount(sa)
}
func (s StaticServiceAccount) ServiceAccount(context.Context) (ServiceAccount, error) {
return ServiceAccount(s), nil
}
// PrivateKeyParser is a [ServiceAccountReader] that parses a PEM-encoded PKCS1 or PKCS8 private key.
type PrivateKeyParser struct {
privateKey []byte
publicKeyID string
serviceAccountID string
}
var _ ServiceAccountReader = PrivateKeyParser{}
// NewPrivateKeyParser returns a [ServiceAccountReader] that parses a PEM-encoded PKCS1 or PKCS8 private key.
func NewPrivateKeyParser(privateKey []byte, publicKeyID string, serviceAccountID string) PrivateKeyParser {
return PrivateKeyParser{
privateKey: privateKey,
publicKeyID: publicKeyID,
serviceAccountID: serviceAccountID,
}
}
func (p PrivateKeyParser) ServiceAccount(context.Context) (ServiceAccount, error) {
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(p.privateKey)
if err != nil {
return ServiceAccount{}, err
}
return ServiceAccount{
PrivateKey: privateKey,
PublicKeyID: p.publicKeyID,
ServiceAccountID: p.serviceAccountID,
}, nil
}
// PrivateKeyFileParser is a [ServiceAccountReader] that reads and parses
// a PEM-encoded PKCS1 or PKCS8 private key from a file.
type PrivateKeyFileParser struct {
fs fs.FS
privateKeyPath string
publicKeyID string
serviceAccountID string
}
var _ ServiceAccountReader = PrivateKeyFileParser{}
// NewPrivateKeyFileParser returns a [ServiceAccountReader] that reads and parses
// a PEM-encoded PKCS1 or PKCS8 private key from a file.
//
// If fs is nil, the default file system will be used.
func NewPrivateKeyFileParser(
fs fs.FS,
privateKeyPath string,
publicKeyID string,
serviceAccountID string,
) PrivateKeyFileParser {
return PrivateKeyFileParser{
fs: fs,
privateKeyPath: privateKeyPath,
publicKeyID: publicKeyID,
serviceAccountID: serviceAccountID,
}
}
const homeShortcut = "~" + string(filepath.Separator)
func sanitizePath(path string) (string, error) {
if path == "~" {
return "", errors.New("invalid path '~'")
}
if strings.HasPrefix(path, homeShortcut) {
homePath, err := os.UserHomeDir()
if err != nil {
return "", err
}
path = filepath.Join(
homePath, strings.TrimPrefix(path, homeShortcut),
)
}
realPath, err := filepath.EvalSymlinks(path)
if err != nil {
return "", err
}
return realPath, nil
}
func fsForGlobalPath(path string) (fs.FS, string, error) {
realPath, err := sanitizePath(path)
if err != nil {
return nil, "", err
}
dirPath, fileName := filepath.Split(realPath)
if dirPath == "" {
dirPath = "."
}
return os.DirFS(dirPath), fileName, nil
}
func (p PrivateKeyFileParser) ServiceAccount(context.Context) (ServiceAccount, error) {
if p.fs == nil {
var err error
p.fs, p.privateKeyPath, err = fsForGlobalPath(p.privateKeyPath)
if err != nil {
return ServiceAccount{}, err
}
}
encoded, err := fs.ReadFile(p.fs, p.privateKeyPath)
if err != nil {
return ServiceAccount{}, err
}
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(encoded)
if err != nil {
return ServiceAccount{}, err
}
return ServiceAccount{
PrivateKey: privateKey,
PublicKeyID: p.publicKeyID,
ServiceAccountID: p.serviceAccountID,
}, nil
}
// ServiceAccountCredentialsFileParser is a [ServiceAccountReader] that parses a JSON file
// containing service account credentials.
type ServiceAccountCredentialsFileParser struct {
fs fs.FS
credentialsPath string
}
var _ ServiceAccountReader = ServiceAccountCredentialsFileParser{}
// NewServiceAccountCredentialsFileParser is a [ServiceAccountReader] that parses a JSON file
// containing service account credentials. The file format is following:
//
// {
// "subject-credentials": {
// "alg": "RS256",
// "private-key": "PKCS#8 PEM with new lines escaped as \n",
// "kid": "publickey-...",
// "iss": "serviceaccount-...",
// "sub": "serviceaccount-..."
// }
// }
//
// Note: Field names follow standard abbreviations, similar to JWT standards or RFC 7515.
//
// If fs is nil, the default file system will be used.
func NewServiceAccountCredentialsFileParser(fs fs.FS, credentialsPath string) ServiceAccountCredentialsFileParser {
return ServiceAccountCredentialsFileParser{
fs: fs,
credentialsPath: credentialsPath,
}
}
func (p ServiceAccountCredentialsFileParser) ServiceAccount(context.Context) (ServiceAccount, error) {
creds, err := p.SubjectCredentials()
if err != nil {
return ServiceAccount{}, err
}
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(creds.PrivateKey))
if err != nil {
return ServiceAccount{}, fmt.Errorf("invalid service account private key field: %w", err)
}
return ServiceAccount{
PrivateKey: privateKey,
PublicKeyID: creds.KeyID,
ServiceAccountID: creds.Subject,
}, nil
}
func (p ServiceAccountCredentialsFileParser) SubjectCredentials() (SubjectCredentials, error) {
if p.fs == nil {
var err error
p.fs, p.credentialsPath, err = fsForGlobalPath(p.credentialsPath)
if err != nil {
return SubjectCredentials{}, err
}
}
data, err := fs.ReadFile(p.fs, p.credentialsPath)
if err != nil {
return SubjectCredentials{}, fmt.Errorf("can't read service account credentials file: %w", err)
}
var credsWithWrapper ServiceAccountCredentials
err = json.Unmarshal(data, &credsWithWrapper)
if err != nil {
return SubjectCredentials{}, fmt.Errorf("parse service account creds: %w", err)
}
err = credsWithWrapper.SubjectCredentials.validate()
if err != nil {
return SubjectCredentials{}, err
}
return credsWithWrapper.SubjectCredentials, nil
}
// ServiceAccountCredentials represents the structure of a service account credentials file.
type ServiceAccountCredentials struct {
SubjectCredentials SubjectCredentials `json:"subject-credentials"`
}
type SubjectCredentials struct {
Type string `json:"type,omitempty"`
Alg string `json:"alg"`
PrivateKey string `json:"private-key"` // PrivateKey is a PEM-encoded PKCS8 string.
KeyID string `json:"kid"`
Issuer string `json:"iss"`
Subject string `json:"sub"`
}
func (c SubjectCredentials) validate() error {
if c.Type != "" && c.Type != "JWT" {
return fmt.Errorf("invalid service account credentials type: '%s', the only supported value is 'JWT'", c.Type)
}
if c.Alg != "RS256" {
return fmt.Errorf("invalid service account algorithm: %s. Only RS256 is supported", c.Alg)
}
if c.Issuer != c.Subject {
return fmt.Errorf("invalid service account subject must be the same as issuer: %s != %s", c.Issuer, c.Subject)
}
return nil
}