-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
141 lines (128 loc) · 3.89 KB
/
config.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
package keyStore
import (
"github.com/echocat/caretakerd/errors"
"github.com/echocat/caretakerd/values"
"strconv"
"strings"
)
var defaults = map[string]interface{}{
"Type": Generated,
"PemFile": values.String(""),
"Hints": values.String("algorithm:`rsa` bits:`1024`"),
"CaFile": values.String(""),
}
// # Description
//
// Defines the keyStore of caretakerd.
type Config struct {
// @default generated
//
// Defines the type of the instance keyStore.
Type Type `json:"type" yaml:"type"`
// @default ""
//
// Defines the pemFile which contains the key and certificate to be used.
// This has to be of type PEM and has to contain the certificate and private key.
// Currently only private keys of type RSA are supported.
//
// This property is only evaluated and required if {@ref #Type type} is set to
// {@ref .Type#FromFile fromFile}.
PemFile values.String `json:"pemFile,omitempty" yaml:"pemFile"`
// @default "algorithm:`rsa` bits:`1024`"
//
// Defines some hints, for example to store in the format ``[<key:`value`>...]``.
// Possible hints are:
//
// * ``algorithm``: Algorithm to be used to create new keys. Currently only ``rsa`` is supported.
// * ``bits``: Number of bits to create a new key with.
Hints values.String `json:"hints,omitempty" yaml:"hints"`
// @default ""
//
// File where trusted certificates are stored in. This has to be in PEM format.
CaFile values.String `json:"caFile,omitempty" yaml:"caFile"`
}
// NewConfig creates a new instance of Config.
func NewConfig() Config {
result := Config{}
values.SetDefaultsTo(defaults, &result)
return result
}
// Validate validates an action on this object and returns an error object if there are any.
func (instance Config) Validate() error {
err := instance.Type.Validate()
if err == nil {
err = instance.validateRequireStringOrNotValue(instance.PemFile, "pemFile", instance.Type.IsTakingFilename)
}
if err == nil {
err = instance.validateStringOnlyAllowedValue(instance.CaFile, "caFile", instance.Type.IsConsumingCAFile)
}
if err == nil {
algorithm := instance.GetHintsArgument("algorithm")
if len(algorithm) > 0 && strings.ToLower(algorithm) != "rsa" {
err = errors.New("Unsupported algorithm: %s", algorithm)
}
}
return err
}
func (instance Config) validateRequireStringOrNotValue(value values.String, fieldName string, isAllowedMethod func() bool) error {
if isAllowedMethod() {
if value.IsEmpty() {
return errors.New("There is no %s set for type %v.", fieldName, instance.Type)
}
} else {
if !value.IsEmpty() {
return errors.New("There is no %s allowed for type %v.", fieldName, instance.Type)
}
}
return nil
}
func (instance Config) validateStringOnlyAllowedValue(value values.String, fieldName string, isAllowedMethod func() bool) error {
if !isAllowedMethod() && !value.IsEmpty() {
return errors.New("There is no %s allowed for type %v.", fieldName, instance.Type)
}
return nil
}
// GetHintsArgument returns hints argument content for the given key.
// If there is no hint for this key and empty string is returned.
func (instance Config) GetHintsArgument(key string) string {
arguments := instance.Hints
for arguments != "" {
i := 0
for i < len(arguments) && arguments[i] == ' ' {
i++
}
arguments = arguments[i:]
if arguments == "" {
break
}
i = 0
for i < len(arguments) && arguments[i] > ' ' && arguments[i] != ':' && arguments[i] != '`' && arguments[i] != 0x7f {
i++
}
if i == 0 || i+1 >= len(arguments) || arguments[i] != ':' || arguments[i+1] != '`' {
break
}
name := string(arguments[:i])
arguments = arguments[i+1:]
i = 1
for i < len(arguments) && arguments[i] != '`' {
if arguments[i] == '\\' {
i++
}
i++
}
if i >= len(arguments) {
break
}
qvalue := string(arguments[:i+1])
arguments = arguments[i+1:]
if key == name {
value, err := strconv.Unquote(qvalue)
if err != nil {
break
}
return value
}
}
return ""
}