-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR is the first implementation of validity check #191. I added the flow of validation, controlled by the `--validate` flag, and added validation for Github token.
- Loading branch information
Baruch Odem (Rothkoff)
authored
Feb 19, 2024
1 parent
5a7ac9d
commit 807e0b5
Showing
4 changed files
with
103 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,68 @@ | ||
package secrets | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type ValidationResult string | ||
|
||
const ( | ||
Valid ValidationResult = "Valid" | ||
Revoked ValidationResult = "Revoked" | ||
Unknown ValidationResult = "Unknown" | ||
) | ||
|
||
type Secret struct { | ||
ID string `json:"id"` | ||
Source string `json:"source"` | ||
RuleID string `json:"ruleId"` | ||
StartLine int `json:"startLine"` | ||
EndLine int `json:"endLine"` | ||
StartColumn int `json:"startColumn"` | ||
EndColumn int `json:"endColumn"` | ||
Value string `json:"value"` | ||
ID string `json:"id"` | ||
Source string `json:"source"` | ||
RuleID string `json:"ruleId"` | ||
StartLine int `json:"startLine"` | ||
EndLine int `json:"endLine"` | ||
StartColumn int `json:"startColumn"` | ||
EndColumn int `json:"endColumn"` | ||
Value string `json:"value"` | ||
ValidationStatus ValidationResult `json:"validationStatus,omitempty"` | ||
} | ||
|
||
type validationFunc = func(*Secret) ValidationResult | ||
|
||
var ruleIDToFunction = map[string]validationFunc{ | ||
"github-fine-grained-pat": validateGithub, | ||
"github-pat": validateGithub, | ||
} | ||
|
||
func (s *Secret) Validate(wg *sync.WaitGroup) { | ||
defer wg.Done() | ||
if f, ok := ruleIDToFunction[s.RuleID]; ok { | ||
s.ValidationStatus = f(s) | ||
} else { | ||
s.ValidationStatus = Unknown | ||
} | ||
} | ||
|
||
func validateGithub(s *Secret) ValidationResult { | ||
const githubURL = "https://api.github.com/" | ||
|
||
req, err := http.NewRequest("GET", githubURL, nil) | ||
if err != nil { | ||
log.Warn().Err(err).Msg("Failed to validate secret") | ||
return Unknown | ||
} | ||
req.Header.Set("Authorization", fmt.Sprintf("token %s", s.Value)) | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Warn().Err(err).Msg("Failed to validate secret") | ||
return Unknown | ||
} | ||
|
||
if resp.StatusCode == http.StatusOK { | ||
return Valid | ||
} | ||
return Revoked | ||
} |