-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathgeneratecontext.go
More file actions
112 lines (96 loc) · 3.07 KB
/
Copy pathgeneratecontext.go
File metadata and controls
112 lines (96 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package cmd
import (
"fmt"
"os"
"github.com/knostic/open-ant-cli/internal/output"
"github.com/knostic/open-ant-cli/internal/python"
"github.com/spf13/cobra"
)
var generateContextCmd = &cobra.Command{
Use: "generate-context [repository-path]",
Short: "Generate application security context for a repository",
Long: `Analyzes a repository and produces an application_context.json file
that describes the application type, trust boundaries, intended
behaviors, and patterns that should not be flagged as vulnerabilities.
This context is automatically used by the analyze and verify commands
to reduce false positives.
If no repository path is given, the active project is used (see: openant init).
The command checks for a manual override file (OPENANT.md or OPENANT.json)
in the repository root before falling back to LLM-based generation.
Use --force to skip the manual override check.`,
Args: cobra.MaximumNArgs(1),
Run: runGenerateContext,
}
var (
gcOutput string
gcForce bool
gcShowPrompt bool
)
func init() {
generateContextCmd.Flags().StringVarP(&gcOutput, "output", "o", "", "Output path (default: <scan-dir>/application_context.json or <repo>/application_context.json)")
generateContextCmd.Flags().BoolVar(&gcForce, "force", false, "Force regeneration, ignoring OPENANT.md override files")
generateContextCmd.Flags().BoolVar(&gcShowPrompt, "show-prompt", false, "Include formatted prompt text in output")
}
func runGenerateContext(cmd *cobra.Command, args []string) {
repoPath, ctx, err := resolveRepoArg(args)
if err != nil {
output.PrintError(err.Error())
os.Exit(2)
}
// Apply project defaults
if ctx != nil {
if gcOutput == "" {
gcOutput = ctx.scanFile("application_context.json")
}
}
rt, err := ensurePython()
if err != nil {
output.PrintError(err.Error())
os.Exit(2)
}
// Build Python CLI args
pyArgs := []string{"generate-context", repoPath}
if gcOutput != "" {
pyArgs = append(pyArgs, "--output", gcOutput)
}
if gcForce {
pyArgs = append(pyArgs, "--force")
}
if gcShowPrompt {
pyArgs = append(pyArgs, "--show-prompt")
}
result, err := python.Invoke(rt.Path, pyArgs, "", quiet, requireAPIKey())
if err != nil {
output.PrintError(err.Error())
os.Exit(2)
}
if jsonOutput {
output.PrintJSON(result.Envelope)
} else if result.Envelope.Status == "success" {
if data, ok := result.Envelope.Data.(map[string]any); ok {
printGenerateContextSummary(data)
}
} else {
output.PrintErrors(result.Envelope.Errors)
}
os.Exit(result.ExitCode)
}
func printGenerateContextSummary(data map[string]any) {
output.PrintHeader("Application Context Generated")
if v, ok := data["application_type"].(string); ok {
output.PrintKeyValue("Type", v)
}
if v, ok := data["purpose"].(string); ok {
output.PrintKeyValue("Purpose", v)
}
if v, ok := data["confidence"].(float64); ok {
output.PrintKeyValue("Confidence", fmt.Sprintf("%.0f%%", v*100))
}
if v, ok := data["source"].(string); ok {
output.PrintKeyValue("Source", v)
}
if v, ok := data["app_context_path"].(string); ok {
output.PrintKeyValue("Output", v)
}
fmt.Println()
}