Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contacts Refactor #1127

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 92 additions & 103 deletions cmd/gdsutil/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"bytes"
"context"
"crypto/aes"
Expand All @@ -26,7 +27,6 @@ import (
"github.com/trisacrypto/directory/pkg"
profiles "github.com/trisacrypto/directory/pkg/gds/client"
"github.com/trisacrypto/directory/pkg/gds/config"
"github.com/trisacrypto/directory/pkg/gds/secrets"
"github.com/trisacrypto/directory/pkg/models/v1"
"github.com/trisacrypto/directory/pkg/store"
storerr "github.com/trisacrypto/directory/pkg/store/errors"
Expand Down Expand Up @@ -154,38 +154,43 @@ func main() {
Flags: []cli.Flag{},
},
{
Name: "contact:migrate",
Usage: "migrate all contacts on vasps into the model contacts namespace",
Category: "contact",
Action: migrateContacts,
Name: "emails:migrate",
Usage: "migrate all contacts and email logs on vasps into the emails namespace",
Category: "emails",
Action: migrateEmails,
Before: connectDB,
After: closeDB,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dryrun",
Aliases: []string{"d"},
Usage: "print migration results without modifying the database, used for testing",
Value: true,
},
&cli.BoolFlag{
Name: "compare",
Aliases: []string{"c"},
Usage: "if the contact exists, compare to the vasp contact record",
},
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "do not prompt to confirm operation",
},
},
},
{
Name: "contact:fixverifytoken",
Usage: "fixes any unverified contacts that do not have verification tokens",
Category: "contact",
Action: fixVerifyToken,
Before: connectDB,
After: closeDB,
Name: "emails:detail",
Usage: "get contact information for the specified email address",
UsageText: "email [email ...]",
Category: "emails",
Action: emailsDetail,
Before: connectDB,
After: closeDB,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dryrun",
Aliases: []string{"d"},
Usage: "print migration results without modifying the database, used for testing",
Name: "no-logs",
Aliases: []string{"l"},
Usage: "omit fetching email logs for the contact",
},
},
},
Expand Down Expand Up @@ -532,9 +537,14 @@ func vaspDetail(c *cli.Context) (err error) {
// Contact Functions
//===========================================================================

func migrateContacts(c *cli.Context) (err error) {
func migrateEmails(c *cli.Context) (err error) {
dryrun := c.Bool("dryrun")
compare := c.Bool("compare")
force := c.Bool("force")

if !force && !askForConfirmation("continue with migration?") {
return cli.Exit("operation canceled by user", 0)
}

// Iterate through all vasps in the database
vasps := db.ListVASPs(context.Background())
Expand All @@ -545,11 +555,13 @@ func migrateContacts(c *cli.Context) (err error) {
return cli.Exit(err, 1)
}

// Iterate through all contacts on the vasp
contacts := models.NewContactIterator(vasp.Contacts, models.SkipNoEmail())
for contacts.Next() {
vaspContact, _ := contacts.Value()
vaspContacts := []*pb.Contact{
vasp.Contacts.Technical, vasp.Contacts.Administrative,
vasp.Contacts.Legal, vasp.Contacts.Billing,
}

// Iterate through all contacts on the vasp
for _, vaspContact := range vaspContacts {
var contact *models.Contact
if contact, err = db.RetrieveContact(context.Background(), vaspContact.Email); err != nil {
if errors.Is(err, storerr.ErrEntityNotFound) {
Expand Down Expand Up @@ -667,63 +679,18 @@ func migrateContacts(c *cli.Context) (err error) {
return nil
}

func fixVerifyToken(c *cli.Context) (err error) {
dryrun := c.Bool("dryrun")
func emailsDetail(c *cli.Context) (err error) {
if c.NArg() == 0 {
return cli.Exit("specify an email address to fetch details for", 1)
}

vasps := db.ListVASPs(context.Background())
defer vasps.Release()
for vasps.Next() {
var vasp *pb.VASP
if vasp, err = vasps.VASP(); err != nil {
for i := 0; i < c.NArg(); i++ {
email := c.Args().Get(i)
contact, err := db.RetrieveContact(context.Background(), email)
if err != nil {
return cli.Exit(err, 1)
}

contacts := models.NewContactIterator(vasp.Contacts, models.SkipNoEmail(), models.SkipDuplicates())
for contacts.Next() {
vaspContact, kind := contacts.Value()

var contact *models.Contact
if contact, err = db.RetrieveContact(context.Background(), vaspContact.Email); err != nil {
return cli.Exit(err, 1)
}

if !contact.Verified && contact.Token == "" {
fmt.Printf("contact %s is not verified and has no verification token\n", contact.Email)

if !dryrun {
contact.Token = secrets.CreateToken(models.VerificationTokenLength)
if err = db.UpdateContact(context.Background(), contact); err != nil {
return cli.Exit(err, 1)
}
}
}

// Ensure the vaspContact matches the contact
token, verified, err := models.GetContactVerification(vaspContact)
if err != nil {
return cli.Exit(err, 1)
}

if contact.Verified != verified || contact.Token != token {
vaspName, _ := vasp.Name()
fmt.Printf("vasp %s contact %s (%s) does not match contact record\n", vaspName, kind, vaspContact.Email)

if !dryrun {
if err = models.SetContactVerification(vaspContact, contact.Token, contact.Verified); err != nil {
return cli.Exit(err, 1)
}

if err = db.UpdateVASP(context.Background(), vasp); err != nil {
return cli.Exit(err, 1)
}
}
}

}
}

if err = vasps.Error(); err != nil {
return cli.Exit(err, 1)
printJSON(contact)
}

return nil
Expand Down Expand Up @@ -845,6 +812,57 @@ func loadProfile(c *cli.Context) (err error) {
return nil
}

func connectDB(c *cli.Context) (err error) {
// Suppress the zerolog output from the store.
logger.Discard()

if conf, err = config.New(); err != nil {
return cli.Exit(err, 1)
}
conf.Database.ReindexOnBoot = false
conf.ConsoleLog = false

if db, err = store.Open(conf.Database); err != nil {
if serr, ok := status.FromError(err); ok {
return cli.Exit(fmt.Errorf("could not open store: %s", serr.Message()), 1)
}
return cli.Exit(err, 1)
}

return nil
}

func closeDB(c *cli.Context) (err error) {
if db != nil {
if err = db.Close(); err != nil {
return cli.Exit(err, 2)
}
}
return nil
}

func askForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)

for {
fmt.Printf("%s [y/n]: ", s)

response, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintf(os.Stderr, "could not interpret response: %s", err)
os.Exit(1)
}

response = strings.ToLower(strings.TrimSpace(response))

if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
}
}

func printJSON(v interface{}) (err error) {
if m, ok := v.(protoreflect.ProtoMessage); ok {
return printJSONPB(m)
Expand Down Expand Up @@ -895,32 +913,3 @@ func printJSONPBList(msgs []protoreflect.ProtoMessage) (err error) {
}
return nil
}

func connectDB(c *cli.Context) (err error) {
// Suppress the zerolog output from the store.
logger.Discard()

if conf, err = config.New(); err != nil {
return cli.Exit(err, 1)
}
conf.Database.ReindexOnBoot = false
conf.ConsoleLog = false

if db, err = store.Open(conf.Database); err != nil {
if serr, ok := status.FromError(err); ok {
return cli.Exit(fmt.Errorf("could not open store: %s", serr.Message()), 1)
}
return cli.Exit(err, 1)
}

return nil
}

func closeDB(c *cli.Context) (err error) {
if db != nil {
if err = db.Close(); err != nil {
return cli.Exit(err, 2)
}
}
return nil
}
17 changes: 14 additions & 3 deletions cmd/reissuer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ func notify(c *cli.Context) (err error) {
var (
nsent int
vasp *pb.VASP
contacts *models.Contacts
emailer *emails.EmailManager
reissueDate time.Time
)
Expand All @@ -333,7 +334,7 @@ func notify(c *cli.Context) (err error) {
ctx, cancel := utils.WithDeadline(context.Background())
defer cancel()

// Step 1: Fetch the VASP record
// Step 1a: Fetch the VASP record
if vasp, err = db.RetrieveVASP(ctx, vaspID); err != nil {
return cli.Exit(fmt.Errorf("could not find VASP record: %s", err), 1)
}
Expand All @@ -346,6 +347,11 @@ func notify(c *cli.Context) (err error) {
}
}

// Step 1b: Fetch contacts for the VASP record
if contacts, err = db.VASPContacts(ctx, vasp); err != nil {
return cli.Exit(fmt.Errorf("could not retrieve VASP contacts: %w", err), 1)
}

// Step 2: Parse reissuance date or get date 1 week from today
if reissueDate, err = time.Parse(dateFmt, c.String("reissuance-date")); err != nil {
return cli.Exit(err, 1)
Expand All @@ -357,7 +363,7 @@ func notify(c *cli.Context) (err error) {
}

// Send reissuance reminder emails
if nsent, err = emailer.SendReissuanceReminder(vasp, reissueDate); err != nil {
if nsent, err = emailer.SendReissuanceReminder(vasp, contacts, reissueDate); err != nil {
return cli.Exit(err, 1)
}

Expand All @@ -368,6 +374,7 @@ func notify(c *cli.Context) (err error) {
func reissueCerts(c *cli.Context) (err error) {
var (
vasp *pb.VASP
contacts *models.Contacts
certreq *models.CertificateRequest
pkcs12password string
emailer *emails.EmailManager
Expand All @@ -386,6 +393,10 @@ func reissueCerts(c *cli.Context) (err error) {
return cli.Exit(fmt.Errorf("could not find VASP record: %s", err), 1)
}

if contacts, err = db.VASPContacts(ctx, vasp); err != nil {
return cli.Exit(fmt.Errorf("could not get VASP contacts: %s", err), 1)
}

// Check with the user if we should continue with the certificate reissuance
fmt.Printf("reissuing certs for %s\n", vasp.CommonName)
if !c.Bool("yes") {
Expand Down Expand Up @@ -462,7 +473,7 @@ func reissueCerts(c *cli.Context) (err error) {
}

// Send the notification email that certificate reissuance is forthcoming and provide whisper link to the PKCS12 password.
if nsent, err = emailer.SendReissuanceStarted(vasp, whisperLink); err != nil {
if nsent, err = emailer.SendReissuanceStarted(vasp, contacts, whisperLink); err != nil {
return cli.Exit(err, 1)
}

Expand Down
3 changes: 3 additions & 0 deletions fixtures/datagen/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 88
max-complexity = 10
Loading
Loading