Skip to content

Find, verify, and analyze leaked credentials

add thog CLI support for GitHub config validate (#1626)

Zubair Khan9a13c74a358d
diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go
index 26e615bb7..5bab8f08a 100644
--- a/pkg/sources/github/github.go
+++ b/pkg/sources/github/github.go
@@ -273,6 +273,65 @@ func (s *Source) Init(aCtx context.Context, name string, jobID, sourceID int64,
return nil
}
+// Validate is used by enterprise CLI to validate the Github config file.
+func (s *Source) Validate(ctx context.Context) []error {
+ var (
+ errs []error
+ ghClient *github.Client
+ err error
+ )
+ apiEndpoint := s.conn.Endpoint
+
+ switch cred := s.conn.GetCredential().(type) {
+ case *sourcespb.GitHub_BasicAuth:
+ s.httpClient.Transport = &github.BasicAuthTransport{
+ Username: cred.BasicAuth.Username,
+ Password: cred.BasicAuth.Password,
+ }
+ ghClient, err = createGitHubClient(s.httpClient, apiEndpoint)
+ if err != nil {
+ errs = append(errs, fmt.Errorf("error creating GitHub client: %+v", err))
+ }
+ case *sourcespb.GitHub_Unauthenticated:
+ ghClient, err = createGitHubClient(s.httpClient, apiEndpoint)
+ if err != nil {
+ errs = append(errs, fmt.Errorf("error creating GitHub client: %+v", err))
+ }
+ case *sourcespb.GitHub_Token:
+ s.githubToken = cred.Token
+
+ ts := oauth2.StaticTokenSource(
+ &oauth2.Token{AccessToken: s.githubToken},
+ )
+ s.httpClient.Transport = &oauth2.Transport{
+ Base: s.httpClient.Transport,
+ Source: oauth2.ReuseTokenSource(nil, ts),
+ }
+
+ ghClient, err = createGitHubClient(s.httpClient, apiEndpoint)
+ if err != nil {
+ errs = append(errs, fmt.Errorf("error creating GitHub client: %+v", err))
+ }
+ default:
+ errs = append(errs, errors.Errorf("Invalid configuration given for source. Name: %s, Type: %s", s.name, s.Type()))
+ }
+
+ // Run a simple query to check if the client is actually valid
+ if ghClient != nil {
+ err = checkGitHubConnection(ctx, ghClient)
+ if err != nil {
+ errs = append(errs, err)
+ }
+ }
+
+ return errs
+}
+
+func checkGitHubConnection(ctx context.Context, client *github.Client) error {
+ _, _, err := client.Users.Get(ctx, "")
+ return err
+}
+
func (s *Source) visibilityOf(ctx context.Context, repoURL string) (visibility source_metadatapb.Visibility) {
s.mu.Lock()
visibility, ok := s.publicMap[repoURL]
diff --git a/pkg/sources/sources.go b/pkg/sources/sources.go
index 17818d260..474de2434 100644
--- a/pkg/sources/sources.go
+++ b/pkg/sources/sources.go
@@ -248,7 +248,7 @@ type Progress struct {
// Validator is an interface for validating a source. Sources can optionally implement this interface to validate
// their configuration.
type Validator interface {
- Validate() []error
+ Validate(ctx context.Context) []error
}
// SetProgressComplete sets job progress information for a running job based on the highest level objects in the source.
diff --git a/pkg/sources/syslog/syslog.go b/pkg/sources/syslog/syslog.go
index 5869f154e..b2ba64fa1 100644
--- a/pkg/sources/syslog/syslog.go
+++ b/pkg/sources/syslog/syslog.go
@@ -60,7 +60,7 @@ func NewSyslog(sourceType sourcespb.SourceType, jobID, sourceID int64, sourceNam
}
// Validate validates the configuration of the source.
-func (s *Source) Validate() []error {
+func (s *Source) Validate(ctx context.Context) []error {
var errors []error
if s.conn.TlsCert != nilString || s.conn.TlsKey != nilString {