forked from miquella/vaulted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tty_steward.go
153 lines (129 loc) · 3.25 KB
/
tty_steward.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"fmt"
"os"
"github.com/miquella/ask"
"github.com/miquella/vaulted/lib"
"github.com/miquella/vaulted/lib/legacy"
)
type TTYSteward struct{}
func (*TTYSteward) VaultExists(name string) bool {
return vaulted.VaultExists(name)
}
func (*TTYSteward) ListVaults() ([]string, error) {
return vaulted.ListVaults()
}
func (*TTYSteward) SealVault(name string, password *string, vault *vaulted.Vault) error {
if password == nil {
envPassword := os.Getenv("VAULTED_NEW_PASSWORD")
if envPassword != "" {
password = &envPassword
} else {
for {
newPassword, err := ask.HiddenAsk("New Password: ")
if err != nil {
return err
}
confirmPassword, err := ask.HiddenAsk("Confirm Password: ")
if err != nil {
return err
}
if newPassword == confirmPassword {
password = &newPassword
break
} else {
ask.Print("Passwords do not match.\n")
}
}
}
}
return vaulted.SealVault(name, *password, vault)
}
func (*TTYSteward) OpenVault(name string, password *string) (string, *vaulted.Vault, error) {
if !vaulted.VaultExists(name) {
return "", nil, os.ErrNotExist
}
if password == nil && os.Getenv("VAULTED_PASSWORD") != "" {
envPassword := os.Getenv("VAULTED_PASSWORD")
password = &envPassword
}
var vault *vaulted.Vault
var err error
if password != nil {
vault, err = vaulted.OpenVault(name, *password)
} else {
for i := 0; i < 3; i++ {
var requestedPassword string
requestedPassword, err = ask.HiddenAsk("Password: ")
if err != nil {
break
}
vault, err = vaulted.OpenVault(name, requestedPassword)
if err != vaulted.ErrInvalidPassword {
password = &requestedPassword
break
}
}
}
if err != nil {
return "", nil, err
}
return *password, vault, nil
}
func (*TTYSteward) RemoveVault(name string) error {
return vaulted.RemoveVault(name)
}
func (*TTYSteward) GetEnvironment(name string, password *string) (string, *vaulted.Environment, error) {
if !vaulted.VaultExists(name) {
return "", nil, os.ErrNotExist
}
if password == nil && os.Getenv("VAULTED_PASSWORD") != "" {
envPassword := os.Getenv("VAULTED_PASSWORD")
password = &envPassword
}
var env *vaulted.Environment
var err error
if password != nil {
env, err = vaulted.GetEnvironment(name, *password)
} else {
for i := 0; i < 3; i++ {
var requestedPassword string
requestedPassword, err = ask.HiddenAsk("Password: ")
if err != nil {
break
}
env, err = vaulted.GetEnvironment(name, requestedPassword)
if err != vaulted.ErrInvalidPassword {
password = &requestedPassword
break
}
}
}
if err != nil {
return "", nil, err
}
return *password, env, nil
}
func (*TTYSteward) OpenLegacyVault() (password string, environments map[string]legacy.Environment, err error) {
legacyVault, err := legacy.ReadVault()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
password = os.Getenv("VAULTED_PASSWORD")
if password != "" {
environments, err = legacyVault.DecryptEnvironments(password)
} else {
for i := 0; i < 3; i++ {
password, err = ask.HiddenAsk("Legacy Password: ")
if err != nil {
break
}
environments, err = legacyVault.DecryptEnvironments(password)
if err != legacy.ErrInvalidPassword {
break
}
}
}
return
}