New creates a database.Store wrapper that encrypts/decrypts values stored at rest in the database.
(ctx context.Context, db database.Store, ciphers ...Cipher)
| 34 | // New creates a database.Store wrapper that encrypts/decrypts values |
| 35 | // stored at rest in the database. |
| 36 | func New(ctx context.Context, db database.Store, ciphers ...Cipher) (database.Store, error) { |
| 37 | cm := make(map[string]Cipher) |
| 38 | for _, c := range ciphers { |
| 39 | cm[c.HexDigest()] = c |
| 40 | } |
| 41 | dbc := &dbCrypt{ |
| 42 | ciphers: cm, |
| 43 | Store: db, |
| 44 | } |
| 45 | if len(ciphers) > 0 { |
| 46 | dbc.primaryCipherDigest = ciphers[0].HexDigest() |
| 47 | } |
| 48 | // nolint: gocritic // This is allowed. |
| 49 | authCtx := dbauthz.AsSystemRestricted(ctx) |
| 50 | if err := dbc.ensureEncryptedWithRetry(authCtx); err != nil { |
| 51 | return nil, xerrors.Errorf("ensure encrypted database fields: %w", err) |
| 52 | } |
| 53 | return dbc, nil |
| 54 | } |
| 55 | |
| 56 | type dbCrypt struct { |
| 57 | // primaryCipherDigest is the digest of the primary cipher used for encrypting data. |