Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
add gitleaks secret scanner (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
fishkerez authored Dec 15, 2022
1 parent 938f7ad commit 736bd8f
Show file tree
Hide file tree
Showing 15 changed files with 390 additions and 22 deletions.
15 changes: 6 additions & 9 deletions .families.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sbom:
enabled: true
enabled: false
analyzers_list:
- "syft"
- "gomod"
Expand All @@ -26,7 +26,7 @@ sbom:
token: "token"

vulnerabilities:
enabled: true
enabled: false
scanners_list:
- "grype"
inputs:
Expand Down Expand Up @@ -61,11 +61,8 @@ secrets:
scanners_list:
- "gitleaks"
inputs:
- input: "/dir"
- input: "./"
input_type: "dir"
- input: "/rootfs"
input_type: "rootfs"
gitleaks_config:
foo: "bar"


scanners_config:
gitleaks:
binary_path: "/usr/local/bin/gitleaks"
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ check: lint test ## Run tests and linters
gomod-tidy:
cd backend && go mod tidy
cd runtime_scan && go mod tidy
cd shared && go mod tidy

.PHONY: api
api: ## Generating API code
Expand Down
14 changes: 14 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/openclarity/vmclarity/shared/pkg/families"
"github.com/openclarity/vmclarity/shared/pkg/families/results"
"github.com/openclarity/vmclarity/shared/pkg/families/sbom"
"github.com/openclarity/vmclarity/shared/pkg/families/secrets"
"github.com/openclarity/vmclarity/shared/pkg/families/vulnerabilities"
)

Expand Down Expand Up @@ -78,6 +79,19 @@ var rootCmd = &cobra.Command{
}
}

if config.Secrets.Enabled {
secretsResults, err := results.GetResult[*secrets.Results](res)
if err != nil {
return fmt.Errorf("failed to get secrets results: %v", err)
}

bytes, _ := json.Marshal(secretsResults)
err = Output(bytes, "secrets")
if err != nil {
return fmt.Errorf("failed to output secrets results: %v", err)
}
}

return nil
},
}
Expand Down
4 changes: 2 additions & 2 deletions shared/pkg/families/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func New(logger *log.Entry, config *Config) *Manager {
func (m *Manager) Run() (*results.Results, error) {
familiesResults := results.New()

for _, analyzer := range m.families {
ret, err := analyzer.Run(familiesResults)
for _, family := range m.families {
ret, err := family.Run(familiesResults)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions shared/pkg/families/sbom/family.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ import (
log "github.com/sirupsen/logrus"

_interface "github.com/openclarity/vmclarity/shared/pkg/families/interface"
"github.com/openclarity/vmclarity/shared/pkg/families/results"
familiesresults "github.com/openclarity/vmclarity/shared/pkg/families/results"
)

type SBOM struct {
logger *log.Entry
conf Config
}

func (s SBOM) Run(res *results.Results) (_interface.IsResults, error) {
func (s SBOM) Run(res *familiesresults.Results) (_interface.IsResults, error) {
s.logger.Info("SBOM Run...")

if len(s.conf.Inputs) == 0 {
Expand Down
26 changes: 26 additions & 0 deletions shared/pkg/families/secrets/common/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright © 2022 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

import (
gitleaksconfig "github.com/openclarity/vmclarity/shared/pkg/families/secrets/gitleaks/config"
)

type ScannersConfig struct {
Gitleaks gitleaksconfig.Config `yaml:"gitleaks" mapstructure:"gitleaks"`
}

func (ScannersConfig) IsConfig() {}
65 changes: 65 additions & 0 deletions shared/pkg/families/secrets/common/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright © 2022 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

// Results for now will be as the gitleaks results struct since it is our only secret scanner.
// once another secret scanner is integrated, we will need to think of a common scheme.
type Results struct {
Findings []Findings
Source string
ScannerName string
Error error
}

type Findings struct {
Description string `json:"description"`
StartLine int `json:"start_line"`
EndLine int `json:"end_line"`
StartColumn int `json:"start_column"`
EndColumn int `json:"end_column"`

Line string `json:"-" json:"line"`

Match string `json:"match"`

// Secret contains the full content of what is matched in
// the tree-sitter query.
Secret string `json:"secret"`

// File is the name of the file containing the finding
File string `json:"file"`
SymlinkFile string `json:"symlink_file"`
Commit string `json:"commit"`

// Entropy is the shannon entropy of Value
Entropy float32 `json:"entropy"`

Author string `json:"author"`
Email string `json:"email"`
Date string `json:"date"`
Message string `json:"message"`
Tags []string `json:"tags"`

// Rule is the name of the rule that was matched
RuleID string `json:"rule_id"`

// unique identifer
Fingerprint string `json:"fingerprint"`
}

func (r *Results) GetError() error {
return r.Error
}
11 changes: 8 additions & 3 deletions shared/pkg/families/secrets/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@

package secrets

import (
"github.com/openclarity/vmclarity/shared/pkg/families/secrets/common"
)

type Config struct {
Enabled bool `yaml:"enabled" mapstructure:"enabled"`
ScannersList []string `yaml:"scanners_list" mapstructure:"scanners_list"`
Inputs []Inputs `yaml:"inputs" mapstructure:"inputs"`
Enabled bool `yaml:"enabled" mapstructure:"enabled"`
ScannersList []string `yaml:"scanners_list" mapstructure:"scanners_list"`
Inputs []Inputs `yaml:"inputs" mapstructure:"inputs"`
ScannersConfig *common.ScannersConfig `yaml:"scanners_config" mapstructure:"scanners_config"`
}

type Inputs struct {
Expand Down
36 changes: 30 additions & 6 deletions shared/pkg/families/secrets/family.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,50 @@
package secrets

import (
"fmt"

log "github.com/sirupsen/logrus"

_interface "github.com/openclarity/vmclarity/shared/pkg/families/interface"
"github.com/openclarity/vmclarity/shared/pkg/families/results"
"github.com/openclarity/kubeclarity/shared/pkg/job_manager"
"github.com/openclarity/kubeclarity/shared/pkg/utils"
familiesinterface "github.com/openclarity/vmclarity/shared/pkg/families/interface"
familiesresults "github.com/openclarity/vmclarity/shared/pkg/families/results"
"github.com/openclarity/vmclarity/shared/pkg/families/secrets/common"
"github.com/openclarity/vmclarity/shared/pkg/families/secrets/job"
)

type Secrets struct {
conf Config
logger *log.Entry
}

func (s Secrets) Run(res *results.Results) (_interface.IsResults, error) {
//TODO implement me
func (s Secrets) Run(res *familiesresults.Results) (familiesinterface.IsResults, error) {
s.logger.Info("Secrets Run...")

manager := job_manager.New(s.conf.ScannersList, s.conf.ScannersConfig, s.logger, job.Factory)
mergedResults := NewMergedResults()

for _, input := range s.conf.Inputs {
results, err := manager.Run(utils.SourceType(input.InputType), input.Input)
if err != nil {
return nil, fmt.Errorf("failed to scan input %q for secrets: %v", input.Input, err)
}

// Merge results.
for name, result := range results {
s.logger.Infof("Merging result from %q", name)
mergedResults = mergedResults.Merge(result.(*common.Results)) // nolint:forcetypeassert
}
}

s.logger.Info("Secrets Done...")
return &Results{}, nil
return &Results{
MergedResults: mergedResults,
}, nil
}

// ensure types implement the requisite interfaces
var _ _interface.Family = &Secrets{}
var _ familiesinterface.Family = &Secrets{}

func New(logger *log.Entry, conf Config) *Secrets {
return &Secrets{
Expand Down
20 changes: 20 additions & 0 deletions shared/pkg/families/secrets/gitleaks/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright © 2022 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package config

type Config struct {
BinaryPath string `yaml:"binary_path" mapstructure:"binary_path"`
}
Loading

0 comments on commit 736bd8f

Please sign in to comment.