package connectors import ( "context" "errors" "fmt" "net/http" "slices" "strings" "github.com/danielgtaylor/huma/v2" "github.com/google/uuid" "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: the package and its instance // installations are deleted, and their processes stopped. A name that isn't an // accepted package of this route's contract is a 404, which keeps a repeated // removal harmless. A package an organisation or a person still has an // installation of is refused, and so is a secret engine a stored credential // still resolves through, since those credentials would stop resolving. func (m *Module) removeAs(ctx context.Context, in *nameInput, contract string) (*httpapi.Reply[packageState], error) { name := in.Name installed, err := m.store.List(ctx) if err != nil { m.log.ErrorContext(ctx, "list installations", "error", err) return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the installations") } i := slices.IndexFunc(installed, func(a store.Installed) bool { return a.Scope == store.ScopeInstance && a.Name == name && a.Contract == contract }) if i < 0 { return nil, huma.NewError(http.StatusNotFound, fmt.Sprintf("no accepted %s named %s", contract, name)) } if err := m.checkEngineUnused(ctx, installed[i]); err != nil { return nil, err } found, err := m.store.DeletePackage(ctx, installed[i].PackageName) switch { case errors.Is(err, store.ErrInstalled): return nil, huma.NewError(http.StatusConflict, fmt.Sprintf("%s is still installed by an organisation or a person", name)) case err != nil: m.log.ErrorContext(ctx, "remove accepted package", "package", name, "error", err) return nil, huma.NewError(http.StatusInternalServerError, "couldn't remove "+name) case !found: return nil, huma.NewError(http.StatusNotFound, fmt.Sprintf("no accepted %s named %s", contract, name)) } for _, a := range installed { if a.PackageID == installed[i].PackageID { m.forget(a.ID.String()) } } if _, err := m.refresh(ctx); err != nil { m.log.ErrorContext(ctx, "read installations", "error", err) } // 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 } // checkEngineUnused refuses the removal of a secret engine that a stored // credential still resolves through. func (m *Module) checkEngineUnused(ctx context.Context, a store.Installed) error { if a.Contract != store.ContractSecretEngine { return nil } users, err := m.engineUsers(ctx, a) if err != nil { m.log.ErrorContext(ctx, "check secret engine use", "engine", a.Name, "error", err) return huma.NewError(http.StatusInternalServerError, "couldn't check the credentials that use "+a.Name) } if len(users) > 0 { return huma.NewError(http.StatusConflict, fmt.Sprintf("secret engine %s is still used by credentials: %s", a.Name, strings.Join(users, ", "))) } return nil } // engineUsers names, in order, the credentials with a field resolved through // the secret engine installation a: those its owner's scope can reach. func (m *Module) engineUsers(ctx context.Context, a store.Installed) ([]string, error) { creds, err := m.store.Credentials(ctx) if err != nil { return nil, err } var users []string for _, c := range creds { if !uses(c.Owner(), a.Owner()) { continue } fields, err := m.openCredential(c) if err != nil { return nil, err } for _, f := range fields { if f.Engine == a.Name { users = append(users, c.Name) break } } } return users, nil } type installationInput struct { ID uuid.UUID `path:"id"` } // removeInstallation deletes one installation the caller manages and stops it. // The package stays accepted. func (m *Module) removeInstallation(ctx context.Context, in *installationInput) (*httpapi.Reply[packageState], error) { a, _, err := m.installationFor(ctx, in.ID, true) if err != nil { return nil, err } if err := m.checkEngineUnused(ctx, a); err != nil { return nil, err } found, err := m.store.DeleteInstallation(ctx, a.ID) if err != nil { m.log.ErrorContext(ctx, "remove installation", "installation", a.Name, "error", err) return nil, huma.NewError(http.StatusInternalServerError, "couldn't remove "+a.Name) } if !found { return nil, huma.NewError(http.StatusNotFound, "no such installation") } m.forget(a.ID.String()) if _, err := m.refresh(ctx); err != nil { m.log.ErrorContext(ctx, "read installations", "error", err) } m.log.InfoContext(ctx, "installation removed", "audit", true, "action", "installation.remove", "installation", a.ID.String(), "scope", a.Scope, "package", a.PackageName) return httpapi.OK(packageState{Name: a.Name, State: "removed"}), nil } // installationFor reads one installation the caller can see. With manage set, // it must be one the caller may change; an installation the caller may not see // is a 404, so another organisation's installations are not confirmed to exist. func (m *Module) installationFor(ctx context.Context, id uuid.UUID, manage bool) (store.Installed, authority, error) { auth, err := authorityOf(ctx) if err != nil { return store.Installed{}, authority{}, err } installed, err := m.store.List(ctx) if err != nil { m.log.ErrorContext(ctx, "list installations", "error", err) return store.Installed{}, authority{}, huma.NewError(http.StatusInternalServerError, "couldn't read the installations") } i := slices.IndexFunc(installed, func(a store.Installed) bool { return a.ID == id }) if i < 0 || !auth.sees(installed[i].Owner()) { return store.Installed{}, authority{}, huma.NewError(http.StatusNotFound, "no such installation") } if manage && !auth.manages(installed[i].Owner()) { return store.Installed{}, authority{}, huma.NewError(http.StatusForbidden, "you may not change this installation") } return installed[i], auth, nil }