Harden host deletion and test stability
This commit is contained in:
@@ -49,6 +49,8 @@ type ActionSet struct {
|
||||
AccessPreview func(context.Context, AccessPreviewRequest) (AccessPreviewResult, error)
|
||||
}
|
||||
|
||||
const maxJSONBodyBytes int64 = 1 << 20
|
||||
|
||||
type HostInfo struct {
|
||||
HostID string `json:"host_id"`
|
||||
BaseURL string `json:"base_url"`
|
||||
@@ -834,9 +836,17 @@ func handleDeleteHost(w http.ResponseWriter, r *http.Request, fn func(context.Co
|
||||
}
|
||||
|
||||
func decodeJSON(r *http.Request, dest any) *httpError {
|
||||
if r == nil {
|
||||
return &httpError{StatusCode: http.StatusBadRequest, Code: "bad_request", Message: "request is required"}
|
||||
}
|
||||
r.Body = http.MaxBytesReader(nil, r.Body, maxJSONBodyBytes)
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(dest); err != nil {
|
||||
var maxBytesErr *http.MaxBytesError
|
||||
if errors.As(err, &maxBytesErr) {
|
||||
return &httpError{StatusCode: http.StatusRequestEntityTooLarge, Code: "request_too_large", Message: fmt.Sprintf("request body exceeds %d bytes", maxJSONBodyBytes)}
|
||||
}
|
||||
return &httpError{StatusCode: http.StatusBadRequest, Code: "bad_request", Message: fmt.Sprintf("decode request body: %v", err)}
|
||||
}
|
||||
if err := decoder.Decode(&struct{}{}); err != nil && !errors.Is(err, io.EOF) {
|
||||
@@ -870,6 +880,10 @@ func classifyError(err error) *httpError {
|
||||
if errors.As(err, &upstreamErr) {
|
||||
return &httpError{StatusCode: http.StatusBadGateway, Code: "host_request_failed", Message: err.Error(), UpstreamStatus: upstreamErr.StatusCode}
|
||||
}
|
||||
var hostDeleteBlocker *sqlite.HostDeleteBlocker
|
||||
if errors.As(err, &hostDeleteBlocker) {
|
||||
return &httpError{StatusCode: http.StatusConflict, Code: "host_in_use", Message: err.Error()}
|
||||
}
|
||||
message := err.Error()
|
||||
switch {
|
||||
case strings.Contains(message, "already installed") || strings.Contains(message, "checksum drift"):
|
||||
@@ -1254,7 +1268,10 @@ func NewActionSet(sqliteDSN string) ActionSet {
|
||||
return err
|
||||
}
|
||||
defer store.Close()
|
||||
return store.Hosts().DeleteByHostID(ctx, hostID)
|
||||
if err := store.Hosts().DeleteByHostID(ctx, hostID); err != nil {
|
||||
return classifyError(err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
ListPacks: func(ctx context.Context) ([]PackInfo, error) {
|
||||
store, err := sqlite.Open(ctx, sqliteDSN)
|
||||
|
||||
Reference in New Issue
Block a user