-
Notifications
You must be signed in to change notification settings - Fork 1
/
audit.go
106 lines (88 loc) · 2.37 KB
/
audit.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
package cmd
import (
"fmt"
"path/filepath"
"strings"
"github.com/pvwnthem/gopwd/constants"
"github.com/pvwnthem/gopwd/pkg/audit"
"github.com/pvwnthem/gopwd/pkg/crypto"
"github.com/pvwnthem/gopwd/pkg/util"
"github.com/spf13/cobra"
)
var (
Hibp bool
)
var auditCommand = &cobra.Command{
Use: "audit",
Short: "Audit the vault for weak passwords",
RunE: func(cmd *cobra.Command, args []string) error {
vaultPath := Path
// Check if the vault exists
vaultExists, err := util.Exists(vaultPath)
if err != nil {
return fmt.Errorf(constants.ErrVaultExistence, err)
}
if !vaultExists {
return fmt.Errorf(constants.ErrVaultDoesNotExist, vaultPath)
}
// Iterate through the vault
dir, err := util.WalkDir(vaultPath)
if err != nil {
return fmt.Errorf("failed to walk directory: %w", err)
}
GPGID, err := util.GetGPGID(vaultPath)
if err != nil {
return fmt.Errorf(constants.ErrGetGPGID, err)
}
GPGModule := crypto.New(GPGID, crypto.Config{})
// array of passwords
passwords := make(map[string]string)
for _, d := range dir {
splitFilePath := strings.Split(d, "/")
if !strings.HasPrefix(splitFilePath[len(splitFilePath)-1], ".") {
passwordPath := filepath.Join(d)
password, err := util.ReadFile(passwordPath)
if err != nil {
return fmt.Errorf("failed to read password file: %w", err)
}
decrypted, err := GPGModule.Decrypt(password)
if err != nil {
return fmt.Errorf(constants.ErrPasswordDecryption, err)
}
// this is probably a bad way to do this
a := strings.Split(vaultPath, "/")
m := make(map[string]bool)
for _, item := range a {
m[item] = true
}
var diff []string
for _, item := range splitFilePath {
if _, ok := m[item]; !ok {
diff = append(diff, item)
}
}
passwords[strings.Join(diff[:len(diff)-1], "/")] = string(decrypted)
}
}
var provider *audit.Provider
if Hibp {
provider = audit.HibpProvider
} else {
provider = audit.DefaultProvider
}
auditor := audit.New(provider)
for k, v := range passwords {
secure, message := auditor.Process(v)
if secure {
fmt.Printf("%s: secure\n", k)
} else {
fmt.Printf("%s: insecure, %s\n", k, message)
}
}
return nil
},
}
func init() {
auditCommand.Flags().BoolVar(&Hibp, "hibp", false, "Check passwords against haveibeenpwned.com")
rootCmd.AddCommand(auditCommand)
}