-
Notifications
You must be signed in to change notification settings - Fork 0
/
unseal.go
67 lines (57 loc) · 1.81 KB
/
unseal.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
package services
import (
"context"
"github.com/PumpkinSeed/heimdall/internal/errors"
"github.com/PumpkinSeed/heimdall/internal/structs"
"github.com/PumpkinSeed/heimdall/pkg/crypto/unseal"
"github.com/PumpkinSeed/heimdall/pkg/crypto/utils"
log "github.com/sirupsen/logrus"
)
type Unseal struct {
state *unseal.Unseal
}
func NewUnseal(u *unseal.Unseal) Unseal {
return Unseal{state: u}
}
func (u Unseal) Handler(ctx context.Context, req structs.SocketRequest) (structs.SocketResponse, error) {
if status := u.state.Status(); status.Unsealed {
return structs.SocketResponse{
Data: []byte(status.String()),
}, nil
}
done, err := u.state.Unseal(ctx, string(req.Data))
if err != nil {
return structs.SocketResponse{
Data: []byte(u.state.Status().String()),
}, errors.Wrap(err, "unseal error", errors.CodeApiSocketUnseal)
}
if !done {
log.Debug("Unseal not done yet")
return structs.SocketResponse{
Data: []byte(u.state.Status().String()),
}, nil
}
if err := u.state.Keyring(ctx); err != nil {
log.Debug("Keyring init error")
return structs.SocketResponse{
Data: []byte(u.state.Status().String()),
}, errors.Wrap(err, "unseal keyring error", errors.CodeApiSocketUnsealKeyring)
}
barrierPath, err := u.state.Mount(ctx)
if err != nil {
log.Debug("Mount error")
return structs.SocketResponse{
Data: []byte(u.state.Status().String()),
}, errors.Wrap(err, "unseal mount error", errors.CodeApiSocketUnsealMount)
}
if err := u.state.PostProcess(ctx, barrierPath); err != nil {
log.Debug("Post process error")
return structs.SocketResponse{
Data: []byte(u.state.Status().String()),
}, errors.Wrap(err, "unseal post process error", errors.CodeApiSocketUnsealPostProcess)
}
utils.Memzero(req.Data)
return structs.SocketResponse{
Data: []byte(u.state.Status().String()),
}, nil
}