Skip to content

feat: encrypt oidc and git auth tokens in the database #7959

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Automatically delete rows when not encrypted
  • Loading branch information
kylecarbs committed Jun 12, 2023
commit deb577b6bab23bac01103303d3eeef7860caa23a
28 changes: 22 additions & 6 deletions coderd/database/dbcrypt/dbcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package dbcrypt
import (
"context"
"database/sql"
"encoding/base64"
"runtime"
"strings"
"sync/atomic"

"cdr.dev/slog"
"golang.org/x/xerrors"

"github.com/coder/coder/coderd/database"
Expand All @@ -22,6 +25,7 @@ type Options struct {
// to encrypt/decrypt user link and git auth link tokens. If this is nil,
// then no encryption/decryption will be performed.
ExternalTokenCipher *atomic.Pointer[cryptorand.Cipher]
Logger slog.Logger
}

// New creates a database.Store wrapper that encrypts/decrypts values
Expand Down Expand Up @@ -128,19 +132,25 @@ func (db *dbCrypt) encryptFields(fields ...*string) error {
if err != nil {
return err
}
*field = MagicPrefix + string(encrypted)
// Base64 is used to support UTF-8 encoding in PostgreSQL.
*field = MagicPrefix + base64.StdEncoding.EncodeToString(encrypted)
}
return nil
}

// decryptFields decrypts the given fields in place.
// If the value fails to decrypt, sql.ErrNoRows will be returned.
func (db *dbCrypt) decryptFields(deleteFn func() error, fields ...*string) error {
delete := func() error {
delete := func(reason string) error {
err := deleteFn()
if err != nil {
return xerrors.Errorf("delete encrypted row: %w", err)
}
pc, _, _, ok := runtime.Caller(2)
details := runtime.FuncForPC(pc)
if ok && details != nil {
db.Logger.Debug(context.Background(), "deleted row", slog.F("reason", reason), slog.F("caller", details.Name()))
}
return sql.ErrNoRows
}

Expand All @@ -154,7 +164,7 @@ func (db *dbCrypt) decryptFields(deleteFn func() error, fields ...*string) error
if strings.HasPrefix(*field, MagicPrefix) {
// If we have a magic prefix but encryption is disabled,
// we should delete the row.
return delete()
return delete("encryption disabled")
}
}
return nil
Expand All @@ -166,13 +176,19 @@ func (db *dbCrypt) decryptFields(deleteFn func() error, fields ...*string) error
continue
}
if len(*field) < len(MagicPrefix) || !strings.HasPrefix(*field, MagicPrefix) {
// We do not force encryption of unencrypted rows. This could be damaging
// to the deployment, and admins can always manually purge data.
continue
}

decrypted, err := cipher.Decrypt([]byte((*field)[len(MagicPrefix):]))
data, err := base64.StdEncoding.DecodeString((*field)[len(MagicPrefix):])
if err != nil {
// If it's not base64 with the prefix, we should delete the row.
return delete("stored value was not base64 encoded")
}
decrypted, err := cipher.Decrypt(data)
if err != nil {
// If the encryption key changed, we should delete the row.
return delete()
return delete("encryption key changed")
}
*field = string(decrypted)
}
Expand Down
8 changes: 7 additions & 1 deletion coderd/database/dbcrypt/dbcrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
"crypto/rand"
"database/sql"
"encoding/base64"
"io"
"sync/atomic"
"testing"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"
"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd/database"
Expand Down Expand Up @@ -172,7 +175,9 @@ func TestGitAuthLinks(t *testing.T) {
func requireEncryptedEquals(t *testing.T, cipher *atomic.Pointer[cryptorand.Cipher], value, expected string) {
t.Helper()
c := (*cipher.Load())
got, err := c.Decrypt([]byte(value[len(dbcrypt.MagicPrefix):]))
data, err := base64.StdEncoding.DecodeString(value[len(dbcrypt.MagicPrefix):])
require.NoError(t, err)
got, err := c.Decrypt(data)
require.NoError(t, err)
require.Equal(t, expected, string(got))
}
Expand All @@ -193,5 +198,6 @@ func setup(t *testing.T) (db, cryptodb database.Store, cipher *atomic.Pointer[cr
cipher = &atomic.Pointer[cryptorand.Cipher]{}
return rawDB, dbcrypt.New(rawDB, &dbcrypt.Options{
ExternalTokenCipher: cipher,
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug),
}), cipher
}
6 changes: 6 additions & 0 deletions coderd/httpmw/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ func ExtractAPIKey(rw http.ResponseWriter, r *http.Request, cfg ExtractAPIKeyCon
UserID: key.UserID,
LoginType: key.LoginType,
})
if errors.Is(err, sql.ErrNoRows) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if errors.Is(err, sql.ErrNoRows) {
if xerrors.Is(err, sql.ErrNoRows) {

return optionalWrite(http.StatusUnauthorized, codersdk.Response{
Message: SignedOutErrorMessage,
Detail: "You must re-authenticate with the login provider.",
})
}
if err != nil {
return write(http.StatusInternalServerError, codersdk.Response{
Message: "A database error occurred",
Expand Down
1 change: 1 addition & 0 deletions enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func New(ctx context.Context, options *Options) (*API, error) {
externalTokenCipher := &atomic.Pointer[cryptorand.Cipher]{}
options.Database = dbcrypt.New(options.Database, &dbcrypt.Options{
ExternalTokenCipher: externalTokenCipher,
Logger: options.Logger.Named("dbcrypt"),
})

api := &API{
Expand Down