forked from pachyderm/pachyderm
/
helpers.go
82 lines (71 loc) · 2.69 KB
/
helpers.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
package pachyderm
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
// errMissingField returns a logical response error that prints a consistent
// error message for when a required field is missing.
func errMissingField(field string) *logical.Response {
return logical.ErrorResponse(fmt.Sprintf("missing required field '%s'", field))
}
// validateFields verifies that no bad arguments were given to the request.
func validateFields(req *logical.Request, data *framework.FieldData) error {
var unknownFields []string
for k := range req.Data {
if _, ok := data.Schema[k]; !ok {
unknownFields = append(unknownFields, k)
}
}
if len(unknownFields) > 0 {
return fmt.Errorf("unknown fields: %q", unknownFields)
}
return nil
}
// putConfig parses and returns the configuration data from the storage backend.
func putConfig(ctx context.Context, s logical.Storage, cfg *config) error {
entry, err := logical.StorageEntryJSON("config", cfg)
if err != nil {
return fmt.Errorf("%v: failed to generate storage entry", err)
}
if err := s.Put(ctx, entry); err != nil {
return fmt.Errorf("%v: failed to write configuration to storage", err)
}
return nil
}
// getConfig parses and returns the configuration data from the storage backend.
func getConfig(ctx context.Context, s logical.Storage) (*config, error) {
entry, err := s.Get(ctx, "config")
if err != nil {
return nil, fmt.Errorf("%v: failed to get config from storage", err)
}
if entry == nil || len(entry.Value) == 0 {
return nil, errors.New("no configuration in storage")
}
var result config
if err := entry.DecodeJSON(&result); err != nil {
return nil, fmt.Errorf("%v: failed to decode configuration", err)
}
return &result, nil
}
// getStringField extracts 'key' from 'req', and either returns the value as a
// string or an error response (vault path handlers seem to return
// ErrorResponse rather than actual errors for malformed requests)
func getStringField(data *framework.FieldData, key string) (string, *logical.Response) {
valueIface, ok, err := data.GetOkErr(key)
if err != nil {
return "", logical.ErrorResponse(fmt.Sprintf("%v: could not extract '%s' from request", err, key))
}
// the convention (in e.g. https://github.com/hashicorp/vault/blob/8142b42d951119a73ce46daa3331921b5e21cdee/builtin/logical/aws/path_config_lease.go
// seems to be to return logical.ErrorResponse for invalid requests, and error for internal errors)
if !ok {
return "", errMissingField(key)
}
value, ok := valueIface.(string)
if !ok {
return "", logical.ErrorResponse(fmt.Sprintf("invalid type for param '%s' (expected string but got %T)", key, valueIface))
}
return value, nil
}