Every /api operation is registered with Huma behind one admin-token middleware, served at /api/openapi.json with a docs page at /api/docs, and committed as api/openapi.yaml, which task check keeps current.
88 lines
3.1 KiB
Go
88 lines
3.1 KiB
Go
package connectors
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
|
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
|
|
)
|
|
|
|
func (m *Module) remove(contract string) func(context.Context, *nameInput) (*httpapi.Reply[packageState], error) {
|
|
return func(ctx context.Context, in *nameInput) (*httpapi.Reply[packageState], error) {
|
|
return m.removeAs(ctx, in, 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(ctx context.Context, in *nameInput, contract string) (*httpapi.Reply[packageState], error) {
|
|
name := in.Name
|
|
accepted, err := m.store.List(ctx, m.orgID)
|
|
if err != nil {
|
|
m.log.ErrorContext(ctx, "list accepted packages", "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the accepted packages")
|
|
}
|
|
if !slices.ContainsFunc(accepted, func(a store.Accepted) bool { return a.Name == name && a.Contract == contract }) {
|
|
return nil, huma.NewError(http.StatusNotFound, fmt.Sprintf("no accepted %s named %s", contract, name))
|
|
}
|
|
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)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't check the credentials that use "+name)
|
|
}
|
|
if len(users) > 0 {
|
|
return nil, huma.NewError(http.StatusConflict,
|
|
fmt.Sprintf("secret engine %s is still used by credentials: %s", name, strings.Join(users, ", ")))
|
|
}
|
|
}
|
|
found, err := m.store.Delete(ctx, m.orgID, name)
|
|
if err != nil {
|
|
m.log.ErrorContext(ctx, "remove accepted package", "package", name, "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't remove "+name)
|
|
}
|
|
if !found {
|
|
return nil, huma.NewError(http.StatusNotFound, fmt.Sprintf("no accepted %s named %s", contract, name))
|
|
}
|
|
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)
|
|
return httpapi.OK(packageState{Name: name, State: "removed"}), nil
|
|
}
|
|
|
|
// 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
|
|
}
|