-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retreives vulnerability explainations from OpenAI
- Loading branch information
Showing
9 changed files
with
111 additions
and
5 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
Binary file not shown.
Binary file not shown.
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,13 +1,102 @@ | ||
// package openai enriches vulnerability information | ||
package openai | ||
|
||
import "github.com/devops-kung-fu/bomber/models" | ||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"text/template" | ||
|
||
openai "github.com/sashabaranov/go-openai" | ||
|
||
"github.com/devops-kung-fu/bomber/models" | ||
) | ||
|
||
// Provider represents the openai enricher | ||
type Enricher struct{} | ||
|
||
// Enrich adds additional information to vulnerabilities | ||
func (Enricher) Enrich(vulnerabilities []models.Vulnerability, credentials *models.Credentials) ([]models.Vulnerability, error) { | ||
if err := validateCredentials(credentials); err != nil { | ||
return nil, fmt.Errorf("could not validate openai credentials: %w", err) | ||
} | ||
|
||
for _, v := range vulnerabilities { | ||
fetch(v, credentials) | ||
log.Println(v.Explanation) | ||
} | ||
return nil, nil | ||
} | ||
|
||
func validateCredentials(credentials *models.Credentials) (err error) { | ||
if credentials == nil { | ||
return errors.New("credentials cannot be nil") | ||
} | ||
|
||
if credentials.OpenAIAPIKey == "" { | ||
credentials.OpenAIAPIKey = os.Getenv("OPENAI_API_KEY") | ||
} | ||
|
||
if credentials.OpenAIAPIKey == "" { | ||
err = errors.New("bomber requires an openai key to enrich vulnerability data") | ||
} | ||
return | ||
} | ||
|
||
func fetch(vulnerability models.Vulnerability, credentials *models.Credentials) { | ||
prompt := generatePrompt(vulnerability) | ||
client := openai.NewClient(credentials.OpenAIAPIKey) | ||
resp, err := client.CreateChatCompletion( | ||
context.Background(), | ||
openai.ChatCompletionRequest{ | ||
Model: openai.GPT3Dot5Turbo, | ||
Messages: []openai.ChatCompletionMessage{ | ||
{ | ||
Role: openai.ChatMessageRoleUser, | ||
Content: prompt, | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
if err != nil { | ||
log.Printf("ChatCompletion error: %v\n", err) //TODO: Need to pass the error back up the stack | ||
return | ||
} | ||
|
||
vulnerability.Explanation = resp.Choices[0].Message.Content | ||
log.Println(vulnerability.Explanation) | ||
|
||
} | ||
|
||
func generatePrompt(vulnerability models.Vulnerability) (prompt string) { | ||
|
||
promptTemplate := ` | ||
Explain what {{ .Cve }} is and dig into: {{ .Description }} so it could be understood by a non-technical business user. | ||
` | ||
// Create a new template with a name | ||
tmpl, err := template.New("prompt").Parse(promptTemplate) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Create a buffer to store the generated result | ||
var resultBuffer bytes.Buffer | ||
|
||
// Execute the template and write the result to the buffer | ||
err = executeTemplate(&resultBuffer, tmpl, vulnerability) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Convert the buffer to a string and return it | ||
return resultBuffer.String() | ||
} | ||
|
||
func executeTemplate(buffer *bytes.Buffer, tmpl *template.Template, data interface{}) error { | ||
// Execute the template and write the result to the buffer | ||
return tmpl.Execute(buffer, data) | ||
} |
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