Skip to content

Commit

Permalink
✨ Add reference to compute network from GCP DNS policy (#799)
Browse files Browse the repository at this point in the history
```typescript
./cnquery run gcp -c "gcp.project.dns.policies{networks}"                                                                                                                                                                              
→ discover related assets for 1 asset(s)
→ resolved assets resolved-assets=1
gcp.project.dns.policies: [
  0: {
    networks: [
  0: gcp.project.computeService.network name="default"
    ]
  }
]
```

Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored Jan 18, 2023
1 parent f59423b commit 0e6c47b
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 3 deletions.
45 changes: 45 additions & 0 deletions resources/packs/gcp/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gcp
import (
"context"
"strconv"
"strings"

"go.mondoo.com/cnquery/resources"

Expand Down Expand Up @@ -172,13 +173,20 @@ func (g *mqlGcpProjectDnsService) GetPolicies() ([]interface{}, error) {
for i := range page.Policies {
policy := page.Policies[i]

networkNames := make([]interface{}, 0, len(policy.Networks))
for _, network := range policy.Networks {
segments := strings.Split(network.NetworkUrl, "/")
networkNames = append(networkNames, segments[len(segments)-1])
}

mqlDnsPolicy, err := g.MotorRuntime.CreateResource("gcp.project.dnsService.policy",
"projectId", projectId,
"id", strconv.FormatInt(int64(policy.Id), 10),
"name", policy.Name,
"description", policy.Description,
"enableInboundForwarding", policy.EnableInboundForwarding,
"enableLogging", policy.EnableLogging,
"networkNames", networkNames,
)
if err != nil {
return err
Expand All @@ -193,6 +201,43 @@ func (g *mqlGcpProjectDnsService) GetPolicies() ([]interface{}, error) {
return res, nil
}

func (g *mqlGcpProjectDnsServicePolicy) GetNetworks() ([]interface{}, error) {
projectId, err := g.ProjectId()
if err != nil {
return nil, err
}

networkNames, err := g.NetworkNames()
if err != nil {
return nil, err
}

obj, err := g.MotorRuntime.CreateResource("gcp.project.computeService", "projectId", projectId)
if err != nil {
return nil, err
}
gcpCompute := obj.(GcpProjectComputeService)
networks, err := gcpCompute.Networks()
if err != nil {
return nil, err
}

res := make([]interface{}, 0, len(networkNames))
for _, network := range networks {
networkName, err := network.(GcpProjectComputeServiceNetwork).Name()
if err != nil {
return nil, err
}
for _, name := range networkNames {
if name == networkName {
res = append(res, network)
break
}
}
}
return res, nil
}

func (g *mqlGcpProjectDnsServiceRecordset) id() (string, error) {
projectId, err := g.ProjectId()
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion resources/packs/gcp/gcp.lr
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ private gcp.project.dnsService.recordset @defaults("name") {
}

// Cloud DNS rules applied to one or more Virtual Private Cloud resources
private gcp.project.dnsService.policy {
private gcp.project.dnsService.policy @defaults("name") {
// Project ID
projectId string
// Managed Zone ID
Expand All @@ -1217,6 +1217,10 @@ private gcp.project.dnsService.policy {
enableInboundForwarding bool
// Indicates if logging is enabled
enableLogging bool
// List of network names specifying networks to which this policy is applied
networkNames []string
// List of networks to which this policy is applied
networks() []gcp.project.computeService.network
}

// GCP GKE
Expand Down
78 changes: 78 additions & 0 deletions resources/packs/gcp/gcp.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion resources/packs/gcp/info/gcp.lr.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion resources/packs/k8s/k8s.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e6c47b

Please sign in to comment.