Skip to content

Find, verify, and analyze leaked credentials

Updated Github Source Validate method (#4144)

Kashif Khan33f46b1236ae
diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go
index 2d2dc87e8..524d66daa 100644
--- a/pkg/sources/github/github.go
+++ b/pkg/sources/github/github.go
@@ -298,7 +298,13 @@ func (s *Source) Init(aCtx context.Context, name string, jobID sources.JobID, so
// Validate is used by enterprise CLI to validate the GitHub config file.
func (s *Source) Validate(ctx context.Context) []error {
- if _, _, err := s.connector.APIClient().Users.Get(ctx, ""); err != nil {
+ /*
+ Uses the rate limit API (docs: https://docs.github.com/en/rest/rate-limit) because:
+ - Works with all auth types: user tokens, PATs, App credentials, and unauthenticated requests
+ - Returns 401 for invalid credentials but works with no auth (as unauthenticated)
+ - Doesn't consume API quota when called
+ */
+ if _, _, err := s.connector.APIClient().RateLimit.Get(ctx); err != nil {
return []error{err}
}
diff --git a/pkg/sources/github/github_integration_test.go b/pkg/sources/github/github_integration_test.go
index bcdb50050..c58150d05 100644
--- a/pkg/sources/github/github_integration_test.go
+++ b/pkg/sources/github/github_integration_test.go
@@ -11,6 +11,7 @@ import (
"github.com/kylelemons/godebug/pretty"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/anypb"
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
@@ -864,6 +865,91 @@ func TestChunkUnit(t *testing.T) {
assert.Equal(t, 0, reporter.errCount)
}
+func TestSource_Validate(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
+ defer cancel()
+
+ secret, err := common.GetTestSecret(ctx)
+ if err != nil {
+ t.Fatal(fmt.Errorf("failed to access secret: %v", err))
+ }
+
+ githubToken := secret.MustGetField("GITHUB_TOKEN")
+ githubPrivateKeyB64New := secret.MustGetField("GITHUB_PRIVATE_KEY_NEW")
+ githubInstallationIDNew := secret.MustGetField("GITHUB_INSTALLATION_ID_NEW")
+ githubAppIDNew := secret.MustGetField("GITHUB_APP_ID_NEW")
+
+ githubPrivateKeyBytesNew, err := base64.StdEncoding.DecodeString(githubPrivateKeyB64New)
+ if err != nil {
+ t.Fatal(err)
+ }
+ githubPrivateKeyNew := string(githubPrivateKeyBytesNew)
+
+ type args struct {
+ ctx context.Context
+ }
+ tests := []struct {
+ name string
+ args args
+ sourceConfig *Source
+ wantErr bool
+ }{
+ {
+ name: "success - validate - unauthenticated",
+ args: args{ctx: context.Background()},
+ sourceConfig: &Source{conn: &sourcespb.GitHub{Credential: &sourcespb.GitHub_Unauthenticated{}}},
+ wantErr: false,
+ },
+ {
+ name: "success - validate - token authentication",
+ args: args{ctx: context.Background()},
+ sourceConfig: &Source{conn: &sourcespb.GitHub{Credential: &sourcespb.GitHub_Token{Token: githubToken}}},
+ wantErr: false,
+ },
+ {
+ name: "sucess- validate - app token authentication",
+ args: args{ctx: context.Background()},
+ sourceConfig: &Source{conn: &sourcespb.GitHub{Credential: &sourcespb.GitHub_GithubApp{
+ GithubApp: &credentialspb.GitHubApp{
+ PrivateKey: githubPrivateKeyNew,
+ InstallationId: githubInstallationIDNew,
+ AppId: githubAppIDNew,
+ },
+ }}},
+ wantErr: false,
+ },
+ {
+ name: "fail - validate - token authentication",
+ args: args{ctx: context.Background()},
+ sourceConfig: &Source{conn: &sourcespb.GitHub{Credential: &sourcespb.GitHub_Token{Token: githubToken + "fake"}}},
+ wantErr: true,
+ },
+ {
+ name: "fail- validate - app token authentication",
+ args: args{ctx: context.Background()},
+ sourceConfig: &Source{conn: &sourcespb.GitHub{Credential: &sourcespb.GitHub_GithubApp{
+ GithubApp: &credentialspb.GitHubApp{
+ PrivateKey: githubPrivateKeyNew + "fake",
+ InstallationId: githubInstallationIDNew + "0",
+ AppId: githubAppIDNew,
+ },
+ }}},
+ wantErr: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ connector, err := newConnector(tt.sourceConfig)
+ require.NoError(t, err)
+ tt.sourceConfig.connector = connector
+
+ if err := tt.sourceConfig.Validate(tt.args.ctx); err != nil && !tt.wantErr {
+ t.Errorf("Source.Validate() = %v, wantErr %t", err, tt.wantErr)
+ }
+ })
+ }
+}
+
type countChunkReporter struct {
chunkCount int
errCount int