Movatterモバイル変換


[0]ホーム

URL:


dbcrypt

package
v2.23.0Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2025 License:AGPL-3.0Imports:14Imported by:0

Details

Repository

github.com/coder/coder

Links

Documentation

Overview

Package dbcrypt provides a database.Store wrapper that encrypts/decryptsvalues stored at rest in the database.

Encryption is done using Ciphers, which is an abstraction over a set ofencryption keys. Each key has a unique identifier, which is used touniquely identify the key whilst maintaining secrecy.

Currently, AES-256-GCM is the only implemented cipher mode.The Cipher is currently used to encrypt/decrypt the following fields:- database.UserLink.OAuthAccessToken- database.UserLink.OAuthRefreshToken- database.GitAuthLink.OAuthAccessToken- database.GitAuthLink.OAuthRefreshToken- database.DBCryptSentinelValue

Multiple ciphers can be provided to support key rotation. The primary cipheris used to encrypt and decrypt all data. Secondary ciphers are only usedfor decryption and, as a general rule, should only be active when rotatingkeys.

Encryption keys are stored in the database in the table `dbcrypt_keys`.The table has the following schema:

  • number: the key number. This is used to avoid conflicts when rotating keys.
  • created_at: the time the key was created.
  • active_key_digest: the SHA256 digest of the active key. If null, the key has been revoked.
  • revoked_key_digest: the SHA256 digest of the revoked key. If null, the key has not been revoked.
  • revoked_at: the time the key was revoked. If null, the key has not been revoked.
  • test: the encrypted value of the string "coder". This is used to ensure that the key is valid.

Encrypted fields are stored in the database as a base64-encoded string.Each encrypted column MUST have a corresponding _key_id column that is a foreign keyreference to `dbcrypt_keys.active_key_digest`. This ensures that a key cannot berevoked until all rows that use that key have been migrated to a new key.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

funcDecrypt

func Decrypt(ctxcontext.Context, logslog.Logger, sqlDB *sql.DB, ciphers []Cipher)error

Decrypt decrypts all user tokens and revokes all ciphers.

funcDelete

func Delete(ctxcontext.Context, logslog.Logger, sqlDB *sql.DB)error

Delete deletes all user tokens and revokes all ciphers.This is a destructive operation and should only be usedas a last resort, for example, if the database encryption key has beenlost.

funcNew

func New(ctxcontext.Context, dbdatabase.Store, ciphers ...Cipher) (database.Store,error)

New creates a database.Store wrapper that encrypts/decrypts valuesstored at rest in the database.

funcRotate

func Rotate(ctxcontext.Context, logslog.Logger, sqlDB *sql.DB, ciphers []Cipher)error

Rotate rotates the database encryption keys by re-encrypting all user tokenswith the first cipher and revoking all other ciphers.

Types

typeCipher

type Cipher interface {Encrypt([]byte) ([]byte,error)Decrypt([]byte) ([]byte,error)HexDigest()string}

funcNewCiphers

func NewCiphers(keys ...[]byte) ([]Cipher,error)

NewCiphers is a convenience function for creating multiple ciphers.It currently only supports AES-256-GCM.

typeDecryptFailedError

type DecryptFailedError struct {Innererror}

DecryptFailedError is returned when decryption fails.

func (*DecryptFailedError)Error

func (e *DecryptFailedError) Error()string

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp