Skip to content

Commit 30090e8

Browse files
authored
fix: Add descriptions to spec JSON schema (#14671)
Closes #13999
1 parent 3005db7 commit 30090e8

9 files changed

Lines changed: 413 additions & 273 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package spec
2+
3+
import "google.golang.org/api/cloudresourcemanager/v3"
4+
5+
type CredentialsConfig struct {
6+
// The email address of the service account to impersonate.
7+
TargetPrincipal string `json:"target_principal" jsonschema:"required,format=email"`
8+
9+
// Scopes that the impersonated credential should have.
10+
//
11+
// See available scopes in the [documentation](https://developers.google.com/identity/protocols/oauth2/scopes).
12+
Scopes []string `json:"scopes" jsonschema:"pattern=^https://www.googleapis.com/auth/(.)+$,default=https://www.googleapis.com/auth/cloud-platform"`
13+
14+
// Delegates are the service account email addresses in a delegation chain.
15+
// Each service account must be granted `roles/iam.serviceAccountTokenCreator` on the next service account in the chain.
16+
Delegates []string `json:"delegates" jsonschema:"format=email"`
17+
18+
// The subject field of a JWT (`sub`).
19+
// This field should only be set if you wish to impersonate a user.
20+
// This feature is useful when using domain wide delegation.
21+
Subject string `json:"subject" jsonschema:"minLength=1"`
22+
}
23+
24+
func (c *CredentialsConfig) SetDefaults() {
25+
if c == nil {
26+
return
27+
}
28+
if len(c.Scopes) == 0 {
29+
// `https://www.googleapis.com/auth/cloud-platform`
30+
// We use this as some APIs don't utilize the read-only alternative `https://www.googleapis.com/auth/cloud-platform.read-only`
31+
// See https://developers.google.com/identity/protocols/oauth2/scopes for more details.
32+
c.Scopes = []string{cloudresourcemanager.CloudPlatformScope}
33+
}
34+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package spec
2+
3+
import (
4+
"testing"
5+
6+
"github.com/cloudquery/codegen/jsonschema"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestCredentialsConfigJSONSchema(t *testing.T) {
11+
schema, err := jsonschema.Generate(CredentialsConfig{})
12+
require.NoError(t, err)
13+
14+
jsonschema.TestJSONSchema(t, string(schema), []jsonschema.TestCase{
15+
{
16+
Name: "empty",
17+
Err: true, // missing target_principal
18+
Spec: `{}`,
19+
},
20+
{
21+
Name: "empty target_principal",
22+
Err: true,
23+
Spec: `{"target_principal":""}`,
24+
},
25+
{
26+
Name: "null target_principal",
27+
Err: true,
28+
Spec: `{"target_principal":null}`,
29+
},
30+
{
31+
Name: "bad target_principal",
32+
Err: true,
33+
Spec: `{"target_principal":123}`,
34+
},
35+
{
36+
Name: "bad target_principal format",
37+
Err: true,
38+
Spec: `{"target_principal":"some"}`,
39+
},
40+
{
41+
Name: "proper target_principal",
42+
Spec: `{"target_principal":"a@some"}`,
43+
},
44+
{
45+
Name: "extra field",
46+
Err: true,
47+
Spec: `{"target_principal":"a@some","extra":true}`,
48+
},
49+
{
50+
Name: "empty scopes",
51+
Spec: `{"target_principal":"a@some", "scopes":[]}`,
52+
},
53+
{
54+
Name: "null scopes",
55+
Spec: `{"target_principal":"a@some", "scopes":null}`,
56+
},
57+
{
58+
Name: "bad scopes",
59+
Err: true,
60+
Spec: `{"target_principal":"a@some", "scopes":123}`,
61+
},
62+
{
63+
Name: "empty scopes entry",
64+
Err: true,
65+
Spec: `{"target_principal":"a@some", "scopes":[""]}`,
66+
},
67+
{
68+
Name: "null scopes entry",
69+
Err: true,
70+
Spec: `{"target_principal":"a@some", "scopes":[null]}`,
71+
},
72+
{
73+
Name: "bad scopes entry",
74+
Err: true,
75+
Spec: `{"target_principal":"a@some", "scopes":[123]}`,
76+
},
77+
{
78+
Name: "bad scopes entry format",
79+
Err: true,
80+
Spec: `{"target_principal":"a@some", "scopes":["https://www.g00gleapis.com/auth/cloud-platform"]}`,
81+
},
82+
{
83+
Name: "proper scopes entry",
84+
Spec: `{"target_principal":"a@some", "scopes":["https://www.googleapis.com/auth/cloud-platform.read-only"]}`,
85+
},
86+
87+
{
88+
Name: "empty delegates",
89+
Spec: `{"target_principal":"a@some", "delegates":[]}`,
90+
},
91+
{
92+
Name: "null delegates",
93+
Spec: `{"target_principal":"a@some", "delegates":null}`,
94+
},
95+
{
96+
Name: "bad delegates",
97+
Err: true,
98+
Spec: `{"target_principal":"a@some", "delegates":123}`,
99+
},
100+
{
101+
Name: "empty delegates entry",
102+
Err: true,
103+
Spec: `{"target_principal":"a@some", "delegates":[""]}`,
104+
},
105+
{
106+
Name: "null delegates entry",
107+
Err: true,
108+
Spec: `{"target_principal":"a@some", "delegates":[null]}`,
109+
},
110+
{
111+
Name: "bad delegates entry",
112+
Err: true,
113+
Spec: `{"target_principal":"a@some", "delegates":[123]}`,
114+
},
115+
{
116+
Name: "bad delegates entry format",
117+
Err: true,
118+
Spec: `{"target_principal":"a@some", "delegates":["abc"]}`,
119+
},
120+
{
121+
Name: "proper delegates entry",
122+
Spec: `{"target_principal":"a@some", "delegates":["a@some"]}`,
123+
},
124+
{
125+
Name: "empty subject",
126+
Err: true,
127+
Spec: `{"target_principal":"a@some", "subject":""}`,
128+
},
129+
{
130+
Name: "null subject",
131+
Err: true,
132+
Spec: `{"target_principal":"a@some", "subject":null}`,
133+
},
134+
{
135+
Name: "bad subject",
136+
Err: true,
137+
Spec: `{"target_principal":"a@some", "subject":123}`,
138+
},
139+
{
140+
Name: "proper subject",
141+
Spec: `{"target_principal":"a@some", "subject":"some"}`,
142+
},
143+
})
144+
}

plugins/source/gcp/client/spec/gen/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212

1313
func main() {
1414
fmt.Println("Generating JSON schema for plugin spec")
15-
jsonschema.GenerateIntoFile(new(spec.Spec), path.Join(currDir(), "..", "schema.json"))
15+
jsonschema.GenerateIntoFile(new(spec.Spec), path.Join(currDir(), "..", "schema.json"),
16+
jsonschema.WithAddGoComments("github.com/cloudquery/cloudquery/plugins/source/gcp/client/spec", path.Join(currDir(), "..")),
17+
)
1618
}
1719

1820
func currDir() string {

plugins/source/gcp/client/spec/schema.json

Lines changed: 41 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)