Skip to content

Commit 9e4c9be

Browse files
authored
fix: json schema for kube config (#109)
1 parent 4b2253e commit 9e4c9be

File tree

3 files changed

+81
-99
lines changed

3 files changed

+81
-99
lines changed

modules/firehose/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ func (mc *moduleConfig) validateAndSanitize(r resource.Resource) error {
5050
return nil
5151
}
5252

53-
func generateFirehoseName(r resource.Resource) string {
54-
return fmt.Sprintf("%s-%s-firehose", r.Project, r.Name)
55-
}
56-
57-
func (mc moduleConfig) GetHelmReleaseConfig(r resource.Resource) (*helm.ReleaseConfig, error) {
53+
func (mc *moduleConfig) GetHelmReleaseConfig(r resource.Resource) (*helm.ReleaseConfig, error) {
5854
var output Output
5955
err := json.Unmarshal(r.State.Output, &output)
6056
if err != nil {
@@ -94,10 +90,14 @@ func (mc moduleConfig) GetHelmReleaseConfig(r resource.Resource) (*helm.ReleaseC
9490
return rc, nil
9591
}
9692

97-
func (mc moduleConfig) JSON() []byte {
93+
func (mc *moduleConfig) JSON() []byte {
9894
b, err := json.Marshal(mc)
9995
if err != nil {
10096
panic(err)
10197
}
10298
return b
10399
}
100+
101+
func generateFirehoseName(r resource.Resource) string {
102+
return fmt.Sprintf("%s-%s-firehose", r.Project, r.Name)
103+
}

modules/kubernetes/config_schema.json

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
3-
"$id": "http://json-schema.org/draft-07/schema#",
43
"type": "object",
54
"properties": {
65
"host": {
76
"type": "string",
8-
"format": "uri",
9-
"pattern": "^https?://"
7+
"format": "uri"
108
},
119
"insecure": {
1210
"type": "boolean",
@@ -21,61 +19,38 @@
2119
"client_certificate": {
2220
"type": "string"
2321
},
24-
"cluster_ca_certificate": {
22+
"client_ca_certificate": {
2523
"type": "string"
26-
},
27-
"timeout": {
28-
"type": "number",
29-
"default": 100000000
3024
}
3125
},
32-
"allOf": [
26+
"required": [
27+
"host"
28+
],
29+
"anyOf": [
3330
{
34-
"oneOf": [
35-
{
36-
"required": [
37-
"token"
38-
]
39-
},
40-
{
41-
"allOf": [
42-
{
43-
"required": [
44-
"client_key"
45-
]
46-
},
47-
{
48-
"required": [
49-
"client_certificate"
50-
]
51-
}
52-
]
53-
}
31+
"required": [
32+
"token"
5433
]
5534
},
5635
{
57-
"oneOf": [
58-
{
59-
"required": [
60-
"cluster_ca_certificate"
61-
]
62-
},
63-
{
36+
"required": [
37+
"client_key",
38+
"client_certificate"
39+
],
40+
"if": {
41+
"not": {
6442
"properties": {
6543
"insecure": {
6644
"const": true
6745
}
68-
},
69-
"required": [
70-
"insecure"
71-
]
46+
}
7247
}
73-
]
74-
},
75-
{
76-
"required": [
77-
"host"
78-
]
48+
},
49+
"then": {
50+
"required": [
51+
"client_ca_certificate"
52+
]
53+
}
7954
}
8055
]
8156
}
Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,120 @@
11
package kubernetes
22

33
import (
4-
"errors"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
88
"github.com/xeipuuv/gojsonschema"
99
)
1010

1111
func TestModule_KubernetesJSONSchema(t *testing.T) {
1212
tests := []struct {
13-
Case string
14-
wantErr error
15-
want bool
13+
title string
14+
Case string
15+
shouldBeValid bool
1616
}{
1717
{
18+
title: "TokenAuthPresent_InsecureTrue",
1819
Case: `{
1920
"host": "http://0.0.0.0:1234",
2021
"insecure": true,
2122
"token": "token"
2223
}`,
23-
wantErr: nil,
24-
want: true,
24+
shouldBeValid: true,
2525
},
2626
{
27+
title: "TokenAuthPresent_InsecureFalse",
28+
Case: `{
29+
"host": "http://0.0.0.0:1234",
30+
"insecure": false,
31+
"token": "foo"
32+
}`,
33+
shouldBeValid: true,
34+
},
35+
{
36+
title: "TokenAuthPresent_CertIsPresentToo",
2737
Case: `{
2838
"host": "http://0.0.0.0:1234",
2939
"insecure": false,
30-
"token": "token"
40+
"token": "token",
41+
"cluster_certificate": "c_ca_cert"
3142
}`,
32-
wantErr: nil,
33-
want: false,
43+
shouldBeValid: true,
3444
},
3545
{
46+
title: "CertAuthPresent_InsecureTrue",
3647
Case: `{
3748
"host": "http://0.0.0.0:1234",
38-
"insecure": false,
39-
"cluster_ca_certificate": "c_ca_cert",
40-
"token": "token"
49+
"insecure": true,
50+
"client_key": "c_key",
51+
"client_certificate": "c_cert"
4152
}`,
42-
wantErr: nil,
43-
want: true,
53+
shouldBeValid: true,
4454
},
4555
{
56+
title: "CertAuthPresent_InsecureFalse",
4657
Case: `{
4758
"host": "http://0.0.0.0:1234",
48-
"cluster_ca_certificate": "c_ca_cert",
49-
"token": "token"
59+
"insecure": false,
60+
"client_key": "c_key",
61+
"client_certificate": "c_cert"
5062
}`,
51-
wantErr: nil,
52-
want: true,
63+
shouldBeValid: false,
5364
},
65+
5466
{
67+
title: "CertAuthPresent_InsecureFalse_WithCACert",
5568
Case: `{
5669
"host": "http://0.0.0.0:1234",
57-
"insecure": true,
70+
"insecure": false,
5871
"client_key": "c_key",
59-
"client_certificate": "c_cert"
72+
"client_certificate": "c_cert",
73+
"client_ca_certificate": "ca_cert"
6074
}`,
61-
wantErr: nil,
62-
want: true,
75+
shouldBeValid: true,
6376
},
6477
{
65-
Case: ` "host": "http://0.0.0.0:1234",
66-
"insecure": true,
67-
"client_key": "c_key"
68-
}`,
69-
wantErr: nil,
70-
want: false,
78+
title: "Missing_ClientCert",
79+
Case: `{
80+
"host": "http://0.0.0.0:1234",
81+
"client_key": "c_key"
82+
}`,
83+
shouldBeValid: false,
7184
},
7285
{
86+
title: "Missing_ClientKey",
7387
Case: `{
7488
"host": "http://0.0.0.0:1234",
7589
"insecure": true,
76-
"token": "token",
77-
"client_key": "c_key",
7890
"client_certificate": "c_cert"
7991
}`,
80-
wantErr: nil,
81-
want: false,
92+
shouldBeValid: false,
8293
},
8394
{
95+
title: "Missing_CACert",
8496
Case: `{
8597
"host": "http://0.0.0.0:1234",
86-
"insecure": true,
87-
"token": "token",
88-
"client_key": "c_key"
98+
"insecure": false,
99+
"client_key": "foo",
100+
"client_certificate": "c_cert"
89101
}`,
90-
wantErr: nil,
91-
want: true,
102+
shouldBeValid: false,
92103
},
93104
}
94-
loader := gojsonschema.NewStringLoader(configSchema)
95-
schema, _ := gojsonschema.NewSchema(loader)
105+
106+
schema, err := gojsonschema.NewSchema(gojsonschema.NewStringLoader(configSchema))
107+
require.NoError(t, err)
96108

97109
for _, tt := range tests {
98110
tt := tt
99-
t.Run(tt.Case, func(t *testing.T) {
111+
t.Run(tt.title, func(t *testing.T) {
100112
t.Parallel()
101113

102114
c := gojsonschema.NewStringLoader(tt.Case)
103115
result, err := schema.Validate(c)
104-
if tt.wantErr != nil {
105-
assert.Error(t, err)
106-
assert.True(t, errors.Is(err, tt.wantErr))
107-
} else {
108-
assert.NoError(t, err)
109-
}
110-
assert.Equal(t, tt.want, result.Valid())
116+
require.NoError(t, err)
117+
assert.Equal(t, tt.shouldBeValid, result.Valid())
111118
})
112119
}
113120
}

0 commit comments

Comments
 (0)