/
totp.go
251 lines (226 loc) · 7.02 KB
/
totp.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"github.com/hashicorp/errwrap"
cleanhttp "github.com/hashicorp/go-cleanhttp"
multierror "github.com/hashicorp/go-multierror"
rootcerts "github.com/hashicorp/go-rootcerts"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/api/auth/approle"
"github.com/hashicorp/vault/sdk/helper/hclutil"
"github.com/mitchellh/mapstructure"
)
const (
// TOTPDefaultMountPoint is the default path at which TOTP backend will be
// mounted in the Vault server.
TOTPDefaultMountPoint = "totp"
)
// TOTP is a structure representing a vault-totp-helper which can talk to vault server
// in order to verify the OTP entered by the user. It contains the path at which
// TOTP backend is mounted at the server.
type TOTP struct {
client *api.Client
MountPoint string
}
// SSHVerifyResponse is a structure representing the fields in Vault server's
// response.
type TOTPVerifyResponse struct {
// Whether the response validated succssfully
Valid bool `json:"valid" mapstructure:"valid"`
}
// TOTPConfig is a structure which represents the entries from the vault-totp-helper's configuration file.
type TOTPConfig struct {
VaultAddr string `hcl:"vault_addr"`
TOTPMountPoint string `hcl:"totp_mount_point"`
Namespace string `hcl:"namespace"`
CACert string `hcl:"ca_cert"`
CAPath string `hcl:"ca_path"`
TLSSkipVerify bool `hcl:"tls_skip_verify"`
TLSServerName string `hcl:"tls_server_name"`
TokenFile string `hcl:"token_file"`
RoleID string `hcl:"role_id"`
SecretID string `hcl:"secret_id"`
SecretFile string `hcl:"secret_file"`
SecretEnv string `hcl:"secret_env"`
}
// SetTLSParameters sets the TLS parameters for this TOTP agent.
func (c *TOTPConfig) SetTLSParameters(clientConfig *api.Config, certPool *x509.CertPool) {
tlsConfig := &tls.Config{
InsecureSkipVerify: c.TLSSkipVerify,
MinVersion: tls.VersionTLS12,
RootCAs: certPool,
ServerName: c.TLSServerName,
}
transport := cleanhttp.DefaultTransport()
transport.TLSClientConfig = tlsConfig
clientConfig.HttpClient.Transport = transport
}
// Returns true if any of the following conditions are true:
// * CA cert is configured
// * CA path is configured
// * configured to skip certificate verification
// * TLS server name is configured
//
func (c *TOTPConfig) shouldSetTLSParameters() bool {
return c.CACert != "" || c.CAPath != "" || c.TLSServerName != "" || c.TLSSkipVerify
}
// NewClient returns a new client for the configuration. This client will be used by the
// vault-totp-helper to communicate with Vault server and verify the OTP entered by user.
// If the configuration supplies Vault SSL certificates, then the client will
// have TLS configured in its transport.
func (c *TOTPConfig) NewClient() (*api.Client, error) {
// Creating a default client configuration for communicating with vault server.
clientConfig := api.DefaultConfig()
// Pointing the client to the actual address of vault server.
clientConfig.Address = c.VaultAddr
// Check if certificates are provided via config file.
if c.shouldSetTLSParameters() {
rootConfig := &rootcerts.Config{
CAFile: c.CACert,
CAPath: c.CAPath,
}
certPool, err := rootcerts.LoadCACerts(rootConfig)
if err != nil {
return nil, err
}
// Enable TLS on the HTTP client information
c.SetTLSParameters(clientConfig, certPool)
}
// Creating the client object for the given configuration
client, err := api.NewClient(clientConfig)
if err != nil {
return nil, err
}
// Configure namespace
if c.Namespace != "" {
client.SetNamespace(c.Namespace)
}
return client, nil
}
// LoadTOTPConfig loads totp-helper's configuration from the file and populates the corresponding
// in-memory structure.
//
// Vault address is a required parameter.
// Mount point defaults to "totp".
func LoadTOTPConfig(path string) (*TOTPConfig, error) {
contents, err := ioutil.ReadFile(path)
if err != nil && !os.IsNotExist(err) {
return nil, multierror.Prefix(err, "totp_helper:")
}
return ParseTOTPConfig(string(contents))
}
func countTrue(bools ...bool) int {
c := 0
for _, v := range bools {
if v {
c += 1
}
}
return c
}
// ParseTOTPConfig parses the given contents as a string for the TOTP
// configuration.
func ParseTOTPConfig(contents string) (*TOTPConfig, error) {
root, err := hcl.Parse(string(contents))
if err != nil {
return nil, errwrap.Wrapf("error parsing config: {{err}}", err)
}
list, ok := root.Node.(*ast.ObjectList)
if !ok {
return nil, fmt.Errorf("error parsing config: file doesn't contain a root object")
}
valid := []string{
"vault_addr",
"totp_mount_point",
"namespace",
"ca_cert",
"ca_path",
"tls_skip_verify",
"tls_server_name",
"token_file",
"role_id",
"secret_id",
"secret_file",
"secret_env",
}
if err := hclutil.CheckHCLKeys(list, valid); err != nil {
return nil, multierror.Prefix(err, "totp_helper:")
}
var c TOTPConfig
c.TOTPMountPoint = TOTPDefaultMountPoint
if err := hcl.DecodeObject(&c, list); err != nil {
return nil, multierror.Prefix(err, "totp_helper:")
}
if c.VaultAddr == "" {
return nil, fmt.Errorf(`missing config "vault_addr"`)
}
if countTrue(c.TokenFile != "", c.RoleID != "") > 1 {
return nil, fmt.Errorf(`only one of "token_file" or "role_id" may be provided`)
}
numSecret := countTrue(c.SecretID != "", c.SecretFile != "", c.SecretEnv != "")
if (c.RoleID == "" && numSecret > 0) || (c.RoleID != "" && numSecret != 1) {
return nil, fmt.Errorf(`"role_id" must be accompanied by exactly one of "secret_id", "secret_file", "secret_env"`)
}
return &c, nil
}
func TOTPLogin(ctx context.Context, c *api.Client, config *TOTPConfig) error {
if config.TokenFile != "" {
token, err := ioutil.ReadFile(config.TokenFile)
if err != nil {
return err
}
c.SetToken(string(token))
return nil
}
if config.RoleID != "" {
cred := &approle.SecretID{
FromString: config.SecretID,
FromFile: config.SecretFile,
FromEnv: config.SecretEnv,
}
appRoleAuth, err := approle.NewAppRoleAuth(config.RoleID, cred)
if err != nil {
return err
}
authInfo, err := c.Auth().Login(ctx, appRoleAuth)
if err != nil {
return err
}
if authInfo == nil {
return fmt.Errorf("no auth info was returned")
}
return nil
}
if os.Getenv("VAULT_TOKEN") != "" {
// This is picked up automatically
return nil
}
return fmt.Errorf("No Vault authentication credentials set")
}
// Verify the TOTP response string provided by the user against the Vault server.
func TOTPVerify(c *api.Client, mountpoint, username, otp string) error {
data := map[string]interface{}{
"code": otp,
}
verifyPath := fmt.Sprintf("%s/code/%s", mountpoint, username)
secret, err := c.Logical().Write(verifyPath, data)
if err != nil {
return err
}
var verifyResp TOTPVerifyResponse
err = mapstructure.Decode(secret.Data, &verifyResp)
if err != nil {
return err
}
if verifyResp.Valid != true {
return fmt.Errorf("Response is invalid")
}
return nil
}