Files
trufflehog/pkg/detectors/endpoint_customizer.go
Kashif KhanandMiccah Castorina 4b6957df66 Endpoint customizer refresh (#3308)
* Refresh EndpointCustomizer for more explicit configuration

Also add CloudProvider interface.

* WIP: Update EndpointSetter

* Updated detectors with new endpoint customizer

* Fixed linter

* Added check for appending cloud endpoints

---------

Co-authored-by: Miccah Castorina <[email protected]>
2024-09-24 11:41:05 -05:00

53 lines
1.4 KiB
Go

package detectors
import (
"fmt"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
)
// EndpointSetter implements a sensible default for the SetEndpoints function
// of the EndpointCustomizer interface. A detector can embed this struct to
// gain the functionality.
type EndpointSetter struct {
configuredEndpoints []string
cloudEndpoint string
useCloudEndpoint bool
useFoundEndpoints bool
}
func (e *EndpointSetter) SetConfiguredEndpoints(userConfiguredEndpoints ...string) error {
if len(userConfiguredEndpoints) == 0 {
return fmt.Errorf("at least one endpoint required")
}
deduped := make([]string, 0, len(userConfiguredEndpoints))
for _, endpoint := range userConfiguredEndpoints {
common.AddStringSliceItem(endpoint, &deduped)
}
e.configuredEndpoints = deduped
return nil
}
func (e *EndpointSetter) SetCloudEndpoint(url string) {
e.cloudEndpoint = url
}
func (e *EndpointSetter) UseCloudEndpoint(enabled bool) {
e.useCloudEndpoint = enabled
}
func (e *EndpointSetter) UseFoundEndpoints(enabled bool) {
e.useFoundEndpoints = enabled
}
func (e *EndpointSetter) Endpoints(foundEndpoints ...string) []string {
endpoints := e.configuredEndpoints
if e.useCloudEndpoint && e.cloudEndpoint != "" {
endpoints = append(endpoints, e.cloudEndpoint)
}
if e.useFoundEndpoints {
endpoints = append(endpoints, foundEndpoints...)
}
return endpoints
}