Skip to content

Commit 200cd68

Browse files
committed
Refactor StaticKey to jwtutils package
移動StaticKeyからcryptokeysパッケージへのjwtutils。 これにより、jwtutilsとのセキュリティおよび独立性の一貫性が強化されます。 また、tailnetの依存関係を減らします。
1 parent 5798a33 commit 200cd68

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

coderd/cryptokeys/cache.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -379,38 +379,6 @@ func (c *cache) Close() error {
379379
return nil
380380
}
381381

382-
// StaticKey fulfills the SigningKeycache and EncryptionKeycache interfaces. Useful for testing.
383-
type StaticKey struct {
384-
ID string
385-
Key interface{}
386-
}
387-
388-
func (s StaticKey) SigningKey(_ context.Context) (string, interface{}, error) {
389-
return s.ID, s.Key, nil
390-
}
391-
392-
func (s StaticKey) VerifyingKey(_ context.Context, id string) (interface{}, error) {
393-
if id != s.ID {
394-
return nil, xerrors.Errorf("invalid id %q", id)
395-
}
396-
return s.Key, nil
397-
}
398-
399-
func (s StaticKey) EncryptingKey(_ context.Context) (string, interface{}, error) {
400-
return s.ID, s.Key, nil
401-
}
402-
403-
func (s StaticKey) DecryptingKey(_ context.Context, id string) (interface{}, error) {
404-
if id != s.ID {
405-
return nil, xerrors.Errorf("invalid id %q", id)
406-
}
407-
return s.Key, nil
408-
}
409-
410-
func (StaticKey) Close() error {
411-
return nil
412-
}
413-
414382
// We have to do this to avoid a circular dependency on db2sdk (cryptokeys -> db2sdk -> tailnet -> cryptokeys)
415383
func toSDKKeys(keys []database.CryptoKey) []codersdk.CryptoKey {
416384
into := make([]codersdk.CryptoKey, 0, len(keys))

coderd/jwtutils/jws.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ const (
4141
signingAlgo = jose.HS512
4242
)
4343

44+
type SigningKeyManager interface {
45+
SigningKeyProvider
46+
VerifyKeyProvider
47+
}
48+
4449
type SigningKeyProvider interface {
4550
SigningKey(ctx context.Context) (id string, key interface{}, err error)
4651
}
@@ -148,3 +153,35 @@ func Verify(ctx context.Context, v VerifyKeyProvider, token string, claims Claim
148153

149154
return claims.Validate(options.RegisteredClaims)
150155
}
156+
157+
// StaticKey fulfills the SigningKeycache and EncryptionKeycache interfaces. Useful for testing.
158+
type StaticKey struct {
159+
ID string
160+
Key interface{}
161+
}
162+
163+
func (s StaticKey) SigningKey(_ context.Context) (string, interface{}, error) {
164+
return s.ID, s.Key, nil
165+
}
166+
167+
func (s StaticKey) VerifyingKey(_ context.Context, id string) (interface{}, error) {
168+
if id != s.ID {
169+
return nil, xerrors.Errorf("invalid id %q", id)
170+
}
171+
return s.Key, nil
172+
}
173+
174+
func (s StaticKey) EncryptingKey(_ context.Context) (string, interface{}, error) {
175+
return s.ID, s.Key, nil
176+
}
177+
178+
func (s StaticKey) DecryptingKey(_ context.Context, id string) (interface{}, error) {
179+
if id != s.ID {
180+
return nil, xerrors.Errorf("invalid id %q", id)
181+
}
182+
return s.Key, nil
183+
}
184+
185+
func (StaticKey) Close() error {
186+
return nil
187+
}

tailnet/resume.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"google.golang.org/protobuf/types/known/durationpb"
1212
"google.golang.org/protobuf/types/known/timestamppb"
1313

14-
"github.com/coder/coder/v2/coderd/cryptokeys"
1514
"github.com/coder/coder/v2/coderd/jwtutils"
1615
"github.com/coder/coder/v2/tailnet/proto"
1716
"github.com/coder/quartz"
@@ -29,7 +28,7 @@ func NewInsecureTestResumeTokenProvider() ResumeTokenProvider {
2928
if err != nil {
3029
panic(err)
3130
}
32-
return NewResumeTokenKeyProvider(cryptokeys.StaticKey{
31+
return NewResumeTokenKeyProvider(jwtutils.StaticKey{
3332
ID: uuid.New().String(),
3433
Key: key[:],
3534
}, quartz.NewReal(), time.Hour)
@@ -52,12 +51,12 @@ func GenerateResumeTokenSigningKey() (ResumeTokenSigningKey, error) {
5251
}
5352

5453
type ResumeTokenKeyProvider struct {
55-
key cryptokeys.SigningKeycache
54+
key jwtutils.SigningKeyManager
5655
clock quartz.Clock
5756
expiry time.Duration
5857
}
5958

60-
func NewResumeTokenKeyProvider(key cryptokeys.SigningKeycache, clock quartz.Clock, expiry time.Duration) ResumeTokenProvider {
59+
func NewResumeTokenKeyProvider(key jwtutils.SigningKeyManager, clock quartz.Clock, expiry time.Duration) ResumeTokenProvider {
6160
if expiry <= 0 {
6261
expiry = DefaultResumeTokenExpiry
6362
}

0 commit comments

Comments
 (0)