From cf0e4a8b85d69956641d5c0aa6eafe95a7b598a3 Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 23 Jul 2021 16:54:53 -0700 Subject: [PATCH] add key trust-all long overdue --- cmd/signaldctl/cmd/key/list/list-keys.go | 12 +- cmd/signaldctl/cmd/key/root.go | 2 + cmd/signaldctl/cmd/key/trust_all/trust-all.go | 148 ++++++++++++++++++ cmd/signaldctl/common/signald.go | 1 + 4 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 cmd/signaldctl/cmd/key/trust_all/trust-all.go diff --git a/cmd/signaldctl/cmd/key/list/list-keys.go b/cmd/signaldctl/cmd/key/list/list-keys.go index 30f5104..b20ca26 100644 --- a/cmd/signaldctl/cmd/key/list/list-keys.go +++ b/cmd/signaldctl/cmd/key/list/list-keys.go @@ -67,19 +67,19 @@ func getOne() { req := v1.GetIdentitiesRequest{Account: account, Address: address} resp, err := req.Submit(common.Signald) if err != nil { - log.Fatal(err, "error communicating with signald") + log.Fatal(err) } switch common.OutputFormat { case common.OutputFormatJSON: err := json.NewEncoder(os.Stdout).Encode(resp) if err != nil { - log.Fatal(err, "error encoding response to stdout") + log.Fatal("error encoding response to stdout:", err) } case common.OutputFormatYAML: err := yaml.NewEncoder(os.Stdout).Encode(resp) if err != nil { - log.Fatal(err, "error encoding response to stdout") + log.Fatal("error encoding response to stdout:", err) } case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault: t := table.NewWriter() @@ -103,19 +103,19 @@ func getAll() { req := v1.GetAllIdentities{Account: account} resp, err := req.Submit(common.Signald) if err != nil { - log.Fatal(err, "error communicating with signald") + log.Fatal(err) } switch common.OutputFormat { case common.OutputFormatJSON: err := json.NewEncoder(os.Stdout).Encode(resp) if err != nil { - log.Fatal(err, "error encoding response to stdout") + log.Fatal("error encoding response to stdout:", err) } case common.OutputFormatYAML: err := yaml.NewEncoder(os.Stdout).Encode(resp) if err != nil { - log.Fatal(err, "error encoding response to stdout") + log.Fatal("error encoding response to stdout:", err) } case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault: t := table.NewWriter() diff --git a/cmd/signaldctl/cmd/key/root.go b/cmd/signaldctl/cmd/key/root.go index f18109a..287b664 100644 --- a/cmd/signaldctl/cmd/key/root.go +++ b/cmd/signaldctl/cmd/key/root.go @@ -21,6 +21,7 @@ import ( "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/key/list" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/key/qr" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/key/trust" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/key/trust_all" ) var KeyCmd = &cobra.Command{ @@ -32,4 +33,5 @@ func init() { KeyCmd.AddCommand(list.ListKeysCmd) KeyCmd.AddCommand(qr.QRKeyCmd) KeyCmd.AddCommand(trust.TrustKeyCmd) + KeyCmd.AddCommand(trust_all.TrustAllCmd) } diff --git a/cmd/signaldctl/cmd/key/trust_all/trust-all.go b/cmd/signaldctl/cmd/key/trust_all/trust-all.go new file mode 100644 index 0000000..7619f01 --- /dev/null +++ b/cmd/signaldctl/cmd/key/trust_all/trust-all.go @@ -0,0 +1,148 @@ +// Copyright © 2021 Finn Herzfeld +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package trust_all + +import ( + "encoding/json" + "log" + "os" + + "github.com/jedib0t/go-pretty/v6/table" + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" + + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/cmd/signaldctl/config" + "gitlab.com/signald/signald-go/signald/client-protocol/v1" +) + +var ( + account string + addresses []v1.JsonAddress + TrustAllCmd = &cobra.Command{ + Use: "trust-all []", + Short: "mark all keys as trusted, optionally limiting by phone number or UUID", + PreRun: func(cmd *cobra.Command, args []string) { + if account == "" { + account = config.Config.DefaultAccount + } + if account == "" { + common.Must(cmd.Help()) + log.Fatal("No account specified. Please specify with --account or set a default") + } + for _, address := range args { + addresses = append(addresses, common.StringToAddress(address)) + } + }, + Run: func(_ *cobra.Command, _ []string) { + go common.Signald.Listen(nil) + changed := []v1.IdentityKeyList{} + if len(addresses) > 0 { + for _, address := range addresses { + identitiesReq := v1.GetIdentitiesRequest{Account: account, Address: &address} + identities, err := identitiesReq.Submit(common.Signald) + if err != nil { + log.Fatal(err) + } + + changedIdentities := []*v1.IdentityKey{} + + for _, identityKey := range identities.Identities { + if identityKey.TrustLevel == "UNTRUSTED" { + trustReq := v1.TrustRequest{Account: account, Address: &address, SafetyNumber: identityKey.SafetyNumber} + err = trustReq.Submit(common.Signald) + if err != nil { + log.Fatal(err) + } + changedIdentities = append(changedIdentities, identityKey) + } + } + if len(changedIdentities) > 0 { + changed = append(changed, v1.IdentityKeyList{ + Address: &address, + Identities: changedIdentities, + }) + } + } + } else { + identitiesReq := v1.GetAllIdentities{Account: account} + resp, err := identitiesReq.Submit(common.Signald) + if err != nil { + log.Fatal(err) + } + for _, user := range resp.IdentityKeys { + changedIdentities := []*v1.IdentityKey{} + for _, identityKey := range user.Identities { + if identityKey.TrustLevel == "UNTRUSTED" { + trustReq := v1.TrustRequest{ + Account: account, + Address: user.Address, + SafetyNumber: identityKey.SafetyNumber, + TrustLevel: "TRUST_UNVERIFIED", + } + err = trustReq.Submit(common.Signald) + if err != nil { + log.Fatal(err) + } + changedIdentities = append(changedIdentities, identityKey) + } + } + if len(changedIdentities) > 0 { + changed = append(changed, v1.IdentityKeyList{ + Address: user.Address, + Identities: changedIdentities, + }) + } + } + } + switch common.OutputFormat { + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(changed) + if err != nil { + log.Fatal("error encoding response to stdout:", err) + } + case common.OutputFormatYAML: + err := yaml.NewEncoder(os.Stdout).Encode(changed) + if err != nil { + log.Fatal("error encoding response to stdout:", err) + } + case common.OutputFormatCSV, common.OutputFormatTable: + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + t.AppendHeader(table.Row{"Number", "UUID", "Safety Number"}) + for _, user := range changed { + for _, identity := range user.Identities { + t.AppendRow(table.Row{user.Address.Number, user.Address.UUID, identity.SafetyNumber}) + } + } + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + common.StylizeTable(t) + t.Render() + } + case common.OutputFormatQuiet, common.OutputFormatDefault: + return + default: + log.Fatal("Unsupported output format") + } + }, + } +) + +func init() { + TrustAllCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use") +} diff --git a/cmd/signaldctl/common/signald.go b/cmd/signaldctl/common/signald.go index f47b075..8ffc145 100644 --- a/cmd/signaldctl/common/signald.go +++ b/cmd/signaldctl/common/signald.go @@ -19,6 +19,7 @@ const ( OutputFormatQR = "qr" OutputFormatMarkdown = "md" OutputFormatMan = "man" + OutputFormatQuiet = "quiet" AnnotationNoSocketConnection = "no-socket" )