my-homelab-configs/apps/opsproof/overlay/handlers/proposal/proposal.go

142 lines
4.1 KiB
Go

package proposal
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"go.laravel.com/db"
"go.laravel.com/inertia"
"go.laravel.com/model"
"go.laravel.com/validation"
"go.laravel.com/validation/rule"
"go.laravel.com/web"
"app/models"
"app/services"
)
type StoreProposal struct {
Rollback string `json:"rollback"`
}
func (form *StoreProposal) Rules() validation.Rules {
return validation.For(form, rule.Field(&form.Rollback, rule.Required, rule.Min(20), rule.Max(2000)))
}
func Store(request *web.Request) *web.Response {
scope, target := request.PathValue("scope"), request.PathValue("target")
if !services.ValidDoctorTarget(scope, target) {
return web.Abort(http.StatusNotFound)
}
form, err := request.Validate[StoreProposal]()
if err != nil {
return web.Fail(err)
}
incident, err := services.From(request.Context()).Jeannie.Doctor(request.Context(), scope, target)
if err != nil {
return web.Fail(err)
}
if incident.Proposal.Action == "" || len(incident.Proposal.EvidenceIDs) == 0 {
return web.Abort(http.StatusConflict)
}
evidenceHash, err := services.EvidenceFingerprint(incident)
if err != nil {
return web.Fail(err)
}
evidenceJSON, err := json.Marshal(incident.Evidence)
if err != nil {
return web.Fail(err)
}
argumentsJSON, _ := json.Marshal([]string{scope, target, "--ai"})
operator := models.CurrentUser(request)
proposal := models.RemediationProposal{
Scope: scope, Target: target, DoctorExecutionID: incident.ExecutionID,
EvidenceHash: evidenceHash, EvidenceJSON: string(evidenceJSON), Command: "doctor",
ArgumentsJSON: string(argumentsJSON), RiskLevel: incident.Proposal.RiskLevel,
Rollback: form.Rollback, Status: "pending", CreatedBy: operator.Email,
}
if err := model.Create(request, &proposal); err != nil {
return web.Fail(err)
}
return web.Redirect(fmt.Sprintf("/proposals/%d", proposal.ID))
}
func Show(request *web.Request) *web.Response {
id, err := request.PathInt("proposal")
if err != nil {
return web.Fail(err)
}
proposal, err := model.Query[models.RemediationProposal]().Find(request, id)
if err != nil {
return web.Fail(err)
}
return inertia.Render("Proposal", inertia.Props{"proposal": proposal})
}
func Approve(request *web.Request) *web.Response {
id, err := request.PathInt("proposal")
if err != nil {
return web.Fail(err)
}
proposal, err := model.Query[models.RemediationProposal]().Find(request, id)
if err != nil {
return web.Fail(err)
}
if proposal.Status != "pending" {
return web.Abort(http.StatusConflict)
}
client := services.From(request.Context()).Jeannie
current, err := client.Doctor(request.Context(), proposal.Scope, proposal.Target)
if err != nil {
return web.Fail(err)
}
fingerprint, err := services.EvidenceFingerprint(current)
if err != nil {
return web.Fail(err)
}
if fingerprint != proposal.EvidenceHash {
_ = model.Update(request, &proposal, db.Values{"status": "stale"})
return web.Abort(http.StatusConflict)
}
allowed, err := client.CommandAllowed(request.Context(), proposal.Command)
if err != nil {
return web.Fail(err)
}
if !allowed {
return web.Abort(http.StatusForbidden)
}
var arguments []string
if err := json.Unmarshal([]byte(proposal.ArgumentsJSON), &arguments); err != nil {
return web.Fail(err)
}
if len(arguments) != 3 || arguments[0] != proposal.Scope || arguments[1] != proposal.Target || arguments[2] != "--ai" {
return web.Abort(http.StatusForbidden)
}
operator := models.CurrentUser(request)
err = db.Transaction(request, func(ctx context.Context) error {
locked, err := model.Query[models.RemediationProposal]().LockForUpdate().Find(ctx, proposal.ID)
if err != nil {
return err
}
if locked.Status != "pending" {
return fmt.Errorf("proposal is no longer pending")
}
approval := models.RemediationApproval{
ProposalID: proposal.ID, OperatorEmail: operator.Email,
EvidenceHash: proposal.EvidenceHash, ApprovedAt: time.Now().UTC(),
}
if err := model.Create(ctx, &approval); err != nil {
return err
}
return model.Update(ctx, &locked, db.Values{"status": "approved"})
})
if err != nil {
return web.Fail(err)
}
return web.Redirect(fmt.Sprintf("/proposals/%d", proposal.ID))
}