-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdestructive_contract.go
More file actions
302 lines (275 loc) · 9.34 KB
/
Copy pathdestructive_contract.go
File metadata and controls
302 lines (275 loc) · 9.34 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"github.com/rest-sh/restish/cli"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
destructiveActionName string
destructiveActionDryRun bool
destructiveCommandSet = map[string]bool{}
destructiveMetadataRead bool
destructiveMetadataErr error
loadOperationAPI = func(base string, root *cobra.Command) (cli.API, error) {
// cli.Load dereferences cli.Cache, which only exists after cli.Init.
if cli.Cache == nil {
return cli.API{}, errors.New("restish CLI is not initialized")
}
return cli.Load(base, root)
}
)
// Non-DELETE operations belong here only when they can revoke access, alter financial contracts,
// or disable ingestion or automation. Routine lifecycle and cosmetic updates remain ungated.
var explicitlyDestructiveOperations = map[string]bool{
"accept-budget-suggestion": true,
"activate-contract": true,
"assign-objects-to-label": true,
"cancel-contract": true,
"cancel-invite": true,
"delete-datahub-events-by-filter": true,
"dismiss-budget-suggestion": true,
"trigger-cloudflow-webhook": true,
"update-aws-feature": true,
"update-cloudflow-connection": true,
"update-contract": true,
"update-contract-template": true,
"update-resource-permission": true,
"update-user": true,
}
func resetDestructiveContractState() {
destructiveActionName = ""
destructiveActionDryRun = false
destructiveCommandSet = map[string]bool{}
destructiveMetadataRead = false
destructiveMetadataErr = nil
}
type destructiveConfirmationError struct {
Command string
Resolved *resolvedTarget
}
func (e destructiveConfirmationError) Error() string {
if e.Resolved != nil {
return fmt.Sprintf(
"%s targets %s %q (%s); re-run with --yes or set DCI_CONFIRM_DESTRUCTIVE=1",
e.Command, e.Resolved.resource, e.Resolved.name, e.Resolved.id,
)
}
return fmt.Sprintf("%s requires confirmation; re-run with --yes or set DCI_CONFIRM_DESTRUCTIVE=1", e.Command)
}
func (e destructiveConfirmationError) ExitCode() int {
return 30
}
func (e destructiveConfirmationError) AgentErrorCode() string {
return "DESTRUCTIVE_REQUIRES_CONFIRMATION"
}
func (e destructiveConfirmationError) AgentErrorHint() string {
if e.Resolved != nil {
// The suggested re-run uses the resolved ID, not the fuzzy input, so
// the retry stays deterministic even if names change in between.
return fmt.Sprintf("Re-run dci %s %s --yes after reviewing the operation, or use --dry-run first", e.Command, e.Resolved.id)
}
return "Re-run with --yes after reviewing the operation, or use --dry-run first"
}
func (e destructiveConfirmationError) AgentErrorRetryable() bool {
return false
}
func (e destructiveConfirmationError) StructuredError() structuredError {
detail := structuredError{
Code: e.AgentErrorCode(),
Message: e.Error(),
Hint: e.AgentErrorHint(),
Retryable: e.AgentErrorRetryable(),
}
if e.Resolved != nil {
detail.Resolved = &resolvedTargetPayload{Input: e.Resolved.input, Name: e.Resolved.name, ID: e.Resolved.id}
}
return detail
}
type dryRunResult struct {
DryRun bool `json:"dry_run"`
Command string `json:"command"`
Arguments []string `json:"arguments,omitempty"`
}
type actionSummary struct {
Command string `json:"command"`
Status string `json:"status"`
DryRun bool `json:"dry_run,omitempty"`
}
type actionResult struct {
Action actionSummary `json:"action"`
Result interface{} `json:"result,omitempty"`
}
type destructiveActionSummaryGuard struct {
next cli.ResponseFormatter
}
func (guard destructiveActionSummaryGuard) Format(response cli.Response) error {
if destructiveActionName != "" && response.Status >= 200 && response.Status < 300 {
if isErrorResponseBody(response) {
return guard.next.Format(response)
}
status := "completed"
if destructiveActionDryRun {
status = "simulated"
}
response.Body = actionResult{
Action: actionSummary{Command: destructiveActionName, Status: status, DryRun: destructiveActionDryRun},
Result: response.Body,
}
}
return guard.next.Format(response)
}
func installDestructiveActionSummaryGuard() {
if cli.Formatter != nil {
cli.Formatter = destructiveActionSummaryGuard{next: cli.Formatter}
}
}
func isDestructiveOperation(operation cli.Operation) bool {
return strings.EqualFold(operation.Method, "DELETE") || explicitlyDestructiveOperations[operation.Name]
}
func setDestructiveOperations(operations []cli.Operation) {
destructiveCommandSet = make(map[string]bool, len(operations))
for _, operation := range operations {
destructiveCommandSet[operation.Name] = isDestructiveOperation(operation)
}
destructiveMetadataRead = true
destructiveMetadataErr = nil
}
// setOperationMetadata records everything the pre-run hooks need from a loaded
// API: which commands are destructive, and the declared type of each path
// parameter. Both are keyed by operation name and read back by command name.
func setOperationMetadata(operations []cli.Operation) {
setDestructiveOperations(operations)
setOperationPathParameters(operations)
setResolutionIndex(operations)
registerBetaResolutionMetadata()
relaxResolvableArgsValidation()
}
func ensureDestructiveOperations() error {
if destructiveMetadataRead {
return destructiveMetadataErr
}
api, err := loadDCIOperationAPI()
if err != nil {
return err
}
destructiveMetadataRead = true
if len(api.Operations) == 0 {
destructiveMetadataErr = errors.New("DCI operation metadata is unavailable")
return destructiveMetadataErr
}
setOperationMetadata(api.Operations)
return nil
}
func loadDCIOperationAPI() (cli.API, error) {
base, err := apiBase()
if err != nil {
return cli.API{}, err
}
return loadOperationAPI(base, &cobra.Command{})
}
func isDestructiveCommand(command *cobra.Command) bool {
return destructiveCommandSet[command.Name()]
}
func enforceDestructiveConfirmation(command *cobra.Command, args []string) error {
if viper.GetBool("agent-dry-run") {
if command.LocalNonPersistentFlags().Lookup("dry-run") != nil {
if err := ensureDryRunIdempotencyKey(command); err != nil {
return err
}
destructiveActionName = command.Name()
destructiveActionDryRun = true
return nil
}
result := dryRunResult{DryRun: true, Command: command.Name(), Arguments: args}
if err := json.NewEncoder(os.Stdout).Encode(result); err != nil {
return err
}
destructiveActionName = ""
destructiveActionDryRun = false
command.Run = nil
command.RunE = func(command *cobra.Command, args []string) error { return nil }
return nil
}
if err := ensureDestructiveOperations(); err != nil {
return fmt.Errorf("load destructive operation metadata: %w", err)
}
if !isDestructiveCommand(command) {
return nil
}
destructiveActionName = command.Name()
destructiveActionDryRun = false
confirmed := viper.GetBool("agent-confirm-destructive")
if !confirmed {
confirmed, _ = parseBoolish(os.Getenv("DCI_CONFIRM_DESTRUCTIVE"))
}
if !confirmed {
// Interactive humans get a default-Cancel confirm prompt instead of
// the --yes usage error (TUI-SPEC F3). Declining (or any non-TTY /
// agent context) keeps the exit-30 error path below byte-identical.
ensureConfirmTargetDetails(command.Name(), args)
confirmed = confirmDestructiveInteractively(command.Name())
}
if !confirmed {
destructiveActionName = ""
return destructiveConfirmationError{Command: command.Name(), Resolved: commandResolvedTarget(command.Name())}
}
return nil
}
// ensureConfirmTargetDetails backfills the confirmation display for a
// destructive command invoked with a raw resource id: name resolution never
// ran, so the prompt would otherwise show only the command name. Best-effort
// and interactive-only — the id is looked up in the resource's collection
// (fresh name cache first, then a live list fetch behind a spinner), and any
// failure or miss simply leaves the prompt without the target line. The
// backfilled target also reaches the exit-30 error when the human declines.
func ensureConfirmTargetDetails(commandName string, args []string) {
if !tuiActive() || commandResolvedTarget(commandName) != nil || len(args) == 0 {
return
}
target, ok := resolutionIndex[commandName]
if !ok {
return
}
id := strings.TrimSpace(args[0])
if id == "" {
return
}
context := activeCustomerContext()
entries, cached := resolverCachedEntries(target.resource, context)
if !cached {
stopSpinner := startTUISpinner(fmt.Sprintf("Looking up %s %s…", singularResourceName(target.resource), id))
result, err := resolverListFetch(target.listPath, context, resolverMaxPages)
stopSpinner()
if err != nil {
return
}
entries = result.entries
}
for _, entry := range entries {
if entry.ID == id {
resolvedTargets[commandName] = resolvedFromEntry(id, singularResourceName(target.resource), entry)
return
}
}
}
func ensureDryRunIdempotencyKey(command *cobra.Command) error {
flag := command.LocalNonPersistentFlags().Lookup("idempotency-key")
if flag == nil || flag.Changed {
return nil
}
bytes := make([]byte, 16)
if _, err := rand.Read(bytes); err != nil {
return fmt.Errorf("generate dry-run idempotency key: %w", err)
}
if err := command.Flags().Set("idempotency-key", "dci-dry-run-"+hex.EncodeToString(bytes)); err != nil {
return fmt.Errorf("set dry-run idempotency key: %w", err)
}
return nil
}