|
8 | 8 | "io" |
9 | 9 | "net/http" |
10 | 10 | "net/url" |
| 11 | + "os" |
11 | 12 | "sort" |
| 13 | + "strconv" |
12 | 14 | "strings" |
13 | 15 | "time" |
14 | 16 |
|
@@ -106,6 +108,7 @@ func (c *ReflectClient) ListModels(ctx context.Context) ([]ReflectModel, error) |
106 | 108 | if err != nil { |
107 | 109 | return nil, fmt.Errorf("reading reflect model list: %w", err) |
108 | 110 | } |
| 111 | + printReflectResponse(os.Stderr, http.MethodGet, resp.Status, body) |
109 | 112 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
110 | 113 | return nil, fmt.Errorf("listing reflect models returned %s: %s", resp.Status, responseBodyPreview(body)) |
111 | 114 | } |
@@ -135,12 +138,33 @@ func (c *ReflectClient) postReflect(ctx context.Context, payload map[string]any) |
135 | 138 | if err != nil { |
136 | 139 | return nil, fmt.Errorf("reading reflect response: %w", err) |
137 | 140 | } |
| 141 | + printReflectResponse(os.Stderr, http.MethodPost, resp.Status, body) |
138 | 142 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
139 | 143 | return nil, fmt.Errorf("reflect returned %s: %s", resp.Status, responseBodyPreview(body)) |
140 | 144 | } |
141 | 145 | return body, nil |
142 | 146 | } |
143 | 147 |
|
| 148 | +func printReflectResponse(w io.Writer, method, status string, body []byte) { |
| 149 | + if !logReflectResponseEnabled() { |
| 150 | + return |
| 151 | + } |
| 152 | + fmt.Fprintf(w, "::group::/reflect %s response (%s)\n%s", method, status, string(body)) |
| 153 | + if !bytes.HasSuffix(body, []byte("\n")) { |
| 154 | + fmt.Fprintln(w) |
| 155 | + } |
| 156 | + fmt.Fprintln(w, "::endgroup::") |
| 157 | +} |
| 158 | + |
| 159 | +func logReflectResponseEnabled() bool { |
| 160 | + value := strings.TrimSpace(os.Getenv("THREAT_DETECTION_LOG_REFLECT_RESPONSE")) |
| 161 | + if value == "" { |
| 162 | + return false |
| 163 | + } |
| 164 | + enabled, err := strconv.ParseBool(value) |
| 165 | + return err == nil && enabled |
| 166 | +} |
| 167 | + |
144 | 168 | func (c *ReflectClient) endpoint() string { |
145 | 169 | base := strings.TrimSpace(c.BaseURL) |
146 | 170 | if base == "" { |
|
0 commit comments