Skip to content
This repository has been archived by the owner on Jan 21, 2025. It is now read-only.

Commit

Permalink
feat: periodically sync accounts to account server
Browse files Browse the repository at this point in the history
  • Loading branch information
siredmar committed Apr 20, 2023
1 parent 8da3850 commit d5d67c8
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"sync"
"time"

"github.com/edgefarm/vault-plugin-secrets-nats/pkg/stm"
"github.com/hashicorp/vault/sdk/framework"
Expand Down Expand Up @@ -53,8 +54,10 @@ func backend() *NatsBackend {
Secrets: []*framework.Secret{
// b.hashiCupsToken(),
},
BackendType: logical.TypeLogical,
Invalidate: b.invalidate,
BackendType: logical.TypeLogical,
Invalidate: b.invalidate,
WALRollbackMinAge: 30 * time.Second,
PeriodicFunc: b.periodicFunc,
}
return &b
}
Expand Down Expand Up @@ -178,3 +181,45 @@ func readOperation[T any](ctx context.Context, s logical.Storage, path string) (
Data: groupMap,
}, nil
}

func (b *NatsBackend) periodicFunc(ctx context.Context, sys *logical.Request) error {
b.Logger().Info("Periodic: starting periodic func for syncing accounts to nats")
operators, err := listOperatorIssues(ctx, sys.Storage)
if err != nil {
return err
}
for _, operator := range operators {
operatorIssue, err := readOperatorIssue(ctx, sys.Storage, IssueOperatorParameters{
Operator: operator,
})
if err != nil {
return err
}
if operatorIssue != nil {
if !operatorIssue.SyncAccountServer {
b.Logger().Info(fmt.Sprintf("Periodic: operator %s not configured for auto syncing to account server", operator))
}
b.Logger().Debug(fmt.Sprintf("Periodic: operator %s selected for auto sync to account server", operator))
accountNames, err := listAccountIssues(ctx, sys.Storage, operator)
if err != nil {
return err
}
for _, account := range accountNames {
b.Logger().Debug(fmt.Sprintf("Periodic: account %s in operator %s syncing to acount server", account, operator))
accountIssue, err := readAccountIssue(ctx, sys.Storage, IssueAccountParameters{
Operator: operator,
Account: account,
})
if err != nil {
return err
}
err = refreshAccountResolver(ctx, sys.Storage, accountIssue, "push")
if err != nil {
return err
}
}
}

}
return nil
}

0 comments on commit d5d67c8

Please sign in to comment.