add key trust-all

long overdue
This commit is contained in:
Finn 2021-07-23 16:54:53 -07:00
parent fcae0fc903
commit cf0e4a8b85
4 changed files with 157 additions and 6 deletions

View file

@ -67,19 +67,19 @@ func getOne() {
req := v1.GetIdentitiesRequest{Account: account, Address: address} req := v1.GetIdentitiesRequest{Account: account, Address: address}
resp, err := req.Submit(common.Signald) resp, err := req.Submit(common.Signald)
if err != nil { if err != nil {
log.Fatal(err, "error communicating with signald") log.Fatal(err)
} }
switch common.OutputFormat { switch common.OutputFormat {
case common.OutputFormatJSON: case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(resp) err := json.NewEncoder(os.Stdout).Encode(resp)
if err != nil { if err != nil {
log.Fatal(err, "error encoding response to stdout") log.Fatal("error encoding response to stdout:", err)
} }
case common.OutputFormatYAML: case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(resp) err := yaml.NewEncoder(os.Stdout).Encode(resp)
if err != nil { 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: case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault:
t := table.NewWriter() t := table.NewWriter()
@ -103,19 +103,19 @@ func getAll() {
req := v1.GetAllIdentities{Account: account} req := v1.GetAllIdentities{Account: account}
resp, err := req.Submit(common.Signald) resp, err := req.Submit(common.Signald)
if err != nil { if err != nil {
log.Fatal(err, "error communicating with signald") log.Fatal(err)
} }
switch common.OutputFormat { switch common.OutputFormat {
case common.OutputFormatJSON: case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(resp) err := json.NewEncoder(os.Stdout).Encode(resp)
if err != nil { if err != nil {
log.Fatal(err, "error encoding response to stdout") log.Fatal("error encoding response to stdout:", err)
} }
case common.OutputFormatYAML: case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(resp) err := yaml.NewEncoder(os.Stdout).Encode(resp)
if err != nil { 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: case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault:
t := table.NewWriter() t := table.NewWriter()

View file

@ -21,6 +21,7 @@ import (
"gitlab.com/signald/signald-go/cmd/signaldctl/cmd/key/list" "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/qr"
"gitlab.com/signald/signald-go/cmd/signaldctl/cmd/key/trust" "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{ var KeyCmd = &cobra.Command{
@ -32,4 +33,5 @@ func init() {
KeyCmd.AddCommand(list.ListKeysCmd) KeyCmd.AddCommand(list.ListKeysCmd)
KeyCmd.AddCommand(qr.QRKeyCmd) KeyCmd.AddCommand(qr.QRKeyCmd)
KeyCmd.AddCommand(trust.TrustKeyCmd) KeyCmd.AddCommand(trust.TrustKeyCmd)
KeyCmd.AddCommand(trust_all.TrustAllCmd)
} }

View file

@ -0,0 +1,148 @@
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
//
// 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 <http://www.gnu.org/licenses/>.
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 [<phone number or UUID>]",
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")
}

View file

@ -19,6 +19,7 @@ const (
OutputFormatQR = "qr" OutputFormatQR = "qr"
OutputFormatMarkdown = "md" OutputFormatMarkdown = "md"
OutputFormatMan = "man" OutputFormatMan = "man"
OutputFormatQuiet = "quiet"
AnnotationNoSocketConnection = "no-socket" AnnotationNoSocketConnection = "no-socket"
) )