Skip to content

feat: enable key rotation #15066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 42 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
b745c8e
feat: enable key rotation
sreya Oct 5, 2024
b98bff0
add migration
sreya Oct 5, 2024
0646b30
Refactor cryptographic key handling for OIDC and API keys
sreya Oct 5, 2024
b73b210
the end is nigh
sreya Oct 5, 2024
7fe88ea
fix migrations
sreya Oct 14, 2024
0323f79
Refactor key cache management for better clarity
sreya Oct 14, 2024
7413907
hm
sreya Oct 14, 2024
2ea31ab
fixing tests
sreya Oct 14, 2024
d0d168b
time to fix it
sreya Oct 15, 2024
33cdb96
Refactor cryptokeys Fetcher to include feature param
sreya Oct 16, 2024
08570b7
Refactor key caching and logging behavior
sreya Oct 16, 2024
b770762
Refactor crypto_key_feature migration logic
sreya Oct 16, 2024
7557ed2
Refactor key management and enhance logging
sreya Oct 16, 2024
fa9a75d
Update cryptokey feature test and migration logic
sreya Oct 17, 2024
94987b6
gen
sreya Oct 17, 2024
76561ac
Refactor cryptokeys cache to include key reader context
sreya Oct 17, 2024
53dcf36
Refactor jwtutils to remove redundant key reader
sreya Oct 17, 2024
c656d00
Remove unused cryptokey feature fixtures
sreya Oct 17, 2024
e7cfb46
Refactor cryptokeys comments and variable typo
sreya Oct 17, 2024
6432b0d
fix comments
sreya Oct 17, 2024
9ad187d
move rotator out of coderd
sreya Oct 17, 2024
d16e98f
remove composite jwtutil interfaces
sreya Oct 17, 2024
3809cd5
fix tests caused by moving rotator initiation out of coderd
sreya Oct 17, 2024
6d3c103
pr comments
sreya Oct 20, 2024
a3020fc
Merge branch 'main' into jon/glue
sreya Oct 20, 2024
bfa88b7
Refactor tests to remove direct database setup
sreya Oct 20, 2024
495c28f
Rename cryptokey migration files to update sequence
sreya Oct 20, 2024
692bb36
Fix conditional logging in key cache initialization
sreya Oct 20, 2024
4028995
Refactor crypto key management in tests
sreya Oct 20, 2024
6b9a3e4
add test for oidc jwt
sreya Oct 21, 2024
5ee6ad5
add test for tailnet_resume jwt
sreya Oct 21, 2024
886d87c
add test for workspaceapps
sreya Oct 21, 2024
5261442
add test for signedtoken
sreya Oct 22, 2024
4a1d974
fix migrations
sreya Oct 22, 2024
092a241
fmt
sreya Oct 22, 2024
87828a2
pr comments
sreya Oct 22, 2024
6aa90bc
Merge branch 'main' into jon/glue
sreya Oct 22, 2024
358aaa8
Rename cryptokey migration files to update sequence
sreya Oct 22, 2024
ad237ad
Merge branch 'main' into jon/glue
sreya Oct 24, 2024
5798a33
migrations
sreya Oct 24, 2024
200cd68
Refactor StaticKey to jwtutils package
sreya Oct 24, 2024
2194b4d
fix tests
sreya Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor key caching and logging behavior
- Improve code clarity by refactoring key caching logic.
- Simplify logger initialization for signing and encryption caches.
- Ensure consistent closing of caches in the API and server.
  • Loading branch information
sreya committed Oct 16, 2024
commit 08570b76f9ca9f1660b817b503545286c021f52d
2 changes: 1 addition & 1 deletion coderd/activitybump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestWorkspaceActivityBump(t *testing.T) {
}

// maxTimeDrift is how long we are willing wait for a deadline to
// be increased. Since it could have been bumped at the intial
// be increased. Since it could have been bumped at the initial
maxTimeDrift := testutil.WaitMedium

updatedAfter := dbtime.Now()
Expand Down
11 changes: 7 additions & 4 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func New(options *Options) *API {

if options.OIDCConvertKeyCache == nil {
options.OIDCConvertKeyCache, err = cryptokeys.NewSigningCache(ctx,
options.Logger.Named("oidc_convert_keycache"),
options.Logger,
fetcher,
codersdk.CryptoKeyFeatureOIDCConvert,
)
Expand All @@ -470,7 +470,7 @@ func New(options *Options) *API {

if options.AppSigningKeyCache == nil {
options.AppSigningKeyCache, err = cryptokeys.NewSigningCache(ctx,
options.Logger.Named("app_signing_keycache"),
options.Logger,
fetcher,
codersdk.CryptoKeyFeatureWorkspaceAppsToken,
)
Expand All @@ -479,7 +479,7 @@ func New(options *Options) *API {

if options.AppEncryptionKeyCache == nil {
options.AppEncryptionKeyCache, err = cryptokeys.NewEncryptionCache(ctx,
options.Logger.Named("app_encryption_keycache"),
options.Logger,
fetcher,
codersdk.CryptoKeyFeatureWorkspaceAppsAPIKey,
)
Expand Down Expand Up @@ -522,7 +522,7 @@ func New(options *Options) *API {
options.Database,
options.Pubsub,
),
dbRolluper: options.DatabaseRolluper,
dbRolluper: options.DatabaseRolluper,
}

f := appearance.NewDefaultFetcher(api.DeploymentValues.DocsURL.String())
Expand Down Expand Up @@ -1474,6 +1474,9 @@ func (api *API) Close() error {
_ = api.agentProvider.Close()
_ = api.statsReporter.Close()
_ = api.NetworkTelemetryBatcher.Close()
_ = api.OIDCConvertKeyCache.Close()
_ = api.AppSigningKeyCache.Close()
_ = api.AppEncryptionKeyCache.Close()
return nil
}

Expand Down
3 changes: 3 additions & 0 deletions coderd/cryptokeys/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cryptokeys
import (
"context"
"encoding/hex"
"fmt"
"io"
"strconv"
"sync"
Expand Down Expand Up @@ -108,6 +109,7 @@ func NewSigningCache(ctx context.Context, logger slog.Logger, fetcher Fetcher,
if !isSigningKeyFeature(feature) {
return nil, xerrors.Errorf("invalid feature: %s", feature)
}
logger = logger.Named(fmt.Sprintf("%s_signing_keycache", feature))
return newCache(ctx, logger, fetcher, feature, opts...)
}

Expand All @@ -117,6 +119,7 @@ func NewEncryptionCache(ctx context.Context, logger slog.Logger, fetcher Fetcher
if !isEncryptionKeyFeature(feature) {
return nil, xerrors.Errorf("invalid feature: %s", feature)
}
logger = logger.Named(fmt.Sprintf("%s_encryption_keycache", feature))
return newCache(ctx, logger, fetcher, feature, opts...)
}

Expand Down
6 changes: 5 additions & 1 deletion enterprise/coderd/coderdenttest/proxytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ type WorkspaceProxy struct {
// owner client. If a token is provided, the proxy will become a replica of the
// existing proxy region.
func NewWorkspaceProxyReplica(t *testing.T, coderdAPI *coderd.API, owner *codersdk.Client, options *ProxyOptions) WorkspaceProxy {
t.Helper()

ctx, cancelFunc := context.WithCancel(context.Background())
t.Cleanup(cancelFunc)

Expand Down Expand Up @@ -142,8 +144,10 @@ func NewWorkspaceProxyReplica(t *testing.T, coderdAPI *coderd.API, owner *coders
statsCollectorOptions.Flush = options.FlushStats
}

logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug).With(slog.F("server_url", serverURL.String()))

wssrv, err := wsproxy.New(ctx, &wsproxy.Options{
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug).With(slog.F("server_url", serverURL.String())),
Logger: logger,
Experiments: options.Experiments,
DashboardURL: coderdAPI.AccessURL,
AccessURL: accessURL,
Expand Down
3 changes: 2 additions & 1 deletion enterprise/wsproxy/keyfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package wsproxy
import (
"context"

"golang.org/x/xerrors"

"github.com/coder/coder/v2/coderd/cryptokeys"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/enterprise/wsproxy/wsproxysdk"
"golang.org/x/xerrors"
)

var _ cryptokeys.Fetcher = &ProxyFetcher{}
Expand Down
35 changes: 28 additions & 7 deletions enterprise/wsproxy/wsproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ type Options struct {
// from the dashboardURL. This should only be used in development.
AllowAllCors bool

StatsCollectorOptions workspaceapps.StatsCollectorOptions
WorkspaceAppsEncryptionKeycache cryptokeys.EncryptionKeycache
WorkspaceAppsSigningKeycache cryptokeys.SigningKeycache
StatsCollectorOptions workspaceapps.StatsCollectorOptions
}

func (o *Options) Validate() error {
Expand Down Expand Up @@ -133,6 +131,9 @@ type Server struct {
// the moon's token.
SDKClient *wsproxysdk.Client

WorkspaceAppsEncryptionKeycache cryptokeys.EncryptionKeycache
WorkspaceAppsSigningKeycache cryptokeys.SigningKeycache

// DERP
derpMesh *derpmesh.Mesh
derpMeshTLSConfig *tls.Config
Expand Down Expand Up @@ -199,8 +200,28 @@ func New(ctx context.Context, opts *Options) (*Server, error) {

ctx, cancel := context.WithCancel(context.Background())

encryptionCache, err := cryptokeys.NewEncryptionCache(ctx,
opts.Logger,
&ProxyFetcher{Client: client},
codersdk.CryptoKeyFeatureWorkspaceAppsAPIKey,
)
if err != nil {
return nil, xerrors.Errorf("create api key encryption cache: %w", err)
}
signingCache, err := cryptokeys.NewSigningCache(ctx,
opts.Logger,
&ProxyFetcher{Client: client},
codersdk.CryptoKeyFeatureWorkspaceAppsToken,
)
if err != nil {
return nil, xerrors.Errorf("create api token signing cache: %w", err)
}

r := chi.NewRouter()
s := &Server{
ctx: ctx,
cancel: cancel,

Options: opts,
Handler: r,
DashboardURL: opts.DashboardURL,
Expand All @@ -210,8 +231,6 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
SDKClient: client,
derpMesh: derpmesh.New(opts.Logger.Named("net.derpmesh"), derpServer, meshTLSConfig),
derpMeshTLSConfig: meshTLSConfig,
ctx: ctx,
cancel: cancel,
}

// Register the workspace proxy with the primary coderd instance and start a
Expand Down Expand Up @@ -280,8 +299,8 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
AccessURL: opts.AccessURL,
AppHostname: opts.AppHostname,
Client: client,
SigningKey: opts.WorkspaceAppsSigningKeycache,
EncryptingKey: opts.WorkspaceAppsEncryptionKeycache,
SigningKey: signingCache,
EncryptingKey: encryptionCache,
Logger: s.Logger.Named("proxy_token_provider"),
},

Expand Down Expand Up @@ -432,6 +451,8 @@ func (s *Server) Close() error {
err = multierror.Append(err, agentProviderErr)
}
s.SDKClient.SDKClient.HTTPClient.CloseIdleConnections()
_ = s.WorkspaceAppsSigningKeycache.Close()
_ = s.WorkspaceAppsEncryptionKeycache.Close()
return err
}

Expand Down