-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_connection.go
295 lines (282 loc) · 10.6 KB
/
model_connection.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package arcaflow_lib_kubernetes
import (
"encoding/json"
"fmt"
"regexp"
"arcaflow-lib-kubernetes/internal/util"
"go.flow.arcalot.io/pluginsdk/schema"
)
// ConnectionParameters describes how to connect to the Kubernetes API.
type ConnectionParameters struct {
Host string `json:"host"`
APIPath string `json:"path"`
Username string `json:"username"`
Password string `json:"password"`
ServerName string `json:"serverName"`
CertData string `json:"cert"`
CertFile string `json:"certFile"`
KeyData string `json:"key"`
KeyFile string `json:"keyFile"`
CAData string `json:"cacert"`
CAFile string `json:"cacertFile"`
BearerToken string `json:"bearerToken"`
BearerTokenFile string `json:"bearerTokenFile"`
Insecure bool `json:"insecure"`
}
// UnmarshalJSON uses the Arcaflow schema system to unmarshal JSON data when called via json.Unmarshal on the
// ConnectionParameters struct. This prevents accidentally using the wrong unmarshalling method.
func (c *ConnectionParameters) UnmarshalJSON(data []byte) error {
return c.UnmarshalYAML(func(a any) error {
return json.Unmarshal(data, a)
})
}
// MarshalJSON uses the Arcaflow schema system to marshal JSON data when called via json.Marshal on the
// ConnectionParameters struct. This prevents accidentally using the wrong unmarshalling method.
func (c *ConnectionParameters) MarshalJSON() ([]byte, error) {
serializedData, err := c.MarshalYAML()
if err != nil {
return nil, err
}
data, err := json.Marshal(serializedData)
if err != nil {
return nil, fmt.Errorf("failed to marshal JSON data (%w)", err)
}
return data, nil
}
// UnmarshalYAML uses the Arcaflow schema system to unmarshal YAML data when called via yaml.Unmarshal on the
// ConnectionParameters struct. This prevents accidentally using the wrong unmarshalling method.
func (c *ConnectionParameters) UnmarshalYAML(unmarshaller func(interface{}) error) error {
temp := map[string]any{}
if err := unmarshaller(&temp); err != nil {
return fmt.Errorf("failed to JSON unmarshal data (%w)", err)
}
unserializedData, err := connectionParametersSchema.UnserializeType(temp) //nolint:all
if err != nil {
return fmt.Errorf("failed to unserialize data (%w)", err)
}
// This assignment has no effect! We disable the linter's objection to it
// pending investigation.
// see https://github.com/arcalot/arcaflow-lib-kubernetes-go/issues/18
c = &unserializedData //nolint:all
return nil
}
// MarshalYAML uses the Arcaflow schema system to marshal JSON data when called via json.Marshal on the
// ConnectionParameters struct. This prevents accidentally using the wrong unmarshalling method.
func (c *ConnectionParameters) MarshalYAML() (any, error) {
serializedData, err := connectionParametersSchema.Serialize(c)
if err != nil {
return nil, fmt.Errorf("failed to serialize connection parameters (%w)", err)
}
return serializedData, nil
}
var connectionParametersSchema = schema.NewTypedObject[ConnectionParameters](
"Connection",
map[string]*schema.PropertySchema{
"host": schema.NewPropertySchema(
schema.NewStringSchema(schema.IntPointer(1), nil, nil),
schema.NewDisplayValue(
schema.PointerTo("Host"),
schema.PointerTo("Host name and port of the Kubernetes server"),
nil,
),
false,
nil,
nil,
nil,
schema.PointerTo(`"kubernetes.default.svc"`),
nil,
).TreatEmptyAsDefaultValue(),
"path": schema.NewPropertySchema(
schema.NewStringSchema(nil, nil, nil),
schema.NewDisplayValue(
schema.PointerTo("Path"),
schema.PointerTo("Path to the API server."),
nil,
),
false,
nil,
nil,
nil,
schema.PointerTo(`"/api"`),
nil,
).TreatEmptyAsDefaultValue(),
"username": schema.NewPropertySchema(
schema.NewStringSchema(nil, nil, nil),
schema.NewDisplayValue(
schema.PointerTo("Username"),
schema.PointerTo("Username for basic authentication."),
nil,
),
false,
[]string{"password"},
nil,
nil,
nil,
nil,
),
"password": schema.NewPropertySchema(
schema.NewStringSchema(nil, nil, nil),
schema.NewDisplayValue(
schema.PointerTo("Password"),
schema.PointerTo("Password for basic authentication."),
nil,
),
false,
[]string{"username"},
nil,
nil,
nil,
nil,
),
"serverName": schema.NewPropertySchema(
schema.NewStringSchema(nil, nil, nil),
schema.NewDisplayValue(
schema.PointerTo("TLS server name"),
schema.PointerTo("Expected TLS server name to verify in the certificate."),
nil,
),
false,
nil,
nil,
nil,
nil,
nil,
),
"cacert": schema.NewPropertySchema(
schema.NewStringSchema(nil, nil, regexp.MustCompile(`^$|^\s*-----BEGIN CERTIFICATE-----(\s*.*\s*)*-----END CERTIFICATE-----\s*$`)),
schema.NewDisplayValue(
schema.PointerTo("CA certificate"),
schema.PointerTo("CA certificate in PEM format to verify Kubernetes server certificate against."),
nil,
),
false,
nil,
nil,
nil,
nil,
[]string{
util.JSONEncode(util.Base64Decode(`LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUI0VENDQVl1Z0F3SUJBZ0lVQ0hoaGZmWTFsemV6R2F0WU1SMDJncEVKQ2hrd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1JURUxNQWtHQTFVRUJoTUNRVlV4RXpBUkJnTlZCQWdNQ2xOdmJXVXRVM1JoZEdVeElUQWZCZ05WQkFvTQpHRWx1ZEdWeWJtVjBJRmRwWkdkcGRITWdVSFI1SUV4MFpEQWVGdzB5TWpBNU1qZ3dOVEk0TVRKYUZ3MHlNekE1Ck1qZ3dOVEk0TVRKYU1FVXhDekFKQmdOVkJBWVRBa0ZWTVJNd0VRWURWUVFJREFwVGIyMWxMVk4wWVhSbE1TRXcKSHdZRFZRUUtEQmhKYm5SbGNtNWxkQ0JYYVdSbmFYUnpJRkIwZVNCTWRHUXdYREFOQmdrcWhraUc5dzBCQVFFRgpBQU5MQURCSUFrRUFycjg5ZjJrZ2dTTy95YUNCNkV3SVFlVDZacHRCb1gwWnZDTUkrRHBrQ3dxT1M1ZndSYmoxCm5FaVBuTGJ6RERnTVU4S0NQQU1oSTdKcFlSbEhuaXB4V3dJREFRQUJvMU13VVRBZEJnTlZIUTRFRmdRVWlaNkoKRHd1RjlRQ2gxdndRR1hzMk11dHVROUV3SHdZRFZSMGpCQmd3Rm9BVWlaNkpEd3VGOVFDaDF2d1FHWHMyTXV0dQpROUV3RHdZRFZSMFRBUUgvQkFVd0F3RUIvekFOQmdrcWhraUc5dzBCQVFzRkFBTkJBRllJRk0yN0JEaUc3MjVkClZraFJibGt2WnplUkhoY3d0RE9RVEM5ZDhNL0x5bU4yeTBuSFNsSkNabS9Mby9hSDh2aVNZMXZpMUdTSGZEejcKVGxmZThncz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=`)),
},
),
"cacertFile": schema.NewPropertySchema(
schema.NewStringSchema(nil, nil, nil),
schema.NewDisplayValue(
schema.PointerTo("CA certificate file"),
schema.PointerTo("File holding the CA certificate in PEM format to verify Kubernetes server certificate against."),
nil,
),
false,
nil,
nil,
nil,
schema.PointerTo(util.JSONEncode("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt")),
nil,
),
"cert": schema.NewPropertySchema(
schema.NewStringSchema(nil, nil, regexp.MustCompile(`^$|^\s*-----BEGIN CERTIFICATE-----(\s*.*\s*)*-----END CERTIFICATE-----\s*$`)),
schema.NewDisplayValue(
schema.PointerTo("Client certificate"),
schema.PointerTo("Client certificate in PEM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ySchema(
schema.NewStringSchema(nil, nil, nil),
schema.NewDisplayValue(
schema.PointerTo("Client certificate file"),
schema.PointerTo("File holding the client certificate in PEM format to authenticate against Kubernetes with."),
nil,
),
false,
nil,
nil,
nil,
nil,
nil,
),
"key": schema.NewPropertySchema(
schema.NewStringSchema(nil, nil, regexp.MustCompile(`^$|^[-]+BEGIN (?:.* )?PRIVATE KEY[-]+([^-]*)[-]+END (?:.* )?PRIVATE KEY[-]+\s*$`)),
schema.NewDisplayValue(
schema.PointerTo("Client key"),
schema.PointerTo("Client private key in PEM format to authenticate against Kubernetes with."),
nil,
),
false,
nil,
nil,
nil,
nil,
[]string{
util.JSONEncode(util.Base64Decode(`LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JSUJWQUlCQURBTkJna3Foa2lHOXcwQkFRRUZBQVNDQVQ0d2dnRTZBZ0VBQWtFQXJyODlmMmtnZ1NPL3lhQ0IKNkV3SVFlVDZacHRCb1gwWnZDTUkrRHBrQ3dxT1M1ZndSYmoxbkVpUG5MYnpERGdNVThLQ1BBTWhJN0pwWVJsSApuaXB4V3dJREFRQUJBa0J5YnUveDBNRWxjR2kydS9KMlVkd1Njc1Y3amU1VHQxMno4Mmw3VEptWkZGSjhSTG1jCnJoMDBHdmViNFZwR2hkMStjM2xaYk8xbUlUNnYzdkhNOUEwaEFpRUExNEVXNmIrOTlYWXphNys1dXdJRHVpTSsKQnozcGtLKzl0bGZWWEU3SnlLc0NJUURQbFlKNXh0YnVUK1Z2QjNYT2REL1ZXaUVxRW12RTNmbFYwNDE3UnFoYQpFUUlnYnl4d05wd3RFZ0V0Vzh1bnRCckE4M2lVMmtXTlJZL3o3YXA0TGt1Uyswc0NJR2UyRSswUm1mcVFzbGxwCmljTXZNMkU5MllueWtDTlluNlR3d0NRU0pqUnhBaUVBbzlNbWFWbEs3WWRoU01QbzUydUpZemQ5TVFaSnFocSsKbEIxWkdEeC9BUkU9Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K`)),
},
),
"keyFile": schema.NewPropertySchema(
schema.NewStringSchema(nil, nil, nil),
schema.NewDisplayValue(
schema.PointerTo("Client key file"),
schema.PointerTo("File holding the client private key in PEM format to authenticate against Kubernetes with."),
nil,
),
false,
nil,
nil,
nil,
nil,
nil,
),
"bearerToken": schema.NewPropertySchema(
schema.NewStringSchema(nil, nil, nil),
schema.NewDisplayValue(
schema.PointerTo("Bearer token"),
schema.PointerTo("Bearer token to authenticate against the Kubernetes API with."),
nil,
),
false,
nil,
nil,
nil,
nil,
nil,
),
"bearerTokenFile": schema.NewPropertySchema(
schema.NewStringSchema(nil, nil, nil),
schema.NewDisplayValue(
schema.PointerTo("Bearer token file"),
schema.PointerTo("File holding the bearer token to authenticate against the Kubernetes API with."),
nil,
),
false,
nil,
nil,
nil,
schema.PointerTo(util.JSONEncode("/var/run/secrets/kubernetes.io/serviceaccount")),
nil,
),
"insecure": schema.NewPropertySchema(
schema.NewBoolSchema(),
schema.NewDisplayValue(
schema.PointerTo("Insecure connection"),
schema.PointerTo("Skip TLS verification"),
nil,
),
false,
nil,
nil,
nil,
nil,
nil,
),
},
)
// ConnectionParametersSchema returns a schema for serializing/unserializing connection parameters.
func ConnectionParametersSchema() *schema.ObjectSchema {
return &connectionParametersSchema.ObjectSchema
}