update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations.

This commit is contained in:
Mechiel Lukkien
2023-05-22 14:40:36 +02:00
parent f6ed860ccb
commit e81930ba20
58 changed files with 1970 additions and 1035 deletions

View File

@ -61,13 +61,13 @@ type TLSReportRecord struct {
Report tlsrpt.Report
}
func database() (rdb *bstore.DB, rerr error) {
func database(ctx context.Context) (rdb *bstore.DB, rerr error) {
mutex.Lock()
defer mutex.Unlock()
if tlsrptDB == nil {
p := mox.DataDirPath("tlsrpt.db")
os.MkdirAll(filepath.Dir(p), 0770)
db, err := bstore.Open(p, &bstore.Options{Timeout: 5 * time.Second, Perm: 0660}, TLSReportRecord{})
db, err := bstore.Open(ctx, p, &bstore.Options{Timeout: 5 * time.Second, Perm: 0660}, TLSReportRecord{})
if err != nil {
return nil, err
}
@ -78,7 +78,7 @@ func database() (rdb *bstore.DB, rerr error) {
// Init opens and possibly initializes the database.
func Init() error {
_, err := database()
_, err := database(mox.Shutdown)
return err
}
@ -106,7 +106,7 @@ func Close() {
func AddReport(ctx context.Context, verifiedFromDomain dns.Domain, mailFrom string, r *tlsrpt.Report) error {
log := xlog.WithContext(ctx)
db, err := database()
db, err := database(ctx)
if err != nil {
return err
}
@ -149,39 +149,39 @@ func AddReport(ctx context.Context, verifiedFromDomain dns.Domain, mailFrom stri
}
}
record.Domain = reportdom.Name()
return db.Insert(&record)
return db.Insert(ctx, &record)
}
// Records returns all TLS reports in the database.
func Records(ctx context.Context) ([]TLSReportRecord, error) {
db, err := database()
db, err := database(ctx)
if err != nil {
return nil, err
}
return bstore.QueryDB[TLSReportRecord](db).List()
return bstore.QueryDB[TLSReportRecord](ctx, db).List()
}
// RecordID returns the report for the ID.
func RecordID(ctx context.Context, id int64) (TLSReportRecord, error) {
db, err := database()
db, err := database(ctx)
if err != nil {
return TLSReportRecord{}, err
}
e := TLSReportRecord{ID: id}
err = db.Get(&e)
err = db.Get(ctx, &e)
return e, err
}
// RecordsPeriodDomain returns the reports overlapping start and end, for the given
// domain. If domain is empty, all records match for domain.
func RecordsPeriodDomain(ctx context.Context, start, end time.Time, domain string) ([]TLSReportRecord, error) {
db, err := database()
db, err := database(ctx)
if err != nil {
return nil, err
}
q := bstore.QueryDB[TLSReportRecord](db)
q := bstore.QueryDB[TLSReportRecord](ctx, db)
if domain != "" {
q.FilterNonzero(TLSReportRecord{Domain: domain})
}

View File

@ -15,6 +15,8 @@ import (
"github.com/mjl-/mox/tlsrpt"
)
var ctxbg = context.Background()
const reportJSON = `{
"organization-name": "Company-X",
"date-range": {
@ -59,6 +61,8 @@ const reportJSON = `{
}`
func TestReport(t *testing.T) {
mox.Context = ctxbg
mox.Shutdown, mox.ShutdownCancel = context.WithCancel(ctxbg)
mox.ConfigStaticPath = "../testdata/tlsrpt/fake.conf"
mox.Conf.Static.DataDir = "."
// Recognize as configured domain.
@ -89,7 +93,7 @@ func TestReport(t *testing.T) {
if err != nil {
t.Fatalf("parsing TLSRPT from message %q: %s", file.Name(), err)
}
if err := AddReport(context.Background(), dns.Domain{ASCII: "mox.example"}, "tlsrpt@mox.example", report); err != nil {
if err := AddReport(ctxbg, dns.Domain{ASCII: "mox.example"}, "tlsrpt@mox.example", report); err != nil {
t.Fatalf("adding report to database: %s", err)
}
}
@ -97,11 +101,11 @@ func TestReport(t *testing.T) {
report, err := tlsrpt.Parse(strings.NewReader(reportJSON))
if err != nil {
t.Fatalf("parsing report: %v", err)
} else if err := AddReport(context.Background(), dns.Domain{ASCII: "company-y.example"}, "tlsrpt@company-y.example", report); err != nil {
} else if err := AddReport(ctxbg, dns.Domain{ASCII: "company-y.example"}, "tlsrpt@company-y.example", report); err != nil {
t.Fatalf("adding report to database: %s", err)
}
records, err := Records(context.Background())
records, err := Records(ctxbg)
if err != nil {
t.Fatalf("fetching records: %s", err)
}
@ -112,14 +116,14 @@ func TestReport(t *testing.T) {
if !reflect.DeepEqual(&r.Report, report) {
t.Fatalf("report, got %#v, expected %#v", r.Report, report)
}
if _, err := RecordID(context.Background(), r.ID); err != nil {
if _, err := RecordID(ctxbg, r.ID); err != nil {
t.Fatalf("get record by id: %v", err)
}
}
start, _ := time.Parse(time.RFC3339, "2016-04-01T00:00:00Z")
end, _ := time.Parse(time.RFC3339, "2016-04-01T23:59:59Z")
records, err = RecordsPeriodDomain(context.Background(), start, end, "test.xmox.nl")
records, err = RecordsPeriodDomain(ctxbg, start, end, "test.xmox.nl")
if err != nil || len(records) != 1 {
t.Fatalf("got err %v, records %#v, expected no error with 1 record", err, records)
}