-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
109 lines (95 loc) · 2.17 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/trufflesecurity/driftwood/pkg/exp/client"
"github.com/trufflesecurity/driftwood/pkg/exp/parser"
)
var version = "dev"
func main() {
app := &cli.App{
Name: "driftwood",
Usage: "Verify if a private key is used for important things.",
UsageText: "driftwood <path to key or '-' for stdin>",
Version: version,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "pretty-json",
},
&cli.BoolFlag{
Name: "debug",
},
},
Action: func(c *cli.Context) error {
args := c.Args().Slice()
logger := logrus.New()
logger.SetOutput(os.Stderr)
if c.Bool("json") {
logger.SetFormatter(&logrus.JSONFormatter{
DisableTimestamp: true,
})
} else {
logger.SetFormatter(&logrus.TextFormatter{
DisableTimestamp: true,
})
}
if c.Bool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
if len(args) == 0 {
cli.ShowAppHelpAndExit(c, 1)
return nil
}
file := args[0]
var privateKey []byte
var err error
if file == "-" {
buff := bytes.NewBuffer(nil)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
buff.WriteString(scanner.Text() + "\n")
}
privateKey = buff.Bytes()
} else {
privateKey, err = ioutil.ReadFile(args[0])
if err != nil {
logger.Fatalf("File cannot be read: %s", err)
return nil
}
}
publicKey, err := parser.PublicKey(privateKey)
if err != nil {
logger.Fatalf("Error computing public key: %s", err)
}
result, err := client.Lookup(version, publicKey)
if err != nil {
logger.Fatalf("Error looking up public key: %s", err)
}
if !c.Bool("pretty-json") {
out, err := json.Marshal(result)
if err != nil {
logger.Fatalf("Error marshalling result: %s", err)
}
fmt.Println(string(out))
} else {
out, err := json.MarshalIndent(result, "", "\t")
if err != nil {
logger.Fatalf("Error marshalling result: %s", err)
}
fmt.Println(string(out))
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}