-
Notifications
You must be signed in to change notification settings - Fork 71
/
tls_spec.go
178 lines (157 loc) · 5.56 KB
/
tls_spec.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
//
// DISCLAIMER
//
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package v2alpha1
import (
"net"
shared "github.com/arangodb/kube-arangodb/pkg/apis/shared"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/validation"
)
type TLSRotateMode string
func (t *TLSRotateMode) Get() TLSRotateMode {
if t == nil {
return TLSRotateModeInPlace
}
return *t
}
func (t TLSRotateMode) New() *TLSRotateMode {
return &t
}
const (
TLSRotateModeInPlace TLSRotateMode = "inplace"
TLSRotateModeRecreate TLSRotateMode = "recreate"
)
const (
defaultTLSTTL = Duration("2610h") // About 3 month
)
// TLSSpec holds TLS specific configuration settings
type TLSSpec struct {
// CASecretName setting specifies the name of a kubernetes `Secret` that contains
// a standard CA certificate + private key used to sign certificates for individual
// ArangoDB servers.
// When no name is specified, it defaults to `<deployment-name>-ca`.
// To disable authentication, set this value to `None`.
// If you specify a name of a `Secret` that does not exist, a self-signed CA certificate + key is created
// and stored in a `Secret` with given name.
// The specified `Secret`, must contain the following data fields:
// - `ca.crt` PEM encoded public key of the CA certificate
// - `ca.key` PEM encoded private key of the CA certificate
CASecretName *string `json:"caSecretName,omitempty"`
// AltNames setting specifies a list of alternate names that will be added to all generated
// certificates. These names can be DNS names or email addresses.
// The default value is empty.
// +doc/type: []string
AltNames []string `json:"altNames,omitempty"`
// TTL setting specifies the time to live of all generated server certificates.
// When the server certificate is about to expire, it will be automatically replaced
// by a new one and the affected server will be restarted.
// Note: The time to live of the CA certificate (when created automatically)
// will be set to 10 years.
// +doc/default: "2160h" (about 3 months)
TTL *Duration `json:"ttl,omitempty"`
SNI *TLSSNISpec `json:"sni,omitempty"`
Mode *TLSRotateMode `json:"mode,omitempty"`
}
const (
// CASecretNameDisabled is the value of CASecretName to use for disabling authentication.
CASecretNameDisabled = "None"
)
// GetCASecretName returns the value of caSecretName.
func (s TLSSpec) GetCASecretName() string {
return util.TypeOrDefault[string](s.CASecretName)
}
// GetAltNames returns the value of altNames.
func (s TLSSpec) GetAltNames() []string {
return s.AltNames
}
// GetTTL returns the value of ttl.
func (s TLSSpec) GetTTL() Duration {
return DurationOrDefault(s.TTL)
}
func (a TLSSpec) GetSNI() TLSSNISpec {
if a.SNI == nil {
return TLSSNISpec{}
}
return *a.SNI
}
// IsSecure returns true when a CA secret has been set, false otherwise.
func (s TLSSpec) IsSecure() bool {
return s.GetCASecretName() != CASecretNameDisabled
}
// GetParsedAltNames splits the list of AltNames into DNS names, IP addresses & email addresses.
// When an entry is not valid for any of those categories, an error is returned.
func (s TLSSpec) GetParsedAltNames() (dnsNames, ipAddresses, emailAddresses []string, err error) {
for _, name := range s.GetAltNames() {
if net.ParseIP(name) != nil {
ipAddresses = append(ipAddresses, name)
} else if validation.IsValidDNSName(name) {
dnsNames = append(dnsNames, name)
} else if validation.IsValidEmailAddress(name) {
emailAddresses = append(emailAddresses, name)
} else {
return nil, nil, nil, errors.WithStack(errors.Errorf("'%s' is not a valid alternate name", name))
}
}
return dnsNames, ipAddresses, emailAddresses, nil
}
// Validate the given spec
func (s TLSSpec) Validate() error {
if s.IsSecure() {
if err := shared.ValidateResourceName(s.GetCASecretName()); err != nil {
return errors.WithStack(err)
}
if _, _, _, err := s.GetParsedAltNames(); err != nil {
return errors.WithStack(err)
}
if err := s.GetTTL().Validate(); err != nil {
return errors.WithStack(err)
}
}
return nil
}
// SetDefaults fills in missing defaults
func (s *TLSSpec) SetDefaults(defaultCASecretName string) {
if s.GetCASecretName() == "" {
// Note that we don't check for nil here, since even a specified, but empty
// string should result in the default value.
s.CASecretName = util.NewType[string](defaultCASecretName)
}
if s.GetTTL() == "" {
// Note that we don't check for nil here, since even a specified, but zero
// should result in the default value.
s.TTL = NewDuration(defaultTLSTTL)
}
}
// SetDefaultsFrom fills unspecified fields with a value from given source spec.
func (s *TLSSpec) SetDefaultsFrom(source TLSSpec) {
if s.CASecretName == nil {
s.CASecretName = util.NewTypeOrNil[string](source.CASecretName)
}
if s.AltNames == nil {
s.AltNames = source.AltNames
}
if s.TTL == nil {
s.TTL = NewDurationOrNil(source.TTL)
}
if s.SNI == nil {
s.SNI = source.SNI.DeepCopy()
}
}