-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cache.go
204 lines (170 loc) · 4.75 KB
/
cache.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
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.
package idp
import (
"context"
"crypto/rsa"
"encoding/json"
"fmt"
"math/rand"
"sync"
"time"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/redis/go-redis/v9"
"gopkg.in/square/go-jose.v2"
)
// KeyCache caches public keys to ensure they're returned with the JWKS as long
// as there are valid tokens out there using those keys.
//
// PoC Note: in production this cache would likely be implemted using Redis or the database.
type KeyCache interface {
// Set rotates the current key
Set(ctx context.Context, current *rsa.PrivateKey) error
// Signer produces a new key signer or nil if Set() hasn't been called yet
Signer(ctx context.Context) (jose.Signer, error)
// PublicKeys returns all un-expired public keys as JSON-encoded *jose.JSONWebKeySet.
// This function returns the JSON-encoded form directly instead of the *jose.JSONWebKeySet
// to allow for persisted JSON implementations of this interface.
PublicKeys(ctx context.Context) ([]byte, error)
}
type inMemoryKey struct {
ID string
Created time.Time
Key *rsa.PublicKey
}
func NewInMemoryCache() *InMemoryCache {
return &InMemoryCache{
keys: make(map[string]*inMemoryKey),
}
}
type InMemoryCache struct {
mu sync.RWMutex
current *rsa.PrivateKey
currentID string
keys map[string]*inMemoryKey
}
// Set rotates the current key
func (imc *InMemoryCache) Set(ctx context.Context, current *rsa.PrivateKey) error {
imc.mu.Lock()
defer imc.mu.Unlock()
id := fmt.Sprintf("id%d%d", time.Now().Unix(), rand.Int())
imc.currentID = id
imc.current = current
imc.keys[id] = &inMemoryKey{
ID: id,
Created: time.Now(),
Key: ¤t.PublicKey,
}
return nil
}
// Signer produces a new key signer or nil if Set() hasn't been called yet
func (imc *InMemoryCache) Signer(ctx context.Context) (jose.Signer, error) {
if imc.current == nil {
return nil, nil
}
return jose.NewSigner(jose.SigningKey{
Algorithm: jose.RS256,
Key: imc.current,
}, nil)
}
// PublicKeys returns all un-expired public keys
func (imc *InMemoryCache) PublicKeys(ctx context.Context) ([]byte, error) {
imc.mu.RLock()
defer imc.mu.RUnlock()
var jwks jose.JSONWebKeySet
for _, key := range imc.keys {
jwks.Keys = append(jwks.Keys, jose.JSONWebKey{
Key: key.Key,
KeyID: key.ID,
Algorithm: string(jose.RS256),
})
}
return json.Marshal(jwks)
}
const (
redisCacheDefaultTTL = 1 * time.Hour
redisIDPKeyPrefix = "idp:keys:"
)
func NewRedisCache(client *redis.Client) *RedisCache {
return &RedisCache{
Client: client,
keyID: defaultKeyID,
}
}
func defaultKeyID(current *rsa.PrivateKey) string {
return fmt.Sprintf("id-%d-%d", time.Now().UnixMicro(), rand.Int())
}
type RedisCache struct {
Client *redis.Client
keyID func(current *rsa.PrivateKey) string
mu sync.RWMutex
current *rsa.PrivateKey
currentID string
}
// PublicKeys implements KeyCache
func (rc *RedisCache) PublicKeys(ctx context.Context) ([]byte, error) {
var (
res = []byte("{\"keys\":[")
first = true
)
iter := rc.Client.Scan(ctx, 0, redisIDPKeyPrefix+"*", 0).Iterator()
for iter.Next(ctx) {
key, err := rc.Client.Get(ctx, iter.Val()).Result()
if err != nil {
return nil, err
}
if !first {
res = append(res, []byte(",")...)
}
res = append(res, []byte(key)...)
first = false
}
if err := iter.Err(); err != nil {
return nil, err
}
res = append(res, []byte("]}")...)
return res, nil
}
// Set implements KeyCache
func (rc *RedisCache) Set(ctx context.Context, current *rsa.PrivateKey) error {
rc.mu.Lock()
defer rc.mu.Unlock()
id := rc.keyID(current)
publicKey := jose.JSONWebKey{
Key: ¤t.PublicKey,
KeyID: id,
Algorithm: string(jose.RS256),
}
publicKeyJSON, err := json.Marshal(publicKey)
if err != nil {
return err
}
redisKey := fmt.Sprintf("%s%s", redisIDPKeyPrefix, id)
err = rc.Client.Set(ctx, redisKey, string(publicKeyJSON), redisCacheDefaultTTL).Err()
if err != nil {
return err
}
rc.currentID = id
rc.current = current
return nil
}
// Signer implements KeyCache
func (rc *RedisCache) Signer(ctx context.Context) (jose.Signer, error) {
if rc.current == nil {
return nil, nil
}
err := rc.Client.Expire(ctx, redisIDPKeyPrefix+rc.currentID, redisCacheDefaultTTL).Err()
if err != nil {
log.WithField("keyID", rc.currentID).WithError(err).Warn("cannot extend cached IDP public key TTL")
}
return jose.NewSigner(jose.SigningKey{
Algorithm: jose.RS256,
Key: rc.current,
}, &jose.SignerOptions{
ExtraHeaders: map[jose.HeaderKey]interface{}{
jose.HeaderKey("kid"): rc.currentID,
},
})
}
var _ KeyCache = ((*RedisCache)(nil))