| 1 | # Detect secrets |
| 2 | |
| 3 | This project it a rewrite of <https://github.com/Yelp/detect-secrets> in Go. |
| 4 | |
| 5 | It is meant to be imported in other projects and uses as a Secret Detection Engine - given a piece of data, determine whether data contains a secret. |
| 6 | |
| 7 | ## How to use |
| 8 | |
| 9 | In order to use it in another project, get the go module: |
| 10 | |
| 11 | ```shell |
| 12 | go get github.com/DefangLabs/secret-detector |
| 13 | ``` |
| 14 | |
| 15 | Then you can import it in your Go code and use it: |
| 16 | |
| 17 | ```go |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "io" |
| 21 | "github.com/DefangLabs/secret-detector/pkg/scanner" |
| 22 | ) |
| 23 | |
| 24 | func main() { |
| 25 | scanner := scanner.NewDefaultScanner() |
| 26 | |
| 27 | // scanner input can be a file path |
| 28 | detectedSecrets, err := scanner.ScanFile("path/to/file") |
| 29 | // or an io.Reader |
| 30 | var in io.Reader |
| 31 | detectedSecrets, err := scanner.ScanReader(in) |
| 32 | // or just a simple string |
| 33 | var secrets string |
| 34 | detectedSecrets, err := scanner.Scan(secrets) |
| 35 | |
| 36 | // print the results |
| 37 | for secret := range detectedSecrets { |
| 38 | fmt.Printf("Secret of type '%s' found in '%s'\n", d.Type, d.Key) |
| 39 | } |
| 40 | } |
| 41 | ``` |
| 42 | |
| 43 | ## How it works |
| 44 | |
| 45 | The tool has a plugin-based architecture. The `scanner.Scanner` has a collection of plugins, and it feeds the data to each plugin, combining the results. |
| 46 | |
| 47 | There are two types of plugins: |
| 48 | |
| 49 | ### Transformer |
| 50 | ```go |
| 51 | type Transformer interface { |
| 52 | Transform(in string) (map[string]string, bool) |
| 53 | SupportedFormats() []dataformat.DataFormat |
| 54 | SupportFiles() bool |
| 55 | } |
| 56 | ``` |
| 57 | Transformer receives a `string` input and tries to convert it into a key-value `map[string]string`. Each transformer supports a different data structure, like `yamltransformer`, `jsontransformer`, etc. |
| 58 | |
| 59 | ### Detector |
| 60 | ```go |
| 61 | type Detector interface { |
| 62 | Scan(in string) ([]DetectedSecret, error) |
| 63 | ScanMap(keyValueMap map[string]string) ([]DetectedSecret, error) |
| 64 | SecretType() string |
| 65 | } |
| 66 | ``` |
| 67 | Detector receives an input `string` or a key-value 'map[string]string', and will try to find secrets in it. |
| 68 | |
| 69 | Each detector look for a different type of secret. |
| 70 | For example, `keyword.detector` checks for keywords like `password` or `api_key`. |
| 71 | `jwt.detector` checks whether a value is a valid JWT. |
| 72 | |
| 73 | ## Load Scanner using a config file |
| 74 | ```go |
| 75 | import ( |
| 76 | "github.com/DefangLabs/secret-detector/pkg/scanner" |
| 77 | ) |
| 78 | |
| 79 | func main() { |
| 80 | // load config from json |
| 81 | jsonCfg := `{ |
| 82 | "transformers": ["json", "yaml"], |
| 83 | "detectors": ["github", "jwt", "keyword"], |
| 84 | "threshold_in_bytes": 1000000}` |
| 85 | cfg, err := scanner.NewConfigFromJson(strings.NewReader(jsonCfg)) |
| 86 | // or from yaml |
| 87 | yamlCfg := "transformers:\n" + |
| 88 | " - json\n" + |
| 89 | " - yaml\n" + |
| 90 | "detectors:\n" + |
| 91 | " - github\n" + |
| 92 | " - jwt\n" + |
| 93 | " - keyword\n" + |
| 94 | "threshold_in_bytes: 1000000" |
| 95 | cfg, err := scanner.NewConfigFromYaml(strings.NewReader(yamlCfg)) |
| 96 | |
| 97 | // create a scanner |
| 98 | scanner := scanner.NewScannerFromConfig(cfg) |
| 99 | } |
| 100 | ``` |