222 lines
6.6 KiB
Go
222 lines
6.6 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)
|
|
}
|
|
arguments, ok := services.RemediationArguments(scope, target)
|
|
if !ok {
|
|
return web.Abort(http.StatusForbidden)
|
|
}
|
|
argumentsJSON, _ := json.Marshal(arguments)
|
|
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 !services.ValidRemediationArguments(proposal.Scope, proposal.Target, arguments) {
|
|
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))
|
|
}
|
|
|
|
func Execute(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 != "approved" {
|
|
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 !services.ValidRemediationArguments(proposal.Scope, proposal.Target, arguments) {
|
|
return web.Abort(http.StatusForbidden)
|
|
}
|
|
|
|
execution := models.OperationExecution{ProposalID: proposal.ID, Status: "submitting"}
|
|
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 != "approved" {
|
|
return fmt.Errorf("proposal is no longer approved")
|
|
}
|
|
if err := model.Create(ctx, &execution); err != nil {
|
|
return err
|
|
}
|
|
return model.Update(ctx, &locked, db.Values{"status": "submitting"})
|
|
})
|
|
if err != nil {
|
|
return web.Fail(err)
|
|
}
|
|
|
|
remote, err := client.Submit(request.Context(), proposal.Command, arguments)
|
|
if err != nil {
|
|
_ = model.Update(request, &execution, db.Values{"status": "submit_failed", "error": err.Error()})
|
|
_ = model.Update(request, &proposal, db.Values{"status": "submit_failed"})
|
|
return web.Redirect(fmt.Sprintf("/executions/%d", execution.ID))
|
|
}
|
|
if err := model.Update(request, &execution, db.Values{
|
|
"jeannie_execution_id": remote.ID, "status": remote.Status,
|
|
}); err != nil {
|
|
return web.Fail(err)
|
|
}
|
|
if err := model.Update(request, &proposal, db.Values{"status": "executing"}); err != nil {
|
|
return web.Fail(err)
|
|
}
|
|
return web.Redirect(fmt.Sprintf("/executions/%d", execution.ID))
|
|
}
|