Skip to content

Commit

Permalink
Read the AI API key also from an environment variable
Browse files Browse the repository at this point in the history
Change-Id: If18fd025ab2ef68a3690f8a69d1c8894e44a87ef
Signed-off-by: Cosmin Cojocar <[email protected]>
  • Loading branch information
ccojocar committed Aug 18, 2024
1 parent 56f943b commit dd5e3ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ gosec can suggest fixes based on AI recommendation. It will call an AI API to re

You can enable this feature by providing the following command line arguments:
- `ai-api-provider`: the name of the AI API provider, currently only `gemini`is supported.
- `ai-api-key`: the key to access the AI API, For gemini, you can create an API key following [these instructions](https://ai.google.dev/gemini-api/docs/api-key).
- `ai-api-key` or set the environment variable `GOSEC_AI_API_KEY`: the key to access the AI API,
For gemini, you can create an API key following [these instructions](https://ai.google.dev/gemini-api/docs/api-key).
- `ai-endpoint`: the endpoint of the AI provider, this is optional argument.


Expand Down
10 changes: 8 additions & 2 deletions cmd/gosec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ USAGE:
$ gosec -exclude=G101 $GOPATH/src/github.com/example/project/...
`
// Environment variable for AI API key.
aiApiKeyEnv = "GOSEC_AI_API_KEY"

Check failure on line 63 in cmd/gosec/main.go

View workflow job for this annotation

GitHub Actions / test (1.21.12, latest)

G101: Potential hardcoded credentials (gosec)

Check failure on line 63 in cmd/gosec/main.go

View workflow job for this annotation

GitHub Actions / test (1.22.5, latest)

G101: Potential hardcoded credentials (gosec)

Check failure

Code scanning / gosec

Potential hardcoded credentials Error

Potential hardcoded credentials
)

type arrayFlags []string
Expand Down Expand Up @@ -468,8 +470,12 @@ func main() {
reportInfo := gosec.NewReportInfo(issues, metrics, errors).WithVersion(Version)

// Call AI request to solve the issues
if *flagAiApiProvider != "" && *flagAiApiKey != "" {
err := autofix.GenerateSolution(*flagAiApiProvider, *flagAiApiKey, *flagAiEndpoint, issues)
aiApiKey := os.Getenv(aiApiKeyEnv)
if aiApiKeyEnv == "" {
aiApiKey = *flagAiApiKey
}
if *flagAiApiProvider != "" && aiApiKey != "" {
err := autofix.GenerateSolution(*flagAiApiProvider, aiApiKey, *flagAiEndpoint, issues)
if err != nil {
logger.Print(err)
}
Expand Down

0 comments on commit dd5e3ff

Please sign in to comment.