-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from kaytu-io/feat/add-get-findings
fix: added checkctl get findings command
- Loading branch information
Showing
4 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package get | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/adorigi/checkctl/pkg/config" | ||
"github.com/adorigi/checkctl/pkg/request" | ||
"github.com/adorigi/checkctl/pkg/types" | ||
"github.com/adorigi/checkctl/pkg/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var findingsCmd = &cobra.Command{ | ||
Use: "findings", | ||
Short: "Get findings with the given filters", | ||
Long: `Get findings with the given filters`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
client := &http.Client{} | ||
configuration, err := config.ReadConfigFile() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// outputFormat := utils.ReadStringFlag(cmd, "output") | ||
// if outputFormat == "" { | ||
// outputFormat = configuration.OutputFormat | ||
// } | ||
|
||
benchmarkIDs, err := utils.ReadStringSliceFlag(cmd, "benchmark-id") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, ok := configuration.Benchmarks[benchmarkIDs[0]]; ok { | ||
fmt.Printf("Found stored Benchmark IDs %s", benchmarkIDs[0]) | ||
benchmarkIDs = configuration.Benchmarks[benchmarkIDs[0]] | ||
} | ||
|
||
integrationStr := utils.ReadStringFlag(cmd, "integration") | ||
if integrationStr == "" { | ||
fmt.Println(`Error: must specify integration | ||
Integration info in the form 'integration=AWS,id=123,id_name=name'`) | ||
return nil | ||
} | ||
|
||
integrationsStr, err := utils.ReadStringArrayFlag(cmd, "integration") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var integrations []types.IntegrationFilterInfo | ||
for _, integrationStr := range integrationsStr { | ||
if _, ok := configuration.Integrations[integrationStr]; ok { | ||
fmt.Printf("Found stored integration %s", integrationStr) | ||
integrationStr = configuration.Integrations[integrationStr] | ||
} | ||
integrations = append(integrations, types.ParseIntegrationInfo(integrationStr)) | ||
} | ||
|
||
requestPayload := types.FindingsRequestPayload{ | ||
Filters: types.FindingRequestFilters{ | ||
BenchmarkIDs: benchmarkIDs, | ||
Integrations: integrations, | ||
}, | ||
} | ||
|
||
payload, err := json.Marshal(requestPayload) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
url := "main/compliance/api/v3/findings" | ||
request, err := request.GenerateRequest( | ||
configuration.ApiKey, | ||
configuration.ApiEndpoint, | ||
"POST", | ||
url, | ||
payload, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
response, err := client.Do(request) | ||
if err != nil { | ||
return err | ||
} | ||
defer response.Body.Close() | ||
|
||
body, err := io.ReadAll(response.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if response.StatusCode != 200 { | ||
fmt.Println(string(body)) | ||
return nil | ||
} | ||
|
||
var findingsResponse types.FindingsResponse | ||
err = json.Unmarshal(body, &findingsResponse) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
js, err := json.MarshalIndent(findingsResponse, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Print(string(js)) | ||
|
||
return nil | ||
|
||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package types | ||
|
||
type FindingRequestFilters struct { | ||
BenchmarkIDs []string `json:"benchmark_id"` | ||
Integrations []IntegrationFilterInfo `json:"integration"` | ||
} | ||
|
||
type FindingsRequestPayload struct { | ||
Filters FindingRequestFilters `json:"filters"` | ||
} | ||
|
||
type FindingsResponse struct { | ||
Findings []Findings `json:"findings"` | ||
TotalCount int64 `json:"totalCount"` | ||
} | ||
|
||
type Findings struct { | ||
ID string `json:"id"` | ||
BenchmarkID string `json:"benchmarkID"` | ||
ControlID string `json:"controlID"` | ||
ConnectionID string `json:"connectionID"` | ||
EvaluatedAt int64 `json:"evaluatedAt"` | ||
StateActive bool `json:"stateActive"` | ||
ConformanceStatus string `json:"conformanceStatus"` | ||
Severity string `json:"severity"` | ||
Evaluator string `json:"evaluator"` | ||
Connector string `json:"connector"` | ||
KaytuResourceID string `json:"kaytuResourceID"` | ||
ResourceID string `json:"resourceID"` | ||
ResourceName string `json:"resourceName"` | ||
ResourceLocation string `json:"resourceLocation"` | ||
ResourceType string `json:"resourceType"` | ||
Reason string `json:"reason"` | ||
CostOptimization interface{} `json:"costOptimization"` | ||
ComplianceJobID int64 `json:"complianceJobID"` | ||
ParentComplianceJobID int64 `json:"parentComplianceJobID"` | ||
ParentBenchmarkReferences []string `json:"parentBenchmarkReferences"` | ||
ParentBenchmarks []string `json:"parentBenchmarks"` | ||
LastEvent string `json:"lastEvent"` | ||
ResourceTypeName string `json:"resourceTypeName"` | ||
ParentBenchmarkNames []string `json:"parentBenchmarkNames"` | ||
ControlTitle string `json:"controlTitle"` | ||
ProviderConnectionID string `json:"providerConnectionID"` | ||
ProviderConnectionName string `json:"providerConnectionName"` | ||
} |