Skip to content

Commit 2f17a4d

Browse files
fix(gcp-project-policies): Use correct API to get Policy v3, fix policy 2.1 query (#7053)
<!-- 🎉 Thank you for making CloudQuery awesome by submitting a PR 🎉 --> #### Summary Reported on Discord https://discord.com/channels/872925471417962546/1065556442800734268/1065871250225909791 `projectsClient.GetIamPolicy` returns `v1.Policy` which has `log_type` as an enum, and as a result evaluated to an int in the destination database. Our query needs the strings, as they appear in `v3.Policy` (configured by the table). To get the `v3.Policy` we need the protobuf API. Additionally the check was wrong. We should verify all logging types are enabled for `allServices`. This is how it looks in GCP: ![image](https://user-images.githubusercontent.com/26760571/214112370-0badc1b4-4465-4b7e-973b-7f08f2a7fa57.png) <!-- Use the following steps to ensure your PR is ready to be reviewed - [ ] Read the [contribution guidelines](../blob/main/CONTRIBUTING.md) 🧑‍🎓 - [ ] Test locally on your own infrastructure - [ ] Run `go fmt` to format your code 🖊 - [ ] Lint your changes via `golangci-lint run` 🚨 (install golangci-lint [here](https://golangci-lint.run/usage/install/#local-installation)) - [ ] Update or add tests 🧪 - [ ] Ensure the status checks below are successful ✅ ---> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent dddd852 commit 2f17a4d

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

plugins/source/gcp/policies/queries/logging/not_configured_across_services_and_users.sql

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ WITH project_policy_audit_configs AS (SELECT project_id,
2929
audit_config ->> 'service' AS "service",
3030
jsonb_array_elements(audit_config -> 'auditLogConfigs') ->> 'logType' AS logs,
3131
jsonb_array_elements(audit_config -> 'auditLogConfigs') ->> 'exemptedMembers' AS exempted
32-
FROM project_policy_audit_configs)
32+
FROM project_policy_audit_configs),
33+
valid_log_types AS (SELECT project_id, service, count(*) as valid_types
34+
FROM log_types
35+
WHERE exempted IS NULL
36+
AND logs IN ('ADMIN_READ', 'DATA_READ', 'DATA_WRITE')
37+
AND service = 'allServices'
38+
GROUP BY project_id, service)
3339
SELECT service AS resource_id,
3440
:'execution_time'::timestamp AS execution_time,
3541
:'framework' AS framework,
@@ -38,10 +44,8 @@ SELECT service
3844
"project_id" AS project_id,
3945
CASE
4046
WHEN
41-
exempted IS NULL
42-
AND logs IN ('DATA_READ', 'DATA_WRITE')
43-
AND service = 'allServices'
44-
THEN 'fail'
45-
ELSE 'pass'
47+
valid_types = 3
48+
THEN 'pass'
49+
ELSE 'fail'
4650
END AS status
47-
FROM log_types;
51+
FROM valid_log_types;

plugins/source/gcp/resources/services/resourcemanager/project_policies_fetch.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,19 @@ package resourcemanager
33
import (
44
"context"
55

6-
"cloud.google.com/go/iam/apiv1/iampb"
7-
resourcemanager "cloud.google.com/go/resourcemanager/apiv3"
86
"github.com/cloudquery/plugin-sdk/schema"
97
"github.com/cloudquery/plugins/source/gcp/client"
8+
pb "google.golang.org/api/cloudresourcemanager/v3"
109
)
1110

1211
func fetchProjectPolicies(ctx context.Context, meta schema.ClientMeta, r *schema.Resource, res chan<- any) error {
1312
c := meta.(*client.Client)
14-
projectsClient, err := resourcemanager.NewProjectsClient(ctx, c.ClientOptions...)
13+
projectsClient, err := pb.NewService(ctx, c.ClientOptions...)
1514
if err != nil {
1615
return err
1716
}
18-
output, err := projectsClient.GetIamPolicy(
19-
ctx,
20-
&iampb.GetIamPolicyRequest{
21-
Resource: "projects/" + c.ProjectId,
22-
},
23-
)
17+
// We need to use the protobut client to get the current version of the policy struct (v3)
18+
output, err := projectsClient.Projects.GetIamPolicy("projects/"+c.ProjectId, &pb.GetIamPolicyRequest{}).Context(ctx).Do()
2419
if err != nil {
2520
return err
2621
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package resourcemanager
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/cloudquery/plugin-sdk/faker"
9+
"github.com/cloudquery/plugins/source/gcp/client"
10+
"github.com/julienschmidt/httprouter"
11+
pb "google.golang.org/api/cloudresourcemanager/v3"
12+
)
13+
14+
func createProjectPolicies(mux *httprouter.Router) error {
15+
var item pb.Policy
16+
if err := faker.FakeObject(&item); err != nil {
17+
return err
18+
}
19+
mux.POST("/v3/projects/testProject:getIamPolicy", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
20+
b, err := json.Marshal(&item)
21+
if err != nil {
22+
http.Error(w, "unable to marshal request: "+err.Error(), http.StatusBadRequest)
23+
return
24+
}
25+
if _, err := w.Write(b); err != nil {
26+
http.Error(w, "failed to write", http.StatusBadRequest)
27+
return
28+
}
29+
})
30+
return nil
31+
}
32+
33+
func TestProjectPolicies(t *testing.T) {
34+
client.MockTestRestHelper(t, ProjectPolicies(), createProjectPolicies, client.TestOptions{})
35+
}

0 commit comments

Comments
 (0)