-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
163 lines (135 loc) · 3.24 KB
/
service.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
154
155
156
157
158
159
160
161
162
163
package memguarded
import (
"fmt"
"github.com/awnumar/memguard"
"github.com/n0rad/go-erlog/errs"
"github.com/n0rad/go-erlog/logs"
"golang.org/x/crypto/ssh/terminal"
"io"
"net"
"os"
"sync"
"syscall"
)
type Service struct {
secret *memguard.Enclave
notify map[chan struct{}]struct{}
notifyLock sync.RWMutex
stop chan struct{}
}
func (s *Service) Init() {
s.notify = make(map[chan struct{}]struct{})
s.stop = make(chan struct{})
}
func (s *Service) Stop(e error) {
close(s.stop)
}
func (s *Service) Start() error {
memguard.CatchInterrupt()
<-s.stop
return nil
}
func (s *Service) FromBytes(secret *[]byte) error {
defer memguard.WipeBytes(*secret)
s.setAndNotify(memguard.NewBufferFromBytes(*secret))
return nil
}
func (s *Service) FromReaderUntilNewLine(conn net.Conn) error {
buffer, err := memguard.NewBufferFromReaderUntil(conn, '\n')
if err != nil && err != io.EOF {
return errs.WithE(err, "Failed to read secret from connection")
}
s.setAndNotify(buffer)
return nil
}
func (s *Service) AskSecret(confirmation bool, name string) error {
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
return errs.With("Cannot ask secret, not in a terminal")
}
return s.FromStdin(confirmation, name)
}
func (s *Service) FromStdin(confirmation bool, name string) error {
var secret, secretConfirm []byte
defer memguard.WipeBytes(secret)
defer memguard.WipeBytes(secretConfirm)
for {
var err error
print(name + ": ")
secret, err = terminal.ReadPassword(syscall.Stdin)
if err != nil {
return errs.WithE(err, "Cannot read secret")
}
print("\n")
if !confirmation {
s.setAndNotify(memguard.NewBufferFromBytes(secret))
return nil
}
print("Confirm: ")
secretConfirm, err = terminal.ReadPassword(syscall.Stdin)
if err != nil {
return errs.WithE(err, "Cannot read secret")
}
print("\n")
if string(secret) == string(secretConfirm) && string(secret) != "" {
s.setAndNotify(memguard.NewBufferFromBytes(secret))
return nil
} else {
fmt.Println("\nEmpty secret or do not match...")
fmt.Println()
}
}
}
func (s *Service) Unwatch(c chan struct{}) {
s.notifyLock.Lock()
defer s.notifyLock.Unlock()
delete(s.notify, c)
}
func (s *Service) Watch() chan struct{} {
s.notifyLock.Lock()
defer s.notifyLock.Unlock()
c := make(chan struct{})
s.notify[c] = struct{}{}
return c
}
func (s Service) Write(writer io.Writer) error {
var total, written int
var err error
if s.secret == nil {
return errs.With("Secret is not set")
}
lockedBuffer, err := s.secret.Open()
if err != nil {
return errs.WithE(err, "Failed to open secret enclave")
}
defer lockedBuffer.Destroy()
bytes := lockedBuffer.Bytes()
for total = 0; total < len(bytes); total += written {
written, err = writer.Write(bytes[total:])
if err != nil {
return err
}
}
return nil
}
func (s Service) IsSet() bool {
if s.secret != nil {
return true
}
return false
}
func (s Service) Get() (*memguard.LockedBuffer, error) {
if !s.IsSet() {
return nil, errs.With("No secret set")
}
return s.secret.Open()
}
/////
func (s *Service) setAndNotify(buffer *memguard.LockedBuffer) {
s.notifyLock.RLock()
defer s.notifyLock.RUnlock()
logs.Debug("Secret set")
s.secret = buffer.Seal()
for e := range s.notify {
e <- struct{}{}
}
}