Skip to content

Commit d85864a

Browse files
committed
Add more contributing docs
1 parent 8d2dd62 commit d85864a

7 files changed

Lines changed: 138 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
dist
3+
.env

CONTRIBUTING.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# Contribution guidelines
22

3-
Please create an issue to collect feedback prior to feature additions. If possible try to keep PR's scoped to one feature, and add tests for new features.
3+
Please create an issue to collect feedback prior to feature additions. If possible try to keep PRs scoped to one feature, and add tests for new features.
44

5-
When showing intereste, in a bug, enhancement, PR, or issue, please use the thumbs up/thumbs down emoji on the original message rather than adding comments expressing the same.
5+
When showing interest in a bug, enhancement, PR, or issue, please use the thumbs up/thumbs down emoji on the original message rather than adding comments expressing the same.
66

7-
Contributors need to [sign our CLA](https://cla-assistant.io/trufflesecurity/trufflehog) before we are able to accept contributions.
7+
Contributors need to [sign our CLA](https://cla-assistant.io/trufflesecurity/trufflehog) before we are able to accept contributions.
8+
9+
# Resources
10+
11+
## Adding new secret detectors
12+
13+
We have published some [documentation and tooling to get started on adding new secret detectors](hack/docs/Adding_Detectors_external.md). Let's improve detection together!

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ Contributions are very welcome! Please see our [contribution guidelines first](C
137137

138138
We no longer accept contributions to TruffleHog v2, but that code is available in the `v2` branch.
139139

140+
### Adding new secret detectors
141+
142+
We have published some [documentation and tooling to get started on adding new secret detectors](hack/docs/Adding_Detectors_external.md). Let's improve detection together!
143+
140144
## License Change
141145

142146
Since v3.0, TruffleHog is released under a AGPL 3 license, included in [`LICENSE`](LICENSE). TruffleHog v3.0 uses none of the previous codebase, but care was taken to preserve backwards compatibility on the command line interface. The work previous to this release is still available licensed under GPL 2.0 in the history of this repository and the previous package releases and tags. A completed CLA is required for us to accept contributions going forward.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ If you think that something should be included outside of these guidelines, plea
4141
- A GitLab account
4242
- A Google account
4343
- [Google Cloud SDK installed](#setting-up-google-cloud-sdk)
44-
- Go 1.16
44+
- Go 1.17+
4545
- Make
4646

4747
### Creating a new Secret Scanner
@@ -100,7 +100,7 @@ Do not embed test credentials in the test code. Instead, use GCP Secrets Manager
100100
gcloud secrets versions add --project trufflehog-testing detectors3 --data-file /tmp/s
101101
```
102102

103-
4. Access the secret value as shown in the [example code](pkg/secrets/heroku/heroku_test.go).
103+
4. Access the secret value as shown in the [example code](pkg/detectors/heroku/heroku_test.go).
104104

105105
### Setting up Google Cloud SDK
106106

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Secret Detectors
2+
3+
Secret Detectors have these two major functions:
4+
5+
1. Given some bytes, extract possible secrets, typically using a regex.
6+
2. Validate the secrets against the target API, typically using a HTTP client.
7+
8+
The purpose of Secret Detectors is to discover secrets with exceptionally high signal. High rates of false positives are not accepted.
9+
10+
## Table of Contents
11+
12+
- [Secret Detectors](#secret-detectors)
13+
* [Table of Contents](#table-of-contents)
14+
* [Getting Started](#getting-started)
15+
+ [Sourcing Guidelines](#sourcing-guidelines)
16+
+ [Development Guidelines](#development-guidelines)
17+
+ [Development Dependencies](#development-dependencies)
18+
+ [Creating a new Secret Scanner](#creating-a-new-secret-scanner)
19+
* [Addendum](#addendum)
20+
+ [Using a test secret file](#using-a-test-secret-file)
21+
+ [Adding Protos in Windows](#adding-protos-in-windows)
22+
23+
## Getting Started
24+
25+
### Sourcing Guidelines
26+
27+
We are interested in detectors for services that meet at least one of these criteria
28+
- host data (they store any sort of data provided)
29+
- have paid services (having a free or trial tier is okay though)
30+
31+
If you think that something should be included outside of these guidelines, please let us know.
32+
33+
### Development Guidelines
34+
35+
- When reasonable, favor using the `net/http` library to make requests instead of bringing in another library.
36+
- Use the [`common.SaneHttpClient`](pkg/common/http.go) for the `http.Client` whenever possible.
37+
38+
### Development Dependencies
39+
40+
- Go 1.17+
41+
- Make
42+
43+
### Creating a new Secret Scanner
44+
45+
1. Identify the Secret Detector name from the [/proto/detectors.proto](/proto/detectors.proto) `DetectorType` enum. If necessary, run `make protos` when adding new ones.
46+
47+
2. Generate the Secret Detector
48+
49+
```bash
50+
go run hack/generate/generate.go detector <DetectorType enum name>
51+
```
52+
53+
3. Complete the secret detector.
54+
55+
The previous step templated a boilerplate + some example code as a package in the `pkg/detectors` folder for you to work on.
56+
The secret detector can be completed with these general steps:
57+
58+
1. Create a [test secrets file, and export the variable](#using-a-test-secret-file)
59+
2. Update the pattern regex and keywords. Try iterating with [regex101.com](http://regex101.com/).
60+
3. Update the verifier code to use a non-destructive API call that can determine whether the secret is valid or not.
61+
4. Update the tests with these test cases at minimum:
62+
1. Found and verified (using a credential loaded from GCP Secrets)
63+
2. Found and unverified
64+
3. Not found
65+
4. Any false positive cases that you come across
66+
5. Create a pull request for review.
67+
68+
## Addendum
69+
70+
### Using a test secret file
71+
72+
1. Create a file called `.env` with this env file format:
73+
74+
```bash
75+
SECRET_TYPE_ONE=value
76+
SECRET_TYPE_ONE_INACTIVE=v@lue
77+
```
78+
79+
2. Export the `TEST_SECRET_FILE` variable, pointing to the env file:
80+
81+
```bash
82+
export TEST_SECRET_FILE=".env"
83+
```
84+
85+
Now, the detector test should attempt to load the given env key from that file.
86+
87+
### Adding Protos in Windows
88+
89+
1. Install Ubuntu App in Microsoft Store https://www.microsoft.com/en-us/p/ubuntu/9nblggh4msv6.
90+
2. Install Docker Desktop https://www.docker.com/products/docker-desktop. Enable WSL integration to Ubuntu. In Docker app, go to Settings->Resources->WSL INTEGRATION->enable Ubuntu.
91+
3. Open Ubuntu cli and install `dos2unix`.
92+
```bash
93+
sudo apt install dos2unix
94+
```
95+
4. Identify the `trufflehog` local directory and convert `scripts/gen_proto.sh` file in Unix format.
96+
```bash
97+
dos2unix ./scripts/gen_proto.sh
98+
```
99+
5. Open [/proto/detectors.proto](/proto/detectors.proto) file and add new detectors then save it. Make sure Docker is running and run this in Ubuntu command line.
100+
```bash
101+
make protos
102+
```

pkg/common/secrets.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package common
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"time"
78

89
secretmanager "cloud.google.com/go/secretmanager/apiv1"
@@ -21,14 +22,32 @@ func (s *Secret) MustGetField(name string) string {
2122
return val
2223
}
2324

25+
func GetSecretFromEnv(filename string) (secret *Secret, err error) {
26+
data, err := godotenv.Read(filename)
27+
if err != nil {
28+
return nil, err
29+
}
30+
return &Secret{kv: data}, nil
31+
}
32+
2433
func GetTestSecret(ctx context.Context) (secret *Secret, err error) {
34+
filename := os.Getenv("TEST_SECRET_FILE")
35+
if len(filename) > 0 {
36+
return GetSecretFromEnv(filename)
37+
}
38+
2539
return GetSecret(ctx, "trufflehog-testing", "test")
2640
}
2741

2842
func GetSecret(ctx context.Context, gcpProject, name string) (secret *Secret, err error) {
2943
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
3044
defer cancel()
3145

46+
filename := os.Getenv("TEST_SECRET_FILE")
47+
if len(filename) > 0 {
48+
return GetSecretFromEnv(filename)
49+
}
50+
3251
parent := fmt.Sprintf("projects/%s/secrets/%s/versions/latest", gcpProject, name)
3352

3453
client, err := secretmanager.NewClient(ctx)

pkg/updater/updater.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (g *OSS) Fetch() (io.Reader, error) {
7676
defer resp.Body.Close()
7777

7878
if resp.StatusCode == http.StatusNoContent {
79-
return nil, nil
79+
return nil, errors.New("already up to date")
8080
}
8181

8282
log.Debug("fetching trufflehog update")

0 commit comments

Comments
 (0)