Skip to content

Commit

Permalink
Merge pull request #18 from kaytu-io/feat/add-get-findings
Browse files Browse the repository at this point in the history
fix: added checkctl get findings command
  • Loading branch information
ADorigi authored Sep 18, 2024
2 parents fd75c41 + 826ce3b commit dfc90c2
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/get/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var controlsCmd = &cobra.Command{
}

if _, ok := configuration.Benchmarks[benchmarkIDs[0]]; ok {
fmt.Printf("Found stored integration %s", benchmarkIDs[0])
fmt.Printf("Found stored Benchmark %s", benchmarkIDs[0])
benchmarkIDs = configuration.Benchmarks[benchmarkIDs[0]]
}

Expand Down
119 changes: 119 additions & 0 deletions cmd/get/findings.go
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

},
}
5 changes: 5 additions & 0 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ func init() {
GetCmd.AddCommand(jobsCmd)
jobsCmd.PersistentFlags().String("job-type", "", "Job Type. Options: compliance, analytics, discovery")
jobsCmd.PersistentFlags().String("interval", "90m", "Specify time interval like: 90m, 1h, 50 minutes, 2 hours")

GetCmd.AddCommand(findingsCmd)
findingsCmd.PersistentFlags().StringArray("integration", []string{}, "Integration info in the form 'integration=AWS,id=123,id_name=name'"+
"values are optional and support regex")
findingsCmd.PersistentFlags().StringSlice("benchmark-id", []string{}, "Benchmark ID")
}
45 changes: 45 additions & 0 deletions pkg/types/findings.go
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"`
}

0 comments on commit dfc90c2

Please sign in to comment.