Skip to content

Commit e3feef1

Browse files
🧹 Add missing init functions for gcp (#5826)
* Add init for keyring * Add init functions for sql * Add in DNS * Add generated lr files * Fix mangagedzone query
1 parent 74bc333 commit e3feef1

File tree

5 files changed

+181
-7
lines changed

5 files changed

+181
-7
lines changed

providers/gcp/resources/dns.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,56 @@ import (
1919
"google.golang.org/api/option"
2020
)
2121

22+
func initGcpProjectDnsServiceManagedzone(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
23+
if len(args) > 3 {
24+
return args, nil, nil
25+
}
26+
27+
if len(args) == 0 {
28+
if args == nil {
29+
args = make(map[string]*llx.RawData)
30+
}
31+
if ids := getAssetIdentifier(runtime); ids != nil {
32+
args["name"] = llx.StringData(ids.name)
33+
args["projectId"] = llx.StringData(ids.project)
34+
} else {
35+
return nil, nil, errors.New("no asset identifier found")
36+
}
37+
}
38+
39+
// Create the parent DNS service and find the specific managed zone
40+
obj, err := CreateResource(runtime, "gcp.project.dnsService", map[string]*llx.RawData{
41+
"projectId": args["projectId"],
42+
})
43+
if err != nil {
44+
return nil, nil, err
45+
}
46+
dnsSvc := obj.(*mqlGcpProjectDnsService)
47+
managedzones := dnsSvc.GetManagedZones()
48+
if managedzones.Error != nil {
49+
return nil, nil, managedzones.Error
50+
}
51+
52+
// Find the matching managed zone
53+
for _, mz := range managedzones.Data {
54+
managedzone := mz.(*mqlGcpProjectDnsServiceManagedzone)
55+
id := managedzone.GetId()
56+
if id.Error != nil {
57+
return nil, nil, id.Error
58+
}
59+
projectId := managedzone.GetProjectId()
60+
if projectId.Error != nil {
61+
return nil, nil, projectId.Error
62+
}
63+
64+
if id.Data == args["name"].Value && projectId.Data == args["projectId"].Value {
65+
return args, managedzone, nil
66+
}
67+
}
68+
69+
return nil, nil, errors.New("DNS managed zone not found")
70+
}
71+
2272
type mqlGcpProjectDnsServiceInternal struct {
2373
serviceEnabled bool
2474
}

providers/gcp/resources/gcp.lr.go

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

providers/gcp/resources/gcp.lr.manifest.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,6 @@ resources:
289289
refs:
290290
- title: Introduction to tables
291291
url: https://cloud.google.com/bigquery/docs/tables-intro
292-
platform:
293-
name:
294-
- gcp
295292
gcp.project.binaryAuthorizationControl:
296293
fields:
297294
policy: {}

providers/gcp/resources/kms.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,61 @@ func initGcpProjectKmsService(runtime *plugin.Runtime, args map[string]*llx.RawD
5050
return args, nil, nil
5151
}
5252

53+
func initGcpProjectKmsServiceKeyring(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
54+
if len(args) > 3 {
55+
return args, nil, nil
56+
}
57+
58+
if len(args) == 0 {
59+
if args == nil {
60+
args = make(map[string]*llx.RawData)
61+
}
62+
if ids := getAssetIdentifier(runtime); ids != nil {
63+
args["name"] = llx.StringData(ids.name)
64+
args["location"] = llx.StringData(ids.region)
65+
args["projectId"] = llx.StringData(ids.project)
66+
} else {
67+
return nil, nil, errors.New("no asset identifier found")
68+
}
69+
}
70+
71+
// Create the parent KMS service and find the specific keyring
72+
obj, err := CreateResource(runtime, "gcp.project.kmsService", map[string]*llx.RawData{
73+
"projectId": args["projectId"],
74+
})
75+
if err != nil {
76+
return nil, nil, err
77+
}
78+
kmsSvc := obj.(*mqlGcpProjectKmsService)
79+
keyrings := kmsSvc.GetKeyrings()
80+
if keyrings.Error != nil {
81+
return nil, nil, keyrings.Error
82+
}
83+
84+
// Find the matching keyring
85+
for _, kr := range keyrings.Data {
86+
keyring := kr.(*mqlGcpProjectKmsServiceKeyring)
87+
name := keyring.GetName()
88+
if name.Error != nil {
89+
return nil, nil, name.Error
90+
}
91+
location := keyring.GetLocation()
92+
if location.Error != nil {
93+
return nil, nil, location.Error
94+
}
95+
projectId := keyring.GetProjectId()
96+
if projectId.Error != nil {
97+
return nil, nil, projectId.Error
98+
}
99+
100+
if name.Data == args["name"].Value && location.Data == args["location"].Value && projectId.Data == args["projectId"].Value {
101+
return args, keyring, nil
102+
}
103+
}
104+
105+
return nil, nil, errors.New("KMS keyring not found")
106+
}
107+
53108
func (g *mqlGcpProject) kms() (*mqlGcpProjectKmsService, error) {
54109
if g.Id.Error != nil {
55110
return nil, g.Id.Error

providers/gcp/resources/sql.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package resources
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910

1011
"go.mondoo.com/cnquery/v11/llx"
@@ -19,6 +20,77 @@ import (
1920
"google.golang.org/api/sqladmin/v1"
2021
)
2122

23+
func initGcpProjectSqlService(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
24+
if len(args) > 0 {
25+
return args, nil, nil
26+
}
27+
28+
conn, ok := runtime.Connection.(*connection.GcpConnection)
29+
if !ok {
30+
return nil, nil, errors.New("invalid connection provided, it is not a GCP connection")
31+
}
32+
33+
projectId := conn.ResourceID()
34+
args["projectId"] = llx.StringData(projectId)
35+
36+
return args, nil, nil
37+
}
38+
39+
func initGcpProjectSqlServiceInstance(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
40+
if len(args) > 3 {
41+
return args, nil, nil
42+
}
43+
44+
if len(args) == 0 {
45+
if args == nil {
46+
args = make(map[string]*llx.RawData)
47+
}
48+
if ids := getAssetIdentifier(runtime); ids != nil {
49+
args["name"] = llx.StringData(ids.name)
50+
args["region"] = llx.StringData(ids.region)
51+
args["projectId"] = llx.StringData(ids.project)
52+
} else {
53+
return nil, nil, errors.New("no asset identifier found")
54+
}
55+
}
56+
57+
// Create the parent SQL service and find the specific instance
58+
obj, err := CreateResource(runtime, "gcp.project.sqlService", map[string]*llx.RawData{
59+
"projectId": args["projectId"],
60+
})
61+
if err != nil {
62+
return nil, nil, err
63+
}
64+
sqlSvc := obj.(*mqlGcpProjectSqlService)
65+
instances := sqlSvc.GetInstances()
66+
if instances.Error != nil {
67+
return nil, nil, instances.Error
68+
}
69+
70+
// Find the matching instance
71+
for _, inst := range instances.Data {
72+
instance := inst.(*mqlGcpProjectSqlServiceInstance)
73+
name := instance.GetName()
74+
if name.Error != nil {
75+
return nil, nil, name.Error
76+
}
77+
projectId := instance.GetProjectId()
78+
if projectId.Error != nil {
79+
return nil, nil, projectId.Error
80+
}
81+
instanceRegion := instance.GetRegion()
82+
if instanceRegion.Error != nil {
83+
return nil, nil, instanceRegion.Error
84+
}
85+
86+
if instanceRegion.Data == args["region"].Value && name.Data == args["name"].Value && projectId.Data == args["projectId"].Value {
87+
return args, instance, nil
88+
}
89+
}
90+
91+
return nil, nil, errors.New("SQL instance not found")
92+
}
93+
2294
func (g *mqlGcpProjectSqlService) id() (string, error) {
2395
if g.ProjectId.Error != nil {
2496
return "", g.ProjectId.Error

0 commit comments

Comments
 (0)