Skip to content

Commit 92bac42

Browse files
authored
Read the AI API key also from an environment variable (#1181)
* Read the AI API key also from an environment variable Change-Id: If18fd025ab2ef68a3690f8a69d1c8894e44a87ef Signed-off-by: Cosmin Cojocar <ccojocar@google.com> * Fix lint warning Change-Id: Icd3eb8a029764db76596c3e171275c03a23f8cef Signed-off-by: Cosmin Cojocar <ccojocar@google.com> --------- Signed-off-by: Cosmin Cojocar <ccojocar@google.com>
1 parent 56f943b commit 92bac42

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ gosec can suggest fixes based on AI recommendation. It will call an AI API to re
279279

280280
You can enable this feature by providing the following command line arguments:
281281
- `ai-api-provider`: the name of the AI API provider, currently only `gemini`is supported.
282-
- `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).
282+
- `ai-api-key` or set the environment variable `GOSEC_AI_API_KEY`: the key to access the AI API,
283+
For gemini, you can create an API key following [these instructions](https://ai.google.dev/gemini-api/docs/api-key).
283284
- `ai-endpoint`: the endpoint of the AI provider, this is optional argument.
284285

285286

cmd/gosec/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ USAGE:
5959
$ gosec -exclude=G101 $GOPATH/src/github.com/example/project/...
6060
6161
`
62+
// Environment variable for AI API key.
63+
aiApiKeyEnv = "GOSEC_AI_API_KEY" // #nosec G101
6264
)
6365

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

470472
// Call AI request to solve the issues
471-
if *flagAiApiProvider != "" && *flagAiApiKey != "" {
472-
err := autofix.GenerateSolution(*flagAiApiProvider, *flagAiApiKey, *flagAiEndpoint, issues)
473+
aiApiKey := os.Getenv(aiApiKeyEnv)
474+
if aiApiKeyEnv == "" {
475+
aiApiKey = *flagAiApiKey
476+
}
477+
if *flagAiApiProvider != "" && aiApiKey != "" {
478+
err := autofix.GenerateSolution(*flagAiApiProvider, aiApiKey, *flagAiEndpoint, issues)
473479
if err != nil {
474480
logger.Print(err)
475481
}

0 commit comments

Comments
 (0)