forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 2
/
path_verify.go
96 lines (83 loc) · 2.6 KB
/
path_verify.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
package ssh
import (
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func pathVerify(b *backend) *framework.Path {
return &framework.Path{
Pattern: "verify",
Fields: map[string]*framework.FieldSchema{
"otp": &framework.FieldSchema{
Type: framework.TypeString,
Description: "[Required] One-Time-Key that needs to be validated",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.WriteOperation: b.pathVerifyWrite,
},
HelpSynopsis: pathVerifyHelpSyn,
HelpDescription: pathVerifyHelpDesc,
}
}
func (b *backend) getOTP(s logical.Storage, n string) (*sshOTP, error) {
entry, err := s.Get("otp/" + n)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var result sshOTP
if err := entry.DecodeJSON(&result); err != nil {
return nil, err
}
return &result, nil
}
func (b *backend) pathVerifyWrite(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
otp := d.Get("otp").(string)
// If OTP is not a UUID and a string matching VerifyEchoRequest, then the
// response will be VerifyEchoResponse. This is used by agent to check if
// connection to Vault server is proper.
if otp == api.VerifyEchoRequest {
return &logical.Response{
Data: map[string]interface{}{
"message": api.VerifyEchoResponse,
},
}, nil
}
// Create the salt of OTP because entry would have been create with the
// salt and not directly of the OTP. Salt will yield the same value which
// because the seed is the same, the backend salt.
otpSalted := b.salt.SaltID(otp)
// Return nil if there is no entry found for the OTP
otpEntry, err := b.getOTP(req.Storage, otpSalted)
if err != nil {
return nil, err
}
if otpEntry == nil {
return nil, nil
}
// Delete the OTP if found. This is what makes the key an OTP.
err = req.Storage.Delete("otp/" + otpSalted)
if err != nil {
return nil, err
}
// Return username and IP only if there were no problems uptill this point.
return &logical.Response{
Data: map[string]interface{}{
"username": otpEntry.Username,
"ip": otpEntry.IP,
},
}, nil
}
const pathVerifyHelpSyn = `
Validate the OTP provided by Vault SSH Agent.
`
const pathVerifyHelpDesc = `
This path will be used by Vault SSH Agent runnin in the remote hosts. The OTP
provided by the client is sent to Vault for validation by the agent. If Vault
finds an entry for the OTP, it responds with the username and IP it is associated
with. Agent uses this information to authenticate the client. Vault deletes the
OTP after validating it once.
`