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 cryptokeys Fetcher to include feature param
Enhances flexibility by making the `Fetcher` interface
receive a `CryptoKeyFeature` parameter. This change aligns
various call sites that implement or utilize `Fetcher`, allowing
for more granular queries.
  • Loading branch information
sreya committed Oct 16, 2024
commit 33cdb96baa0f67e0b4d2f7f34326a436b8cc0f9c
13 changes: 11 additions & 2 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,9 +746,18 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
return xerrors.Errorf("set deployment id: %w", err)
}

resumeKeycache, err := cryptokeys.NewSigningCache(logger, options.Database, database.CryptoKeyFeatureTailnetResume)
fetcher := &cryptokeys.DBFetcher{
DB: options.Database,
}

// TODO(JonA): The instantiation of this cache + coordinator seems like it should be done inside coderd so that it uses the correct context.
resumeKeycache, err := cryptokeys.NewSigningCache(ctx,
logger,
fetcher,
codersdk.CryptoKeyFeatureTailnetResume,
)
if err != nil {
return xerrors.Errorf("create resume token key cache: %w", err)
return xerrors.Errorf("create tailnet resume key cache: %w", err)
}

options.CoordinatorResumeTokenProvider = tailnet.NewResumeTokenKeyProvider(resumeKeycache, quartz.NewReal(), tailnet.DefaultResumeTokenExpiry)
Expand Down
52 changes: 45 additions & 7 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,44 @@ func New(options *Options) *API {
if err != nil {
panic(xerrors.Errorf("get deployment ID: %w", err))
}

// Start a background process that rotates keys.
err = cryptokeys.StartRotator(ctx, options.Logger.Named("keyrotator"), options.Database)
if err != nil {
options.Logger.Fatal(ctx, "start key rotator", slog.Error(err))
}

fetcher := &cryptokeys.DBFetcher{
DB: options.Database,
}

if options.OIDCConvertKeyCache == nil {
options.OIDCConvertKeyCache, err = cryptokeys.NewSigningCache(ctx,
options.Logger.Named("oidc_convert_keycache"),
fetcher,
codersdk.CryptoKeyFeatureOIDCConvert,
)
must(options.Logger, "start oidc convert key cache", err)
}

if options.AppSigningKeyCache == nil {
options.AppSigningKeyCache, err = cryptokeys.NewSigningCache(ctx,
options.Logger.Named("app_signing_keycache"),
fetcher,
codersdk.CryptoKeyFeatureWorkspaceAppsToken,
)
must(options.Logger, "start app signing key cache", err)
}

if options.AppEncryptionKeyCache == nil {
options.AppEncryptionKeyCache, err = cryptokeys.NewEncryptionCache(ctx,
options.Logger.Named("app_encryption_keycache"),
fetcher,
codersdk.CryptoKeyFeatureWorkspaceAppsAPIKey,
)
must(options.Logger, "start app encryption key cache", err)
}

api := &API{
ctx: ctx,
cancel: cancel,
Expand Down Expand Up @@ -484,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 @@ -613,12 +651,6 @@ func New(options *Options) *API {
api.Logger.Fatal(api.ctx, "failed to initialize tailnet client service", slog.Error(err))
}

// Start a background process that rotates keys.
err = cryptokeys.StartRotator(api.ctx, api.Logger.Named("keyrotator"), api.Database)
if err != nil {
api.Logger.Fatal(api.ctx, "start key rotator", slog.Error(err))
}

api.statsReporter = workspacestats.NewReporter(workspacestats.ReporterOptions{
Database: options.Database,
Logger: options.Logger.Named("workspacestats"),
Expand Down Expand Up @@ -1612,3 +1644,9 @@ func ReadExperiments(log slog.Logger, raw []string) codersdk.Experiments {
}
return exps
}

func must(logger slog.Logger, msg string, err error) {
if err != nil {
logger.Fatal(context.Background(), msg, slog.Error(err))
}
}
11 changes: 0 additions & 11 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import (
"github.com/coder/coder/v2/coderd/audit"
"github.com/coder/coder/v2/coderd/autobuild"
"github.com/coder/coder/v2/coderd/awsidentity"
"github.com/coder/coder/v2/coderd/cryptokeys"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/db2sdk"
"github.com/coder/coder/v2/coderd/database/dbauthz"
Expand Down Expand Up @@ -326,13 +325,6 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
}
auditor.Store(&options.Auditor)

oidcConvertKeyCache, err := cryptokeys.NewSigningCache(options.Logger.Named("oidc_convert_keycache"), options.Database, database.CryptoKeyFeatureOIDCConvert)
require.NoError(t, err)
appSigningKeyCache, err := cryptokeys.NewSigningCache(options.Logger.Named("app_signing_keycache"), options.Database, database.CryptoKeyFeatureWorkspaceAppsToken)
require.NoError(t, err)
appEncryptionKeyCache, err := cryptokeys.NewEncryptionCache(options.Logger.Named("app_encryption_keycache"), options.Database, database.CryptoKeyFeatureWorkspaceAppsAPIKey)
require.NoError(t, err)

ctx, cancelFunc := context.WithCancel(context.Background())
lifecycleExecutor := autobuild.NewExecutor(
ctx,
Expand Down Expand Up @@ -540,9 +532,6 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
WorkspaceUsageTracker: wuTracker,
NotificationsEnqueuer: options.NotificationsEnqueuer,
OneTimePasscodeValidityPeriod: options.OneTimePasscodeValidityPeriod,
AppSigningKeyCache: appSigningKeyCache,
AppEncryptionKeyCache: appEncryptionKeyCache,
OIDCConvertKeyCache: oidcConvertKeyCache,
}
}

Expand Down
13 changes: 6 additions & 7 deletions coderd/cryptokeys/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
)

type Fetcher interface {
Fetch(ctx context.Context) ([]codersdk.CryptoKey, error)
Fetch(ctx context.Context, feature codersdk.CryptoKeyFeature) ([]codersdk.CryptoKey, error)
}

type EncryptionKeycache interface {
Expand Down Expand Up @@ -62,12 +62,11 @@ const (
)

type DBFetcher struct {
DB database.Store
Feature database.CryptoKeyFeature
DB database.Store
}

func (d *DBFetcher) Fetch(ctx context.Context) ([]codersdk.CryptoKey, error) {
keys, err := d.DB.GetCryptoKeysByFeature(ctx, d.Feature)
func (d *DBFetcher) Fetch(ctx context.Context, feature codersdk.CryptoKeyFeature) ([]codersdk.CryptoKey, error) {
keys, err := d.DB.GetCryptoKeysByFeature(ctx, database.CryptoKeyFeature(feature))
if err != nil {
return nil, xerrors.Errorf("get crypto keys by feature: %w", err)
}
Expand Down Expand Up @@ -198,7 +197,7 @@ func (c *cache) VerifyingKey(ctx context.Context, id string) (interface{}, error
}

func isEncryptionKeyFeature(feature codersdk.CryptoKeyFeature) bool {
return feature == codersdk.CryptoKeyFeatureWorkspaceApp
return feature == codersdk.CryptoKeyFeatureWorkspaceAppsAPIKey
}

func isSigningKeyFeature(feature codersdk.CryptoKeyFeature) bool {
Expand Down Expand Up @@ -332,7 +331,7 @@ func (c *cache) refresh() {
// cryptoKeys queries the control plane for the crypto keys.
// Outside of initialization, this should only be called by fetch.
func (c *cache) cryptoKeys(ctx context.Context) (map[int32]codersdk.CryptoKey, error) {
keys, err := c.fetcher.Fetch(ctx)
keys, err := c.fetcher.Fetch(ctx, c.feature)
if err != nil {
return nil, xerrors.Errorf("crypto keys: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion coderd/cryptokeys/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ type fakeFetcher struct {
called int
}

func (f *fakeFetcher) Fetch(_ context.Context) ([]codersdk.CryptoKey, error) {
func (f *fakeFetcher) Fetch(_ context.Context, _ codersdk.CryptoKeyFeature) ([]codersdk.CryptoKey, error) {
f.called++
return f.keys, nil
}
Expand Down
6 changes: 3 additions & 3 deletions coderd/jwtutils/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func TestJWS(t *testing.T) {
StartsAt: time.Now(),
})
log = slogtest.Make(t, nil)
fetcher = &cryptokeys.DBFetcher{DB: db, Feature: database.CryptoKeyFeatureOidcConvert}
fetcher = &cryptokeys.DBFetcher{DB: db}
)

cache, err := cryptokeys.NewSigningCache(ctx, log, fetcher, codersdk.CryptoKeyFeatureOIDCConvert)
Expand Down Expand Up @@ -331,10 +331,10 @@ func TestJWE(t *testing.T) {
})
log = slogtest.Make(t, nil)

fetcher = &cryptokeys.DBFetcher{DB: db, Feature: database.CryptoKeyFeatureWorkspaceApps}
fetcher = &cryptokeys.DBFetcher{DB: db}
)

cache, err := cryptokeys.NewEncryptionCache(ctx, log, fetcher, codersdk.CryptoKeyFeatureWorkspaceApp)
cache, err := cryptokeys.NewEncryptionCache(ctx, log, fetcher, codersdk.CryptoKeyFeatureWorkspaceAppsAPIKey)
require.NoError(t, err)

claims := testClaims{
Expand Down
2 changes: 1 addition & 1 deletion codersdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3109,7 +3109,7 @@ func (c *Client) SSHConfiguration(ctx context.Context) (SSHConfigResponse, error
type CryptoKeyFeature string

const (
CryptoKeyFeatureWorkspaceAppAPIKey CryptoKeyFeature = "workspace_apps_api_key"
CryptoKeyFeatureWorkspaceAppsAPIKey CryptoKeyFeature = "workspace_apps_api_key"
//nolint:gosec // This denotes a type of key, not a literal.
CryptoKeyFeatureWorkspaceAppsToken CryptoKeyFeature = "workspace_apps_token"
CryptoKeyFeatureOIDCConvert CryptoKeyFeature = "oidc_convert"
Expand Down
4 changes: 2 additions & 2 deletions enterprise/coderd/workspaceproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ func TestGetCryptoKeys(t *testing.T) {
Name: testutil.GetRandomName(t),
})

keys, err := proxy.SDKClient.CryptoKeys(ctx, codersdk.CryptoKeyFeatureWorkspaceAppAPIKey)
keys, err := proxy.SDKClient.CryptoKeys(ctx, codersdk.CryptoKeyFeatureWorkspaceAppsAPIKey)
require.NoError(t, err)
require.NotEmpty(t, keys)
require.Equal(t, 2, len(keys.CryptoKeys))
Expand Down Expand Up @@ -986,7 +986,7 @@ func TestGetCryptoKeys(t *testing.T) {
client := wsproxysdk.New(cclient.URL)
client.SetSessionToken(cclient.SessionToken())

_, err := client.CryptoKeys(ctx, codersdk.CryptoKeyFeatureWorkspaceAppAPIKey)
_, err := client.CryptoKeys(ctx, codersdk.CryptoKeyFeatureWorkspaceAppsAPIKey)
require.Error(t, err)
var sdkErr *codersdk.Error
require.ErrorAs(t, err, &sdkErr)
Expand Down
7 changes: 3 additions & 4 deletions enterprise/wsproxy/keyfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import (
var _ cryptokeys.Fetcher = &ProxyFetcher{}

type ProxyFetcher struct {
Client *wsproxysdk.Client
Feature codersdk.CryptoKeyFeature
Client *wsproxysdk.Client
}

func (p *ProxyFetcher) Fetch(ctx context.Context) ([]codersdk.CryptoKey, error) {
keys, err := p.Client.CryptoKeys(ctx)
func (p *ProxyFetcher) Fetch(ctx context.Context, feature codersdk.CryptoKeyFeature) ([]codersdk.CryptoKey, error) {
keys, err := p.Client.CryptoKeys(ctx, feature)
if err != nil {
return nil, xerrors.Errorf("crypto keys: %w", err)
}
Expand Down