SMTP-Credentials und E_Mail-Adressen verschlüsselt abgelegt
This commit is contained in:
1 parent
781aa20253
commit
66659d9a23
32 files changed
+3276
-132
No files matched your search
+132
-24
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lektorenplan/domain"
|
||||
)
|
||||
@@ -151,35 +152,68 @@ func (s *Store) DeleteChurch(ctx context.Context, tenantID, id string) error {
|
||||
// People lists a tenant's volunteers.
|
||||
func (s *Store) People(ctx context.Context, tenantID string) ([]domain.Person, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT id, first_name, last_name, roles, email, phone, preferred_church_ids,
|
||||
SELECT id, first_name, last_name, roles, email, preferred_church_ids,
|
||||
preferred_weekdays, preferred_service_titles, max_per_month, active, notes
|
||||
FROM people WHERE tenant_id=? ORDER BY last_name COLLATE NOCASE, first_name COLLATE NOCASE`, tenantID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []domain.Person
|
||||
for rows.Next() {
|
||||
person, err := scanPerson(rows)
|
||||
if err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, *person)
|
||||
}
|
||||
return out, rows.Err()
|
||||
if err := rows.Err(); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
rows.Close()
|
||||
// The connection pool has a single connection, so the rows must be closed
|
||||
// before the encrypted contacts are loaded.
|
||||
for index := range out {
|
||||
s.hydratePersonContacts(ctx, tenantID, &out[index])
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Person loads one volunteer.
|
||||
func (s *Store) Person(ctx context.Context, tenantID, id string) (*domain.Person, error) {
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
SELECT id, first_name, last_name, roles, email, phone, preferred_church_ids,
|
||||
SELECT id, first_name, last_name, roles, email, preferred_church_ids,
|
||||
preferred_weekdays, preferred_service_titles, max_per_month, active, notes
|
||||
FROM people WHERE tenant_id=? AND id=?`, tenantID, id)
|
||||
person, err := scanPerson(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return person, err
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.hydratePersonContacts(ctx, tenantID, person)
|
||||
return person, nil
|
||||
}
|
||||
|
||||
// hydratePersonContacts fills the e-mail address from the encrypted store.
|
||||
// Values that exist but cannot be decrypted stay empty and are marked as
|
||||
// locked so the UI can explain the situation. Before a tenant has been
|
||||
// migrated, plaintext contacts are only exposed to key-holding editors.
|
||||
func (s *Store) hydratePersonContacts(ctx context.Context, tenantID string, person *domain.Person) {
|
||||
if !s.SecretIsInitialized(ctx, tenantID) {
|
||||
if KeyringFrom(ctx) == nil && strings.TrimSpace(person.Email) != "" {
|
||||
person.Email = ""
|
||||
person.EmailLocked = true
|
||||
}
|
||||
return
|
||||
}
|
||||
email, emailLocked := s.PersonContactSecrets(ctx, tenantID, person.ID)
|
||||
if person.Email == "" {
|
||||
person.Email = email
|
||||
person.EmailLocked = emailLocked
|
||||
}
|
||||
}
|
||||
|
||||
func scanPerson(row rowScanner) (*domain.Person, error) {
|
||||
@@ -187,7 +221,7 @@ func scanPerson(row rowScanner) (*domain.Person, error) {
|
||||
var roles, churches, weekdays, titles string
|
||||
var active int
|
||||
if err := row.Scan(&person.ID, &person.FirstName, &person.LastName, &roles,
|
||||
&person.Email, &person.Phone, &churches, &weekdays, &titles,
|
||||
&person.Email, &churches, &weekdays, &titles,
|
||||
&person.MaxPerMonth, &active, &person.Notes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -221,24 +255,88 @@ func (s *Store) UpsertPerson(ctx context.Context, tenantID string, person domain
|
||||
if person.MaxPerMonth < 0 {
|
||||
person.MaxPerMonth = 0
|
||||
}
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
INSERT INTO people(tenant_id, id, first_name, last_name, roles, email, phone,
|
||||
preferred_church_ids, preferred_weekdays, preferred_service_titles,
|
||||
max_per_month, active, notes)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(tenant_id, id) DO UPDATE SET
|
||||
first_name=excluded.first_name, last_name=excluded.last_name, roles=excluded.roles,
|
||||
email=excluded.email, phone=excluded.phone,
|
||||
preferred_church_ids=excluded.preferred_church_ids,
|
||||
preferred_weekdays=excluded.preferred_weekdays,
|
||||
preferred_service_titles=excluded.preferred_service_titles,
|
||||
max_per_month=excluded.max_per_month, active=excluded.active, notes=excluded.notes`,
|
||||
tenantID, person.ID, person.FirstName, person.LastName, encodeStrings(roleStrings),
|
||||
person.Email, person.Phone, encodeStrings(person.PreferredChurchIDs),
|
||||
encodeInts(person.PreferredWeekdays),
|
||||
encodeStrings(person.PreferredServiceTitles), person.MaxPerMonth,
|
||||
boolInt(person.Active), person.Notes)
|
||||
return err
|
||||
initialized := s.SecretIsInitialized(ctx, tenantID)
|
||||
keyring := KeyringFrom(ctx)
|
||||
// Plaintext contact writes are only a migration fallback for callers
|
||||
// without key material. A reader who is logged in never stores an address
|
||||
// in the legacy column.
|
||||
manageContacts := !initialized && keyring == nil
|
||||
// A locked field that was left empty must keep its existing value. This
|
||||
// also lets readers without their own data key update their profile and
|
||||
// prevents an administrator who cannot see plaintext contacts from
|
||||
// clearing them before the tenant has been migrated.
|
||||
preserveContact := person.EmailLocked && strings.TrimSpace(person.Email) == ""
|
||||
updateLegacyContact := manageContacts && !preserveContact
|
||||
var dek []byte
|
||||
if initialized && keyring != nil && !preserveContact {
|
||||
if err := s.EnsurePersonScope(ctx, keyring, tenantID, person.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
var err error
|
||||
dek, err = s.scopeDEK(ctx, keyring, KeyScope{Type: ScopePerson, TenantID: tenantID, ID: person.ID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return s.transaction(ctx, func(tx *sql.Tx) error {
|
||||
email := person.Email
|
||||
if initialized || !manageContacts {
|
||||
// Contact details only live in the encrypted secrets table.
|
||||
email = ""
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO people(tenant_id, id, first_name, last_name, roles, email,
|
||||
preferred_church_ids, preferred_weekdays, preferred_service_titles,
|
||||
max_per_month, active, notes)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(tenant_id, id) DO UPDATE SET
|
||||
first_name=excluded.first_name, last_name=excluded.last_name, roles=excluded.roles,
|
||||
email=CASE WHEN ? THEN excluded.email ELSE people.email END,
|
||||
preferred_church_ids=excluded.preferred_church_ids,
|
||||
preferred_weekdays=excluded.preferred_weekdays,
|
||||
preferred_service_titles=excluded.preferred_service_titles,
|
||||
max_per_month=excluded.max_per_month, active=excluded.active, notes=excluded.notes`,
|
||||
tenantID, person.ID, person.FirstName, person.LastName, encodeStrings(roleStrings),
|
||||
email, encodeStrings(person.PreferredChurchIDs),
|
||||
encodeInts(person.PreferredWeekdays),
|
||||
encodeStrings(person.PreferredServiceTitles), person.MaxPerMonth,
|
||||
boolInt(person.Active), person.Notes, boolInt(updateLegacyContact))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !initialized || keyring == nil {
|
||||
return nil
|
||||
}
|
||||
setSecret := func(key, value string) error {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
DELETE FROM secrets
|
||||
WHERE tenant_id=? AND scope_type=? AND scope_id=? AND secret_key=?`,
|
||||
tenantID, ScopePerson, person.ID, key)
|
||||
return err
|
||||
}
|
||||
sealed, err := sealAESGCM(dek, []byte(value),
|
||||
keyAAD(ScopePerson, tenantID, person.ID, key))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `
|
||||
INSERT INTO secrets(tenant_id, scope_type, scope_id, secret_key, ciphertext, updated_at)
|
||||
VALUES(?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(tenant_id, scope_type, scope_id, secret_key) DO UPDATE SET
|
||||
ciphertext=excluded.ciphertext, updated_at=excluded.updated_at`,
|
||||
tenantID, ScopePerson, person.ID, key, sealed,
|
||||
time.Now().UTC().Format(time.RFC3339))
|
||||
return err
|
||||
}
|
||||
// A locked field that was left empty must keep its existing secret.
|
||||
if !(person.EmailLocked && strings.TrimSpace(person.Email) == "") {
|
||||
if err := setSecret("email", person.Email); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// DeletePerson removes a volunteer with assignments and absences.
|
||||
@@ -252,6 +350,16 @@ func (s *Store) DeletePerson(ctx context.Context, tenantID, id string) error {
|
||||
"DELETE FROM absences WHERE tenant_id=? AND person_id=?", tenantID, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
"DELETE FROM secrets WHERE tenant_id=? AND scope_type=? AND scope_id=?",
|
||||
tenantID, ScopePerson, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
"DELETE FROM key_wrappers WHERE scope_type=? AND tenant_id=? AND scope_id=?",
|
||||
ScopePerson, tenantID, id); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, "DELETE FROM people WHERE tenant_id=? AND id=?", tenantID, id)
|
||||
return err
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user