Files
archie/modules/connectors/remove.go
T

91 lines
3.0 KiB
Go

package connectors
import (
"context"
"fmt"
"net/http"
"slices"
"strings"
"github.com/samcharles93/archie-core/internal/platform/httpjson"
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
)
func (m *Module) remove(contract string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { m.removeAs(w, r, contract) }
}
// removeAs retires an accepted package: its row (and so its credential slots)
// is deleted and its process stopped. A name that isn't accepted under this
// route's contract is a 404, which keeps a repeated removal harmless. A secret
// engine a stored credential still resolves through is refused, since those
// credentials would stop resolving.
func (m *Module) removeAs(w http.ResponseWriter, r *http.Request, contract string) {
ctx := r.Context()
name := r.PathValue("name")
accepted, err := m.store.List(ctx, m.orgID)
if err != nil {
m.log.ErrorContext(ctx, "list accepted packages", "error", err)
httpjson.Error(w, http.StatusInternalServerError, "couldn't read the accepted packages")
return
}
if !slices.ContainsFunc(accepted, func(a store.Accepted) bool { return a.Name == name && a.Contract == contract }) {
httpjson.Error(w, http.StatusNotFound, fmt.Sprintf("no accepted %s named %s", contract, name))
return
}
if contract == store.ContractSecretEngine {
users, err := m.engineUsers(ctx, name)
if err != nil {
m.log.ErrorContext(ctx, "check secret engine use", "engine", name, "error", err)
httpjson.Error(w, http.StatusInternalServerError, "couldn't check the credentials that use "+name)
return
}
if len(users) > 0 {
httpjson.Error(w, http.StatusConflict,
fmt.Sprintf("secret engine %s is still used by credentials: %s", name, strings.Join(users, ", ")))
return
}
}
found, err := m.store.Delete(ctx, m.orgID, name)
if err != nil {
m.log.ErrorContext(ctx, "remove accepted package", "package", name, "error", err)
httpjson.Error(w, http.StatusInternalServerError, "couldn't remove "+name)
return
}
if !found {
httpjson.Error(w, http.StatusNotFound, fmt.Sprintf("no accepted %s named %s", contract, name))
return
}
m.release(name)
m.mu.Lock()
delete(m.failed, name)
m.mu.Unlock()
// There is no audit trail module yet, so the removal is a structured log
// line.
m.log.InfoContext(ctx, "accepted package removed", "audit", true, "action", "package.remove", "contract", contract, "package", name)
httpjson.Write(w, http.StatusOK, map[string]string{"name": name, "state": "removed"})
}
// engineUsers names, in order, the credentials with a field resolved through
// the secret engine.
func (m *Module) engineUsers(ctx context.Context, engine string) ([]string, error) {
creds, err := m.store.Credentials(ctx, m.orgID)
if err != nil {
return nil, err
}
var users []string
for _, c := range creds {
fields, err := m.openCredential(c)
if err != nil {
return nil, err
}
for _, f := range fields {
if f.Engine == engine {
users = append(users, c.Name)
break
}
}
}
return users, nil
}