Skip to content

Commit f25c72c

Browse files
authored
chore: re-add compliance enable/disable commands (#1106)
* chore: re-add compliance enable/disable commands Signed-off-by: Ross <[email protected]> * chore: fix goimports issues Signed-off-by: Ross <[email protected]> * chore: Address review comments. Update databox reports to Map to help simplify code Signed-off-by: Ross <[email protected]> * chore: fix goimports issues Signed-off-by: Ross <[email protected]> * chore: fix failing unit tests Signed-off-by: Ross <[email protected]> * chore: fix failing integration tests Signed-off-by: Ross <[email protected]> Signed-off-by: Ross <[email protected]>
1 parent 9571b91 commit f25c72c

21 files changed

+3655
-533
lines changed

api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ const (
130130
apiV2OrganizationInfo = "v2/OrganizationInfo"
131131

132132
apiSuppressions = "v2/suppressions/%s/allExceptions"
133+
134+
apiRecommendations = "v2/recommendations/%s"
133135
)
134136

135137
// WithApiV2 configures the client to use the API version 2 (/api/v2)

api/v2.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type V2Endpoints struct {
5959
Vulnerabilities *v2VulnerabilitiesService
6060
Alerts *AlertsService
6161
Suppressions *SuppressionsServiceV2
62+
Recommendations *RecommendationsServiceV2
6263
}
6364

6465
func NewV2Endpoints(c *Client) *V2Endpoints {
@@ -94,6 +95,11 @@ func NewV2Endpoints(c *Client) *V2Endpoints {
9495
&AzureSuppressionsV2{c},
9596
&GcpSuppressionsV2{c},
9697
},
98+
&RecommendationsServiceV2{c,
99+
&AwsRecommendationsV2{c},
100+
&AzureRecommendationsV2{c},
101+
&GcpRecommendationsV2{c},
102+
},
97103
}
98104

99105
v2.Schemas.Services = map[integrationSchema]V2Service{

api/v2_recommendations.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//
2+
// Author:: Ross Moles (<[email protected]>)
3+
// Copyright:: Copyright 2023, Lacework Inc.
4+
// License:: Apache License, Version 2.0
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
19+
package api
20+
21+
import (
22+
"fmt"
23+
)
24+
25+
// RecommendationsServiceV2 is a service that interacts with the V2 Recommendations
26+
// endpoints from the Lacework Server
27+
type RecommendationsServiceV2 struct {
28+
client *Client
29+
Aws recommendationServiceV2
30+
Azure recommendationServiceV2
31+
Gcp recommendationServiceV2
32+
}
33+
34+
type recommendationServiceV2 interface {
35+
List() ([]RecV2, error)
36+
Patch(recommendations RecommendationStateV2) (RecommendationResponseV2, error)
37+
GetReport(reportType string) ([]RecV2, error)
38+
}
39+
40+
type RecommendationTypeV2 string
41+
42+
const (
43+
AwsRecommendation RecommendationTypeV2 = "aws"
44+
AzureRecommendation RecommendationTypeV2 = "azure"
45+
GcpRecommendation RecommendationTypeV2 = "gcp"
46+
)
47+
48+
func (svc *RecommendationsServiceV2) list(cloudType RecommendationTypeV2) ([]RecV2, error) {
49+
var response RecommendationResponseV2
50+
err := svc.client.RequestDecoder("GET", fmt.Sprintf(apiRecommendations, cloudType), nil, &response)
51+
return response.RecommendationList(), err
52+
}
53+
54+
func (svc *RecommendationsServiceV2) patch(cloudType RecommendationTypeV2, recommendations RecommendationStateV2) (
55+
response RecommendationResponseV2,
56+
err error,
57+
) {
58+
err = svc.client.RequestEncoderDecoder("PATCH", fmt.Sprintf(apiRecommendations, cloudType), recommendations, &response)
59+
return
60+
}
61+
62+
type RecommendationStateV2 map[string]string
63+
64+
type RecommendationDataV2 map[string]RecommendationEnabledV2
65+
66+
type RecV2 struct {
67+
ID string
68+
State bool
69+
}
70+
71+
type RecommendationEnabledV2 struct {
72+
Enabled bool `json:"enabled"`
73+
}
74+
75+
type RecommendationResponseV2 struct {
76+
Data []RecommendationDataV2 `json:"data"`
77+
Ok bool `json:"ok"`
78+
Message string `json:"message"`
79+
}
80+
81+
func (res *RecommendationResponseV2) RecommendationList() (recommendations []RecV2) {
82+
if len(res.Data) > 0 {
83+
for k, v := range res.Data[0] {
84+
recommendations = append(recommendations, RecV2{k, v.Enabled})
85+
}
86+
}
87+
return
88+
}
89+
90+
type ReportSchema struct {
91+
Name string `json:"name"`
92+
RecommendationIDs map[string]string `json:"recommendationIDs"`
93+
}
94+
95+
func NewRecommendationV2State(recommendations []RecV2, state bool) RecommendationStateV2 {
96+
request := make(map[string]string)
97+
for _, rec := range recommendations {
98+
if state {
99+
request[rec.ID] = "enable"
100+
101+
} else {
102+
request[rec.ID] = "disable"
103+
}
104+
}
105+
return request
106+
}
107+
108+
func NewRecommendationV2(recommendations []RecV2) RecommendationStateV2 {
109+
request := make(map[string]string)
110+
for _, rec := range recommendations {
111+
if rec.State {
112+
request[rec.ID] = "enable"
113+
114+
} else {
115+
request[rec.ID] = "disable"
116+
}
117+
}
118+
return request
119+
}
120+
121+
// ReportStatus This is an experimental feature. Returned RecommendationID's are not guaranteed to be correct.
122+
func (res *RecommendationResponseV2) ReportStatus() map[string]bool {
123+
var recommendations = make(map[string]bool)
124+
125+
for _, rec := range res.RecommendationList() {
126+
recommendations[rec.ID] = rec.State
127+
}
128+
129+
return recommendations
130+
}
131+
132+
// filterRecommendations This is an experimental feature. Returned RecommendationID's are not guaranteed to be correct.
133+
func filterRecommendations(allRecommendations []RecV2, schema ReportSchema) []RecV2 {
134+
var recommendations []RecV2
135+
136+
for _, rec := range allRecommendations {
137+
_, ok := schema.RecommendationIDs[rec.ID]
138+
if ok {
139+
recommendations = append(recommendations, rec)
140+
}
141+
}
142+
return recommendations
143+
}

api/v2_recommendations_aws.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// Author:: Ross Moles (<[email protected]>)
3+
// Copyright:: Copyright 2023, Lacework Inc.
4+
// License:: Apache License, Version 2.0
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
19+
package api
20+
21+
import (
22+
"encoding/json"
23+
"errors"
24+
25+
"github.com/lacework/go-sdk/internal/databox"
26+
)
27+
28+
// AwsRecommendationsV2 is a service that interacts with the V2 Recommendations
29+
// endpoints from the Lacework Server
30+
type AwsRecommendationsV2 struct {
31+
client *Client
32+
}
33+
34+
func (svc *AwsRecommendationsV2) List() ([]RecV2, error) {
35+
return svc.client.V2.Recommendations.list(AwsRecommendation)
36+
}
37+
38+
func (svc *AwsRecommendationsV2) Patch(recommendations RecommendationStateV2) (RecommendationResponseV2, error) {
39+
return svc.client.V2.Recommendations.patch(AwsRecommendation, recommendations)
40+
}
41+
42+
// GetReport This is an experimental feature. Returned RecommendationID's are not guaranteed to be correct. Scoped to Lacework Account/Subaccount
43+
func (svc *AwsRecommendationsV2) GetReport(reportType string) ([]RecV2, error) {
44+
report := struct {
45+
Ids map[string]string `json:"recommendation_ids"`
46+
}{}
47+
48+
schemaBytes, ok := databox.Get("/reports/aws/cis.json")
49+
if !ok {
50+
return []RecV2{}, errors.New(
51+
"compliance report schema not found",
52+
)
53+
}
54+
55+
err := json.Unmarshal(schemaBytes, &report)
56+
if err != nil {
57+
return []RecV2{}, err
58+
}
59+
60+
schema := ReportSchema{reportType, report.Ids}
61+
62+
// fetch all aws recommendations
63+
allRecommendations, err := svc.client.V2.Recommendations.Aws.List()
64+
if err != nil {
65+
return []RecV2{}, err
66+
}
67+
filteredRecommendations := filterRecommendations(allRecommendations, schema)
68+
return filteredRecommendations, nil
69+
}

api/v2_recommendations_aws_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Author:: Darren Murray (<[email protected]>)
3+
// Copyright:: Copyright 2022, Lacework Inc.
4+
// License:: Apache License, Version 2.0
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
19+
package api_test
20+
21+
import (
22+
"fmt"
23+
"net/http"
24+
"testing"
25+
26+
"github.com/lacework/go-sdk/api"
27+
"github.com/lacework/go-sdk/internal/lacework"
28+
"github.com/stretchr/testify/assert"
29+
)
30+
31+
func TestRecommendationsAwsGetReport(t *testing.T) {
32+
var (
33+
expectedLen = 160
34+
fakeServer = lacework.MockServer()
35+
)
36+
37+
fakeServer.MockToken("TOKEN")
38+
fakeServer.UseApiV2()
39+
fakeServer.MockAPI("recommendations/aws",
40+
func(w http.ResponseWriter, r *http.Request) {
41+
assert.Equal(t, "GET", r.Method, "GetReport() should be a GET method")
42+
Recommendations := generateRecommendations()
43+
fmt.Fprintf(w, Recommendations)
44+
},
45+
)
46+
defer fakeServer.Close()
47+
48+
c, err := api.NewClient("test",
49+
api.WithToken("TOKEN"),
50+
api.WithURL(fakeServer.URL()),
51+
)
52+
assert.Nil(t, err)
53+
54+
response, err := c.V2.Recommendations.Aws.GetReport("CIS_1_1")
55+
assert.Nil(t, err)
56+
assert.NotNil(t, response)
57+
assert.Equal(t, expectedLen, len(response))
58+
for _, rec := range response {
59+
assert.NotEmpty(t, rec.ID)
60+
}
61+
}

api/v2_recommendations_azure.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//
2+
// Author:: Ross Moles (<[email protected]>)
3+
// Copyright:: Copyright 2023, Lacework Inc.
4+
// License:: Apache License, Version 2.0
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
19+
package api
20+
21+
import (
22+
"encoding/json"
23+
"errors"
24+
"fmt"
25+
26+
"github.com/lacework/go-sdk/internal/databox"
27+
)
28+
29+
// AzureRecommendationsV2 is a service that interacts with the V2 Recommendations
30+
// endpoints from the Lacework Server
31+
type AzureRecommendationsV2 struct {
32+
client *Client
33+
}
34+
35+
func (svc *AzureRecommendationsV2) List() ([]RecV2, error) {
36+
return svc.client.V2.Recommendations.list(AzureRecommendation)
37+
}
38+
39+
func (svc *AzureRecommendationsV2) Patch(recommendations RecommendationStateV2) (RecommendationResponseV2, error) {
40+
return svc.client.V2.Recommendations.patch(AzureRecommendation, recommendations)
41+
}
42+
43+
// GetReport This is an experimental feature. Returned RecommendationID's are not guaranteed to be correct. Scoped to Lacework Account/Subaccount
44+
func (svc *AzureRecommendationsV2) GetReport(reportType string) ([]RecV2, error) {
45+
var (
46+
schemaBytes []byte
47+
ok bool
48+
)
49+
report := struct {
50+
Ids map[string]string `json:"recommendation_ids"`
51+
}{}
52+
53+
switch reportType {
54+
case "CIS_1_0":
55+
schemaBytes, ok = databox.Get("/reports/azure/cis.json")
56+
if !ok {
57+
return []RecV2{}, errors.New(
58+
"compliance report schema not found",
59+
)
60+
}
61+
case "CIS_1_3_1":
62+
schemaBytes, ok = databox.Get("/reports/azure/cis_131.json")
63+
if !ok {
64+
return []RecV2{}, errors.New(
65+
"compliance report schema not found",
66+
)
67+
}
68+
default:
69+
return nil, fmt.Errorf("unable to find recommendations for report type %s", reportType)
70+
}
71+
72+
err := json.Unmarshal(schemaBytes, &report)
73+
if err != nil {
74+
return []RecV2{}, err
75+
}
76+
77+
schema := ReportSchema{reportType, report.Ids}
78+
79+
// fetch all azure recommendations
80+
allRecommendations, err := svc.client.V2.Recommendations.Azure.List()
81+
if err != nil {
82+
return []RecV2{}, err
83+
}
84+
filteredRecommendations := filterRecommendations(allRecommendations, schema)
85+
return filteredRecommendations, nil
86+
}

0 commit comments

Comments
 (0)