-
Notifications
You must be signed in to change notification settings - Fork 263
/
password.go
79 lines (71 loc) · 2.06 KB
/
password.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
package db_local
import (
"encoding/json"
"os"
"strings"
"github.com/google/uuid"
filehelpers "github.com/turbot/go-kit/files"
"github.com/turbot/steampipe/pkg/utils"
)
// Passwords :: structure for working with DB passwords
type Passwords struct {
Root string
Steampipe string
}
func writePasswordFile(password string) error {
return os.WriteFile(getPasswordFileLocation(), []byte(password), 0600)
}
// readPasswordFile reads the password file and returns it contents.
// the the password file could not be found, then it generates a new
// password and writes it to the password file, before returning it
func readPasswordFile() (string, error) {
if !filehelpers.FileExists(getPasswordFileLocation()) {
p := generatePassword()
if err := writePasswordFile(p); err != nil {
return "", err
}
return p, nil
}
contentBytes, err := os.ReadFile(getPasswordFileLocation())
if err != nil {
return "", err
}
return strings.TrimSpace(string(contentBytes)), nil
}
func generatePassword() string {
// Create a simple, random password of the form f9fe-442f-90fb
// Simple to read / write, and has a strength rating of 4 per https://lowe.github.io/tryzxcvbn/
// Yes, this UUIDv4 does always include a 4, but good enough for our needs.
u, err := uuid.NewRandom()
if err != nil {
// Should never happen?
panic(err)
}
s := u.String()
return strings.ReplaceAll(s[9:23], "-", "_")
}
func migrateLegacyPasswordFile() error {
utils.LogTime("db_local.migrateLegacyPasswordFile start")
defer utils.LogTime("db_local.migrateLegacyPasswordFile end")
if filehelpers.FileExists(getLegacyPasswordFileLocation()) {
p, err := getLegacyPasswords()
if err != nil {
return err
}
os.Remove(getLegacyPasswordFileLocation())
return writePasswordFile(p.Steampipe)
}
return nil
}
func getLegacyPasswords() (*Passwords, error) {
contentBytes, err := os.ReadFile(getLegacyPasswordFileLocation())
if err != nil {
return nil, err
}
var passwords = new(Passwords)
err = json.Unmarshal(contentBytes, passwords)
if err != nil {
return nil, err
}
return passwords, nil
}