-
Notifications
You must be signed in to change notification settings - Fork 159
/
migration_v16.go
48 lines (38 loc) · 1.53 KB
/
migration_v16.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package db
import (
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/storage/ast"
"github.com/openziti/storage/boltz"
)
func (m *Migrations) removeOrphanedOttCaEnrollments(step *boltz.MigrationStep) {
var enrollmentsToDelete []string
for cursor := m.stores.Enrollment.IterateIds(step.Ctx.Tx(), ast.BoolNodeTrue); cursor.IsValid(); cursor.Next() {
current := cursor.Current()
currentEnrollmentId := string(current)
enrollment, err := m.stores.Enrollment.LoadOneById(step.Ctx.Tx(), currentEnrollmentId)
if err != nil {
step.SetError(fmt.Errorf("error interating ids of enrollments, enrollment [%s]: %v", currentEnrollmentId, err))
return
}
if enrollment.CaId != nil && *enrollment.CaId != "" {
_, err := m.stores.Ca.LoadOneById(step.Ctx.Tx(), *enrollment.CaId)
if err != nil && boltz.IsErrNotFoundErr(err) {
enrollmentsToDelete = append(enrollmentsToDelete, currentEnrollmentId)
}
}
}
//clear caIds that are invalid via CheckIntegrity
err := m.stores.Enrollment.CheckIntegrity(step.Ctx, true, func(err error, fixed bool) {
if !fixed {
pfxlog.Logger().Errorf("unfixable error during orphaned ottca enrollment integrity check: %v", err)
}
})
step.SetError(err)
for _, enrollmentId := range enrollmentsToDelete {
pfxlog.Logger().Infof("removing invalid ottca enrollment [%s]", enrollmentId)
if err := m.stores.Enrollment.DeleteById(step.Ctx, enrollmentId); err != nil {
step.SetError(fmt.Errorf("could not delete enrollment [%s] with invalid CA reference: %v", enrollmentId, err))
}
}
}